Skip to content

Commit ba6430d

Browse files
Rollup merge of #152897 - jdonszelmann:logging, r=jieyouxu
Add optional json logging I became slightly frustrated with rust's logging output, and wanted to experiment a little with slightly postprocessing it with custom scripts. I think this is a pretty uninteresting change otherwise, shouldn't affect anyone else nor bother anyone. It's small enough that we can remove it again, nor is any of this stable or expected to be. r? @jieyouxu
2 parents 8583fea + 3f375ff commit ba6430d

5 files changed

Lines changed: 96 additions & 19 deletions

File tree

Cargo.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5751,6 +5751,16 @@ dependencies = [
57515751
"tracing-core",
57525752
]
57535753

5754+
[[package]]
5755+
name = "tracing-serde"
5756+
version = "0.2.0"
5757+
source = "registry+https://github.com/rust-lang/crates.io-index"
5758+
checksum = "704b1aeb7be0d0a84fc9828cae51dab5970fee5088f83d1dd7ee6f6246fc6ff1"
5759+
dependencies = [
5760+
"serde",
5761+
"tracing-core",
5762+
]
5763+
57545764
[[package]]
57555765
name = "tracing-subscriber"
57565766
version = "0.3.20"
@@ -5762,12 +5772,15 @@ dependencies = [
57625772
"once_cell",
57635773
"parking_lot",
57645774
"regex-automata",
5775+
"serde",
5776+
"serde_json",
57655777
"sharded-slab",
57665778
"smallvec",
57675779
"thread_local",
57685780
"tracing",
57695781
"tracing-core",
57705782
"tracing-log",
5783+
"tracing-serde",
57715784
]
57725785

57735786
[[package]]

compiler/rustc_log/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ edition = "2024"
77
# tidy-alphabetical-start
88
tracing = "0.1.41"
99
tracing-core = "0.1.34"
10-
tracing-subscriber = { version = "0.3.3", default-features = false, features = ["fmt", "env-filter", "smallvec", "parking_lot", "ansi"] }
10+
tracing-subscriber = { version = "0.3.3", default-features = false, features = ["fmt", "env-filter", "smallvec", "parking_lot", "ansi", "json"] }
1111
tracing-tree = "0.3.1"
1212
# tidy-alphabetical-end
1313

compiler/rustc_log/src/lib.rs

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,18 @@
3535
3636
use std::env::{self, VarError};
3737
use std::fmt::{self, Display};
38+
use std::fs::File;
3839
use std::io::{self, IsTerminal};
40+
use std::sync::Mutex;
3941

4042
use tracing::dispatcher::SetGlobalDefaultError;
4143
use tracing::{Event, Subscriber};
42-
use tracing_subscriber::Registry;
4344
use tracing_subscriber::filter::{Directive, EnvFilter, LevelFilter};
4445
use tracing_subscriber::fmt::FmtContext;
45-
use tracing_subscriber::fmt::format::{self, FormatEvent, FormatFields};
46+
use tracing_subscriber::fmt::format::{self, FmtSpan, FormatEvent, FormatFields};
47+
use tracing_subscriber::fmt::writer::BoxMakeWriter;
4648
use tracing_subscriber::layer::SubscriberExt;
49+
use tracing_subscriber::{Layer, Registry};
4750
// Re-export tracing
4851
pub use {tracing, tracing_core, tracing_subscriber};
4952

@@ -55,12 +58,15 @@ pub struct LoggerConfig {
5558
pub verbose_entry_exit: Result<String, VarError>,
5659
pub verbose_thread_ids: Result<String, VarError>,
5760
pub backtrace: Result<String, VarError>,
61+
pub json: Result<String, VarError>,
62+
pub output_target: Result<String, VarError>,
5863
pub wraptree: Result<String, VarError>,
5964
pub lines: Result<String, VarError>,
6065
}
6166

6267
impl LoggerConfig {
6368
pub fn from_env(env: &str) -> Self {
69+
// NOTE: documented in the dev guide. If you change this, also update it!
6470
LoggerConfig {
6571
filter: env::var(env),
6672
color_logs: env::var(format!("{env}_COLOR")),
@@ -69,6 +75,8 @@ impl LoggerConfig {
6975
backtrace: env::var(format!("{env}_BACKTRACE")),
7076
wraptree: env::var(format!("{env}_WRAPTREE")),
7177
lines: env::var(format!("{env}_LINES")),
78+
json: env::var(format!("{env}_FORMAT_JSON")),
79+
output_target: env::var(format!("{env}_OUTPUT_TARGET")),
7280
}
7381
}
7482
}
@@ -136,23 +144,59 @@ where
136144
Err(_) => false,
137145
};
138146

