Skip to content

minor: simplify logging implementation #11625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/rust-analyzer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ tracing-subscriber = { version = "0.3", default-features = false, features = [
"fmt",
"tracing-log",
] }
tracing-log = "0.1.2"
tracing-tree = "0.2"
always-assert = "0.1"

Expand Down
105 changes: 17 additions & 88 deletions crates/rust-analyzer/src/bin/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@
//! filter syntax and `tracing_appender` for non blocking output.

use std::{
fmt,
fs::File,
io::{self, Stderr},
sync::Arc,
};

use rust_analyzer::Result;
use tracing::{level_filters::LevelFilter, Event, Subscriber};
use tracing_log::NormalizeEvent;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::{
fmt::{
format::Writer, writer::BoxMakeWriter, FmtContext, FormatEvent, FormatFields,
FormattedFields, MakeWriter,
},
fmt::{writer::BoxMakeWriter, MakeWriter},
layer::SubscriberExt,
registry::LookupSpan,
util::SubscriberInitExt,
EnvFilter, Registry,
};
Expand Down Expand Up @@ -63,93 +57,28 @@ impl Logger {
})
.ok();

let chalk_layer = HierarchicalLayer::default()
.with_indent_lines(true)
.with_ansi(false)
.with_indent_amount(2)
.with_writer(io::stderr);

let writer = match self.file {
Some(file) => BoxMakeWriter::new(Arc::new(file)),
None => BoxMakeWriter::new(io::stderr),
};
let ra_fmt_layer =
tracing_subscriber::fmt::layer().event_format(LoggerFormatter).with_writer(writer);

match chalk_level_dir {
Some(val) => {
Registry::default()
.with(
self.filter
.add_directive(format!("chalk_solve={}", val).parse()?)
.add_directive(format!("chalk_ir={}", val).parse()?)
.add_directive(format!("chalk_recursive={}", val).parse()?),
)
.with(ra_fmt_layer)
.with(chalk_layer)
.init();
}
None => {
Registry::default().with(self.filter).with(ra_fmt_layer).init();
}
};

Ok(())
}
}

#[derive(Debug)]
struct LoggerFormatter;

impl<S, N> FormatEvent<S, N> for LoggerFormatter
where
S: Subscriber + for<'a> LookupSpan<'a>,
N: for<'a> FormatFields<'a> + 'static,
{
fn format_event(
&self,
ctx: &FmtContext<'_, S, N>,
mut writer: Writer,
event: &Event<'_>,
) -> fmt::Result {
// Write level and target
let level = *event.metadata().level();

// If this event is issued from `log` crate, then the value of target is
// always "log". `tracing-log` has hard coded it for some reason, so we
// need to extract it using `normalized_metadata` method which is part of
// `tracing_log::NormalizeEvent`.
let target = match event.normalized_metadata() {
Comment on lines -117 to -122
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment seems to be either wrong or outdated from my testing so we don't need this custom formatter any longer

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually nevermind, it still has this behaviour? Hmm, that is a bit annoying. Guess we need some changes to land in tracing-tree before we can properly use it for our things.
https://github.com/tokio-rs/tracing/blob/master/tracing-log/src/lib.rs#L165-L193
https://github.com/tokio-rs/tracing/blob/master/tracing-log/src/lib.rs#L277-L320

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking back into this the reason is described here https://docs.rs/tracing-log/latest/tracing_log/trait.NormalizeEvent.html

// This event is issued from `log` crate
Some(log) => log.target(),
None => event.metadata().target(),
};
write!(writer, "[{} {}] ", level, target)?;

// Write spans and fields of each span
ctx.visit_spans(|span| {
write!(writer, "{}", span.name())?;

let ext = span.extensions();

// `FormattedFields` is a a formatted representation of the span's
// fields, which is stored in its extensions by the `fmt` layer's
// `new_span` method. The fields will have been formatted
// by the same field formatter that's provided to the event
// formatter in the `FmtContext`.
let fields = &ext.get::<FormattedFields<N>>().expect("will never be `None`");
let ra_fmt_layer = HierarchicalLayer::default()
.with_indent_lines(true)
.with_ansi(false)
.with_indent_amount(2)
.with_targets(true)
.with_writer(writer);

if !fields.is_empty() {
write!(writer, "{{{}}}", fields)?;
}
write!(writer, ": ")?;
let mut filter = self.filter;

Ok(())
})?;
if let Some(val) = chalk_level_dir {
filter = filter
.add_directive(format!("chalk_solve={}", val).parse()?)
.add_directive(format!("chalk_ir={}", val).parse()?)
.add_directive(format!("chalk_recursive={}", val).parse()?);
}

// Write fields on the event
ctx.field_format().format_fields(writer.by_ref(), event)?;
Registry::default().with(filter).with(ra_fmt_layer).init();

writeln!(writer)
Ok(())
}
}