[ty] Use better datetime format for server logs (#19083)

This PR improves the timer format for ty server logs to be same as Ruff.

Ref: https://github.com/astral-sh/ruff/pull/16389
This commit is contained in:
Dhruv Manilawala 2025-07-02 10:09:12 +05:30 committed by GitHub
parent 316c1b21e2
commit c3d9b21db5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 14 deletions

View file

@ -31,7 +31,7 @@ serde = { workspace = true }
serde_json = { workspace = true } serde_json = { workspace = true }
shellexpand = { workspace = true } shellexpand = { workspace = true }
tracing = { workspace = true } tracing = { workspace = true }
tracing-subscriber = { workspace = true } tracing-subscriber = { workspace = true, features = ["chrono"] }
[dev-dependencies] [dev-dependencies]

View file

@ -4,17 +4,18 @@
//! are written to `stderr` by default, which should appear in the logs for most LSP clients. A //! 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 //! `logFile` path can also be specified in the settings, and output will be directed there
//! instead. //! instead.
use core::str; use std::path::{Path, PathBuf};
use serde::Deserialize; use std::str::FromStr;
use std::{path::PathBuf, str::FromStr, sync::Arc}; use std::sync::Arc;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::{
Layer,
fmt::{time::Uptime, writer::BoxMakeWriter},
layer::SubscriberExt,
};
pub(crate) fn init_logging(log_level: LogLevel, log_file: Option<&std::path::Path>) { use serde::Deserialize;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::Layer;
use tracing_subscriber::fmt::time::ChronoLocal;
use tracing_subscriber::fmt::writer::BoxMakeWriter;
use tracing_subscriber::layer::SubscriberExt;
pub(crate) fn init_logging(log_level: LogLevel, log_file: Option<&Path>) {
let log_file = log_file let log_file = log_file
.map(|path| { .map(|path| {
// this expands `logFile` so that tildes and environment variables // this expands `logFile` so that tildes and environment variables
@ -49,10 +50,11 @@ pub(crate) fn init_logging(log_level: LogLevel, log_file: Option<&std::path::Pat
Some(file) => BoxMakeWriter::new(Arc::new(file)), Some(file) => BoxMakeWriter::new(Arc::new(file)),
None => BoxMakeWriter::new(std::io::stderr), None => BoxMakeWriter::new(std::io::stderr),
}; };
let is_trace_level = log_level == LogLevel::Trace;
let subscriber = tracing_subscriber::Registry::default().with( let subscriber = tracing_subscriber::Registry::default().with(
tracing_subscriber::fmt::layer() tracing_subscriber::fmt::layer()
.with_timer(Uptime::default()) .with_timer(ChronoLocal::new("%Y-%m-%d %H:%M:%S.%f".to_string()))
.with_thread_names(true) .with_thread_names(is_trace_level)
.with_ansi(false) .with_ansi(false)
.with_writer(logger) .with_writer(logger)
.with_filter(LogLevelFilter { filter: log_level }), .with_filter(LogLevelFilter { filter: log_level }),
@ -108,7 +110,7 @@ impl<S> tracing_subscriber::layer::Filter<S> for LogLevelFilter {
meta.level() <= &filter meta.level() <= &filter
} }
fn max_level_hint(&self) -> Option<tracing::level_filters::LevelFilter> { fn max_level_hint(&self) -> Option<LevelFilter> {
Some(LevelFilter::from_level(self.filter.trace_level())) Some(LevelFilter::from_level(self.filter.trace_level()))
} }
} }