-
Notifications
You must be signed in to change notification settings - Fork 10
Tracing logs #432
Tracing logs #432
Changes from 22 commits
a5117db
43bf8fe
d485555
ed5ab9d
5796d5c
961b6be
a205b2e
b83dd8e
bba832b
3d11185
5a5c134
d6b0f0d
9a585f1
e4873a6
884a5a2
c83dde3
87445a6
2fa6c84
6bb0bf0
ae9aed1
8e73396
5d3113f
0e1bcc3
f79f7f3
7a9e257
6810ed9
dde324d
636bdc5
d2d2fa8
0ba8f9c
46022c9
2e374f0
bbbb8a5
6d94af3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,28 @@ | ||
| // Copyright 2022-2023 Protocol Labs | ||
| // SPDX-License-Identifier: Apache-2.0, MIT | ||
|
|
||
| use tracing_subscriber::FmtSubscriber; | ||
| use anyhow::{anyhow, Context}; | ||
| use std::path::PathBuf; | ||
| use std::str::FromStr; | ||
| use std::sync::Arc; | ||
| use trace4rs::config::{AppenderId, Policy, Target}; | ||
| use trace4rs::{ | ||
| config::{self, Config, Format}, | ||
| Handle, | ||
| }; | ||
|
|
||
| pub use fendermint_app_options as options; | ||
| pub use fendermint_app_settings as settings; | ||
| use fendermint_app_settings::expand_tilde; | ||
|
|
||
| mod cmd; | ||
|
|
||
| #[tokio::main] | ||
| async fn main() { | ||
| let opts = options::parse(); | ||
|
|
||
| // Log events to stdout. | ||
| if let Some(level) = opts.tracing_level() { | ||
| // Writing to stderr so if we have output like JSON then we can pipe it to something else. | ||
| let subscriber = FmtSubscriber::builder() | ||
| .with_max_level(level) | ||
| .with_writer(std::io::stderr) | ||
| .finish(); | ||
|
|
||
| tracing::subscriber::set_global_default(subscriber) | ||
| .expect("setting default subscriber failed"); | ||
| create_log(level, opts.log_dir.as_ref()).expect("cannot create logging"); | ||
| } | ||
|
|
||
| if let Err(e) = cmd::exec(&opts).await { | ||
|
|
@@ -30,6 +31,88 @@ async fn main() { | |
| } | ||
| } | ||
|
|
||
| fn create_log(level: tracing::Level, log_dir: Option<&PathBuf>) -> anyhow::Result<()> { | ||
| let console_appender = config::Appender::Console; | ||
|
|
||
| // default logging output info log to console | ||
| let default = config::Logger { | ||
| level: config::LevelFilter::from_str(level.as_str())?, | ||
| appenders: literally::hset! { AppenderId::from("console") }, | ||
| format: Format::default(), | ||
| }; | ||
|
|
||
| let mut config = Config { | ||
| default, | ||
| loggers: literally::hmap! {}, | ||
| appenders: literally::hmap! {AppenderId::from("console") => console_appender}, | ||
| }; | ||
|
|
||
| if let Some(log_dir) = log_dir { | ||
| let log_folder = expand_tilde(log_dir) | ||
| .to_str() | ||
| .ok_or_else(|| anyhow!("cannot parse log folder"))? | ||
| .to_string(); | ||
| std::fs::create_dir_all(&log_folder).context("cannot create log folder")?; | ||
|
|
||
| let topdown_appender = config::Appender::RollingFile { | ||
| path: format!("{log_folder}/topdown.log"), | ||
| policy: Policy { | ||
| maximum_file_size: "10mb".to_string(), | ||
| // we keep the last 5 log files, older files will be deleted | ||
| max_size_roll_backups: 5, | ||
| pattern: None, | ||
| }, | ||
| }; | ||
| let debug_appender = config::Appender::RollingFile { | ||
| path: format!("{log_folder}/debug.log"), | ||
| policy: Policy { | ||
| maximum_file_size: "10mb".to_string(), | ||
| // we keep the last 5 log files, older files will be deleted | ||
| max_size_roll_backups: 5, | ||
| pattern: None, | ||
| }, | ||
| }; | ||
|
|
||
| let topdown_logger = config::Logger { | ||
| level: config::LevelFilter::DEBUG, | ||
| appenders: literally::hset! { AppenderId::from("topdown") }, | ||
| format: Format::default(), | ||
| }; | ||
| let debug_logger = config::Logger { | ||
| level: config::LevelFilter::DEBUG, | ||
| appenders: literally::hset! { AppenderId::from("debug") }, | ||
| format: Format::default(), | ||
| }; | ||
|
|
||
| config.loggers.insert( | ||
| Target::from("fendermint_vm_topdown"), | ||
| topdown_logger.clone(), | ||
| ); | ||
| config.loggers.insert( | ||
| Target::from("fendermint_vm_interpreter::chain"), | ||
| topdown_logger, | ||
| ); | ||
| config | ||
| .appenders | ||
| .insert(AppenderId::from("topdown"), topdown_appender); | ||
|
Comment on lines
+95
to
+97
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need to insert the logger into the config, when it's already set in the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I havent looked very deep into |
||
|
|
||
| config | ||
| .loggers | ||
| // "" matches everything | ||
aakoshh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .insert(Target::from(""), debug_logger); | ||
| config | ||
| .appenders | ||
| .insert(AppenderId::from("debug"), debug_appender); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah so why don't we insert a clone of the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, but it seems to me this is a bug or sth, we should not be able to do that though. But I added this.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's a bug? Why shouldn't we be able to set a logger that captures everything and logs to the console if we want to?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean we need to do this: config
.loggers
.insert(Target::from("fendermint"), debug_logger);
config.loggers.insert(Target::from("fendermint"), default);but ideally we should not need
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, okay, so if |
||
| } | ||
|
|
||
| let handle = Arc::new(Handle::try_from(config).unwrap()); | ||
cryptoAtwill marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| tracing::subscriber::set_global_default(handle.subscriber()) | ||
| .context("setting default subscriber failed")?; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use cid::Cid; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you know if this is available on Github somewhere? I can't find a link at https://crates.io/crates/trace4rs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/imperva/trace4rs