-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Improve logging system using logLevel, avoid trace value
#15232
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
069d01c
Improve server logging
dhruvmanila 7ad0e66
Rename `trace.rs` to `logging.rs`
dhruvmanila 1fca6dc
Update docs
dhruvmanila 3887d2d
Fix clippy
dhruvmanila f75e1fa
Add trace spans for request and notification
dhruvmanila f2c09e7
Address review comments (1)
dhruvmanila 14f735b
Enter span locally for each request / notification
dhruvmanila File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| //! The logging system for `red_knot server`. | ||
| //! | ||
| //! Log messages are controlled by the `logLevel` setting which defaults to `"info"`. Log messages | ||
| //! are written to `stderr` by default, which should appear in the logs for most LSP clients. A | ||
| //! `logFile` path can also be specified in the settings, and output will be directed there | ||
| //! instead. | ||
| use core::str; | ||
| use serde::Deserialize; | ||
| use std::{path::PathBuf, str::FromStr, sync::Arc}; | ||
| use tracing::level_filters::LevelFilter; | ||
| use tracing_subscriber::{ | ||
| fmt::{time::Uptime, writer::BoxMakeWriter}, | ||
| layer::SubscriberExt, | ||
| Layer, | ||
| }; | ||
|
|
||
| pub(crate) fn init_logging(log_level: LogLevel, log_file: Option<&std::path::Path>) { | ||
| let log_file = log_file | ||
| .map(|path| { | ||
| // this expands `logFile` so that tildes and environment variables | ||
| // are replaced with their values, if possible. | ||
| if let Some(expanded) = shellexpand::full(&path.to_string_lossy()) | ||
| .ok() | ||
| .and_then(|path| PathBuf::from_str(&path).ok()) | ||
| { | ||
| expanded | ||
| } else { | ||
| path.to_path_buf() | ||
| } | ||
| }) | ||
| .and_then(|path| { | ||
| std::fs::OpenOptions::new() | ||
| .create(true) | ||
| .append(true) | ||
| .open(&path) | ||
| .map_err(|err| { | ||
| #[allow(clippy::print_stderr)] | ||
| { | ||
| eprintln!( | ||
| "Failed to open file at {} for logging: {err}", | ||
| path.display() | ||
| ); | ||
| } | ||
| }) | ||
| .ok() | ||
| }); | ||
|
|
||
| let logger = match log_file { | ||
| Some(file) => BoxMakeWriter::new(Arc::new(file)), | ||
| None => BoxMakeWriter::new(std::io::stderr), | ||
| }; | ||
| let subscriber = tracing_subscriber::Registry::default().with( | ||
| tracing_subscriber::fmt::layer() | ||
| .with_timer(Uptime::default()) | ||
| .with_thread_names(true) | ||
| .with_ansi(false) | ||
| .with_writer(logger) | ||
| .with_filter(LogLevelFilter { filter: log_level }), | ||
| ); | ||
|
|
||
| tracing::subscriber::set_global_default(subscriber) | ||
| .expect("should be able to set global default subscriber"); | ||
| } | ||
|
|
||
| /// The log level for the server as provided by the client during initialization. | ||
| /// | ||
| /// The default log level is `info`. | ||
| #[derive(Clone, Copy, Debug, Deserialize, Default, PartialEq, Eq, PartialOrd, Ord)] | ||
| #[serde(rename_all = "lowercase")] | ||
| pub(crate) enum LogLevel { | ||
| Error, | ||
| Warn, | ||
| #[default] | ||
| Info, | ||
| Debug, | ||
| Trace, | ||
| } | ||
|
|
||
| impl LogLevel { | ||
| fn trace_level(self) -> tracing::Level { | ||
| match self { | ||
| Self::Error => tracing::Level::ERROR, | ||
| Self::Warn => tracing::Level::WARN, | ||
| Self::Info => tracing::Level::INFO, | ||
| Self::Debug => tracing::Level::DEBUG, | ||
| Self::Trace => tracing::Level::TRACE, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Filters out traces which have a log level lower than the `logLevel` set by the client. | ||
| struct LogLevelFilter { | ||
| filter: LogLevel, | ||
| } | ||
|
|
||
| impl<S> tracing_subscriber::layer::Filter<S> for LogLevelFilter { | ||
| fn enabled( | ||
| &self, | ||
| meta: &tracing::Metadata<'_>, | ||
| _: &tracing_subscriber::layer::Context<'_, S>, | ||
| ) -> bool { | ||
| let filter = if meta.target().starts_with("red_knot") { | ||
| self.filter.trace_level() | ||
| } else { | ||
| tracing::Level::INFO | ||
| }; | ||
|
|
||
| meta.level() <= &filter | ||
| } | ||
|
|
||
| fn max_level_hint(&self) -> Option<tracing::level_filters::LevelFilter> { | ||
| Some(LevelFilter::from_level(self.filter.trace_level())) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 0 additions & 25 deletions
25
crates/red_knot_server/src/server/api/notifications/set_trace.rs
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.