139-
let mut layer = tracing_tree::HierarchicalLayer::default()
140-
.with_writer(io::stderr)
141-
.with_ansi(color_logs)
142-
.with_targets(true)
143-
.with_verbose_exit(verbose_entry_exit)
144-
.with_verbose_entry(verbose_entry_exit)
145-
.with_indent_amount(2)
146-
.with_indent_lines(lines)
147-
.with_thread_ids(verbose_thread_ids)
148-
.with_thread_names(verbose_thread_ids);
149-
150-
if let Ok(v) = cfg.wraptree {
151-
match v.parse::<usize>() {
152-
Ok(v) => layer = layer.with_wraparound(v),
153-
Err(_) => return Err(Error::InvalidWraptree(v)),
147+
let json = match cfg.json {
148+
Ok(v) => &v == "1",
149+
Err(_) => false,
150+
};
151+
152+
let output_target: BoxMakeWriter = match cfg.output_target {
153+
Ok(v) => match File::options().create(true).write(true).truncate(true).open(&v) {
154+
Ok(i) => BoxMakeWriter::new(Mutex::new(i)),
155+
Err(e) => {
156+
eprintln!("couldn't open {v} as a log target: {e:?}");
157+
BoxMakeWriter::new(io::stderr)
158+
}
159+
},
160+
Err(_) => BoxMakeWriter::new(io::stderr),
161+
};
162+
163+
let layer = if json {
164+
let format = tracing_subscriber::fmt::format()
165+
.json()
166+
.with_span_list(true)
167+
.with_source_location(true);
168+
169+
let fmt_layer = tracing_subscriber::fmt::layer()
170+
.json()
171+
.event_format(format)
172+
.with_writer(output_target)
173+
.with_target(true)
174+
.with_ansi(false)
175+
.with_thread_ids(verbose_thread_ids)
176+
.with_thread_names(verbose_thread_ids)
177+
.with_span_events(FmtSpan::ACTIVE);
178+
Layer::boxed(fmt_layer)
179+
} else {
180+
let mut layer = tracing_tree::HierarchicalLayer::default()
181+
.with_writer(output_target)
182+
.with_ansi(color_logs)
183+
.with_targets(true)
184+
.with_verbose_exit(verbose_entry_exit)
185+
.with_verbose_entry(verbose_entry_exit)
186+
.with_indent_amount(2)
187+
.with_indent_lines(lines)
188+
.with_thread_ids(verbose_thread_ids)
189+
.with_thread_names(verbose_thread_ids);
190+
191+
if let Ok(v) = cfg.wraptree {
192+
match v.parse::<usize>() {
193+
Ok(v) => layer = layer.with_wraparound(v),
194+
Err(_) => return Err(Error::InvalidWraptree(v)),
195+
}
154196
}
155-
}
197+
198+
Layer::boxed(layer)
199+
};
156200

157201
let subscriber = build_subscriber();
158202
// NOTE: It is important to make sure that the filter is applied on the last layer

src/doc/rustc-dev-guide/src/tracing.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@ To see the logs, you need to set the `RUSTC_LOG` environment variable to your lo
1212
The full syntax of the log filters can be found in the [rustdoc
1313
of `tracing-subscriber`](https://docs.rs/tracing-subscriber/0.2.24/tracing_subscriber/filter/struct.EnvFilter.html#directives).
1414

15+
## Environment variables
16+
17+
This is an overview of the environment variables rustc accepts to customize
18+
its tracing output. The definition of these can mostly be found in `compiler/rustc_log/src/lib.rs`.
19+
20+
| Name | Usage |
21+
| ------------------------- | ----------------------------------------------------------------------------------------------------------------------- |
22+
| `RUSTC_LOG` | Tracing filters (see the rest of this page) |
23+
| `RUSTC_LOG_COLOR` | `always`, `never` or `auto`. |
24+
| `RUSTC_LOG_ENTRY_EXIT` | if set and not '0', log span entry/exit. |
25+
| `RUSTC_LOG_THREAD_IDS` | if set and equal to '1', also log thread ids. |
26+
| `RUSTC_LOG_BACKTRACE` | Capture and print a backtrace if a trace is emitted with a target that matches the provided string value. |
27+
| `RUSTC_LOG_LINES` | If `1`, indents log lines based on depth. |
28+
| `RUSTC_LOG_FORMAT_JSON` | If `1`, outputs logs as JSON. The exact parameters can be found in `rustc_log/src/lib.rs` but the format is *UNSTABLE*. |
29+
| `RUSTC_LOG_OUTPUT_TARGET` | If provided, logs go to the provided file name instead of stderr. |
30+
31+
32+
33+
1534
## Function level filters
1635

1736
Lots of functions in rustc are annotated with

src/tools/tidy/src/deps.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
450450
"tracing-attributes",
451451
"tracing-core",
452452
"tracing-log",
453+
"tracing-serde",
453454
"tracing-subscriber",
454455
"tracing-tree",
455456
"twox-hash",

0 commit comments

Comments
 (0)