Skip to content

Commit e869302

Browse files
chore: improve logs (#266)
* yes yes and yes * use bunyan * add comment * format * loglvl comment * merge * woops * import * ok * remove log kind env * default debug * comment * add hierarchical layer
1 parent d90148c commit e869302

File tree

16 files changed

+234
-84
lines changed

16 files changed

+234
-84
lines changed

Cargo.lock

+55-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+12-11
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,18 @@ similar = "2.6.0"
4141
smallvec = { version = "1.13.2", features = ["union", "const_new", "serde"] }
4242
strum = { version = "0.27.1", features = ["derive"] }
4343
# this will use tokio if available, otherwise async-std
44-
sqlx = { version = "0.8.2", features = ["runtime-tokio", "runtime-async-std", "postgres", "json"] }
45-
syn = "1.0.109"
46-
termcolor = "1.4.1"
47-
test-log = "0.2.17"
48-
tokio = { version = "1.40.0", features = ["full"] }
49-
tower-lsp = "0.20.0"
50-
tracing = { version = "0.1.40", default-features = false, features = ["std"] }
51-
tracing-subscriber = "0.3.18"
52-
tree-sitter = "0.20.10"
53-
tree_sitter_sql = { path = "./lib/tree_sitter_sql", version = "0.0.0" }
54-
unicode-width = "0.1.12"
44+
sqlx = { version = "0.8.2", features = ["runtime-tokio", "runtime-async-std", "postgres", "json"] }
45+
syn = "1.0.109"
46+
termcolor = "1.4.1"
47+
test-log = "0.2.17"
48+
tokio = { version = "1.40.0", features = ["full"] }
49+
tower-lsp = "0.20.0"
50+
tracing = { version = "0.1.40", default-features = false, features = ["std"] }
51+
tracing-bunyan-formatter = { version = "0.3.10 " }
52+
tracing-subscriber = "0.3.18"
53+
tree-sitter = "0.20.10"
54+
tree_sitter_sql = { path = "./lib/tree_sitter_sql", version = "0.0.0" }
55+
unicode-width = "0.1.12"
5556

5657
# postgres specific crates
5758
pgt_analyse = { path = "./crates/pgt_analyse", version = "0.0.0" }

crates/pgt_cli/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ serde_json = { workspace = true }
3737
tokio = { workspace = true, features = ["io-std", "io-util", "net", "time", "rt", "sync", "rt-multi-thread", "macros"] }
3838
tracing = { workspace = true }
3939
tracing-appender = "0.2.3"
40+
tracing-bunyan-formatter = { workspace = true }
4041
tracing-subscriber = { workspace = true, features = ["env-filter", "json"] }
41-
tracing-tree = "0.4.0"
42+
tracing-tree = { version = "0.4.0", features = ["time"] }
4243

4344
[target.'cfg(unix)'.dependencies]
4445
libc = "0.2.161"

crates/pgt_cli/src/cli_options.rs

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ pub struct CliOptions {
6161
pub reporter: CliReporter,
6262

6363
#[bpaf(
64+
env("PGT_LOG_LEVEL"),
6465
long("log-level"),
6566
argument("none|debug|info|warn|error"),
6667
fallback(LoggingLevel::default()),

crates/pgt_cli/src/commands/daemon.rs

+70-30
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ use tokio::runtime::Runtime;
1111
use tracing::subscriber::Interest;
1212
use tracing::{Instrument, Metadata, debug_span, metadata::LevelFilter};
1313
use tracing_appender::rolling::Rotation;
14+
use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer};
1415
use tracing_subscriber::{
15-
Layer,
1616
layer::{Context, Filter},
1717
prelude::*,
1818
registry,
1919
};
20-
use tracing_tree::HierarchicalLayer;
20+
use tracing_tree::{HierarchicalLayer, time::UtcDateTime};
2121

2222
pub(crate) fn start(
2323
session: CliSession,
@@ -78,8 +78,10 @@ pub(crate) fn run_server(
7878
config_path: Option<PathBuf>,
7979
log_path: Option<PathBuf>,
8080
log_file_name_prefix: Option<String>,
81+
log_level: Option<String>,
82+
log_kind: Option<String>,
8183
) -> Result<(), CliDiagnostic> {
82-
setup_tracing_subscriber(log_path, log_file_name_prefix);
84+
setup_tracing_subscriber(log_path, log_file_name_prefix, log_level, log_kind);
8385

8486
let rt = Runtime::new()?;
8587
let factory = ServerFactory::new(stop_on_disconnect);
@@ -181,28 +183,54 @@ async fn start_lsp_proxy(
181183
/// is written to log files rotated on a hourly basis (in
182184
/// `pgt-logs/server.log.yyyy-MM-dd-HH` files inside the system temporary
183185
/// directory)
184-
fn setup_tracing_subscriber(log_path: Option<PathBuf>, log_file_name_prefix: Option<String>) {
186+
fn setup_tracing_subscriber(
187+
log_path: Option<PathBuf>,
188+
log_file_name_prefix: Option<String>,
189+
log_level: Option<String>,
190+
log_kind: Option<String>,
191+
) {
185192
let pgt_log_path = log_path.unwrap_or(pgt_fs::ensure_cache_dir().join("pgt-logs"));
193+
186194
let appender_builder = tracing_appender::rolling::RollingFileAppender::builder();
195+
187196
let file_appender = appender_builder
188197
.filename_prefix(log_file_name_prefix.unwrap_or(String::from("server.log")))
189198
.max_log_files(7)
190199
.rotation(Rotation::HOURLY)
191200
.build(pgt_log_path)
192201
.expect("Failed to start the logger for the daemon.");
193202

194-
registry()
195-
.with(
196-
HierarchicalLayer::default()
197-
.with_indent_lines(true)
198-
.with_indent_amount(2)
199-
.with_bracketed_fields(true)
200-
.with_targets(true)
201-
.with_ansi(false)
202-
.with_writer(file_appender)
203-
.with_filter(LoggingFilter),
204-
)
205-
.init();
203+
let filter = PgtLoggingFilter::from(log_level);
204+
205+
let log_kind = log_kind.unwrap_or("hierarchical".into());
206+
207+
match log_kind.as_str() {
208+
"bunyan" => {
209+
registry()
210+
.with(JsonStorageLayer)
211+
.with(
212+
BunyanFormattingLayer::new("pgt_logs".into(), file_appender)
213+
.with_filter(filter),
214+
)
215+
.init();
216+
}
217+
218+
_ => registry()
219+
.with(
220+
HierarchicalLayer::default()
221+
.with_indent_lines(true)
222+
.with_indent_amount(2)
223+
.with_bracketed_fields(true)
224+
.with_targets(true)
225+
.with_ansi(false)
226+
.with_timer(UtcDateTime {
227+
higher_precision: false,
228+
})
229+
.with_writer(file_appender)
230+
.with_filter(filter),
231+
)
232+
.init(),
233+
}
206234
}
207235

208236
pub fn default_pgt_log_path() -> PathBuf {
@@ -212,22 +240,34 @@ pub fn default_pgt_log_path() -> PathBuf {
212240
}
213241
}
214242

215-
/// Tracing filter enabling:
216-
/// - All spans and events at level info or higher
217-
/// - All spans and events at level debug in crates whose name starts with `pgt`
218-
struct LoggingFilter;
243+
/// Tracing Filter with two rules:
244+
/// For all crates starting with pgt*, use `PGT_LOG_LEVEL` or CLI option or "info" as default
245+
/// For all other crates, use "info"
246+
struct PgtLoggingFilter(LevelFilter);
219247

220-
/// Tracing filter used for spans emitted by `pgt*` crates
221-
const SELF_FILTER: LevelFilter = if cfg!(debug_assertions) {
222-
LevelFilter::TRACE
223-
} else {
224-
LevelFilter::DEBUG
225-
};
248+
impl From<Option<String>> for PgtLoggingFilter {
249+
fn from(value: Option<String>) -> Self {
250+
Self(
251+
value
252+
.map(|lv_filter| match lv_filter.as_str() {
253+
"trace" => LevelFilter::TRACE,
254+
"debug" => LevelFilter::DEBUG,
255+
"info" => LevelFilter::INFO,
256+
"warn" => LevelFilter::WARN,
257+
"error" => LevelFilter::ERROR,
258+
"off" => LevelFilter::OFF,
259+
260+
_ => LevelFilter::INFO,
261+
})
262+
.unwrap_or(LevelFilter::INFO),
263+
)
264+
}
265+
}
226266

227-
impl LoggingFilter {
267+
impl PgtLoggingFilter {
228268
fn is_enabled(&self, meta: &Metadata<'_>) -> bool {
229269
let filter = if meta.target().starts_with("pgt") {
230-
SELF_FILTER
270+
self.0
231271
} else {
232272
LevelFilter::INFO
233273
};
@@ -236,7 +276,7 @@ impl LoggingFilter {
236276
}
237277
}
238278

239-
impl<S> Filter<S> for LoggingFilter {
279+
impl<S> Filter<S> for PgtLoggingFilter {
240280
fn enabled(&self, meta: &Metadata<'_>, _cx: &Context<'_, S>) -> bool {
241281
self.is_enabled(meta)
242282
}
@@ -250,6 +290,6 @@ impl<S> Filter<S> for LoggingFilter {
250290
}
251291

252292
fn max_level_hint(&self) -> Option<LevelFilter> {
253-
Some(SELF_FILTER)
293+
Some(self.0)
254294
}
255295
}

crates/pgt_cli/src/commands/mod.rs

+19
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ pub enum PgtCommand {
151151
display_fallback
152152
)]
153153
log_prefix_name: String,
154+
154155
/// Allows to change the folder where logs are stored.
155156
#[bpaf(
156157
env("PGT_LOG_PATH"),
@@ -161,6 +162,24 @@ pub enum PgtCommand {
161162
)]
162163
log_path: PathBuf,
163164

165+
/// Allows to change the log level. Default is debug. This will only affect "pgt*" crates. All others are logged with info level.
166+
#[bpaf(
167+
env("PGT_LOG_LEVEL"),
168+
long("log-level"),
169+
argument("trace|debug|info|warn|error|none"),
170+
fallback(String::from("debug"))
171+
)]
172+
log_level: String,
173+
174+
/// Allows to change the logging format kind. Default is hierarchical.
175+
#[bpaf(
176+
env("PGT_LOG_KIND"),
177+
long("log-kind"),
178+
argument("hierarchical|bunyan"),
179+
fallback(String::from("hierarchical"))
180+
)]
181+
log_kind: String,
182+
164183
#[bpaf(long("stop-on-disconnect"), hide_usage)]
165184
stop_on_disconnect: bool,
166185
/// Allows to set a custom file path to the configuration file,

crates/pgt_cli/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,15 @@ impl<'app> CliSession<'app> {
105105
config_path,
106106
log_path,
107107
log_prefix_name,
108+
log_level,
109+
log_kind,
108110
} => commands::daemon::run_server(
109111
stop_on_disconnect,
110112
config_path,
111113
Some(log_path),
112114
Some(log_prefix_name),
115+
Some(log_level),
116+
Some(log_kind),
113117
),
114118
PgtCommand::PrintSocket => commands::daemon::print_socket(),
115119
};

crates/pgt_cli/src/logging.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub fn setup_cli_subscriber(level: LoggingLevel, kind: LoggingKind) {
1111
if level == LoggingLevel::None {
1212
return;
1313
}
14+
1415
let format = tracing_subscriber::fmt::layer()
1516
.with_level(true)
1617
.with_target(false)

0 commit comments

Comments
 (0)