File appender writes gibberish #1502
-
I just discovered let subscriber = Subscriber::builder()
.with_env_filter(EnvFilter::from_default_env())
.finish();
tracing::subscriber::set_global_default(subscriber)?;
tracing_log::LogTracer::builder()
.with_max_level(tracing_core::LevelFilter::current().as_log())
.init()?; Here is an example of Aug 08 17:44:52.451 WARN pfd: Starting up
Aug 08 17:44:52.451 WARN pfd: data_dir=/home/igor/.pfd
Aug 08 17:44:52.451 WARN pfd: args=["target/debug/pfd", "serve"]
Aug 08 17:44:52.458 INFO pfd::db: Migrating db schema current_version=3 target_version=Latest
Aug 08 17:44:52.459 INFO pfd::db: Loaded migrations count=3
Aug 08 17:44:52.459 INFO pfd::db: Schema is up to date I'm happy with default log format and the only extra thing I need is being able to save the same output to a file. After reading the docs and examples, I came up with a following solution: let log_file_appender = tracing_appender::rolling::never(data_dir, "pfd.log");
let (log_file_appender, _log_file_guard) = tracing_appender::non_blocking(log_file_appender);
let subscriber = Registry::default()
.with(EnvFilter::from_default_env())
.with(Layer::new().with_writer(std::io::stdout))
.with(Layer::new().with_writer(log_file_appender));
tracing::subscriber::set_global_default(subscriber)?;
tracing_log::LogTracer::builder()
.with_max_level(tracing_core::LevelFilter::current().as_log())
.init()?; Full source: https://github.com/bubelov/pfd/blob/master/src/main.rs It didn't affect ^[[2mAug 08 17:50:29.628^[[0m ^[[33m WARN^[[0m pfd: Starting up
^[[2mAug 08 17:50:29.628^[[0m ^[[33m WARN^[[0m pfd: data_dir=/home/igor/.pfd
^[[2mAug 08 17:50:29.628^[[0m ^[[33m WARN^[[0m pfd: args=["target/debug/pfd", "serve"]
^[[2mAug 08 17:50:29.635^[[0m ^[[32m INFO^[[0m pfd::db: Migrating db schema current_version=3 target_version=Latest
^[[2mAug 08 17:50:29.635^[[0m ^[[32m INFO^[[0m pfd::db: Loaded migrations count=3
^[[2mAug 08 17:50:29.635^[[0m ^[[32m INFO^[[0m pfd::db: Schema is up to date Any ideas on what might be wrong here? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Ah, the gibberish are ANSI color codes. You'll want to disable colors to Layer writing to a file by passing let log_file_appender = tracing_appender::rolling::never(data_dir, "pfd.log");
let (log_file_appender, _log_file_guard) = tracing_appender::non_blocking(log_file_appender);
let subscriber = Registry::default()
.with(EnvFilter::from_default_env())
.with(Layer::new().with_writer(std::io::stdout))
.with(Layer::new().with_ansi(false).with_writer(log_file_appender));
tracing::subscriber::set_global_default(subscriber)?;
tracing_log::LogTracer::builder()
.with_max_level(tracing_core::LevelFilter::current().as_log())
.init()?; |
Beta Was this translation helpful? Give feedback.
Ah, the gibberish are ANSI color codes. You'll want to disable colors to Layer writing to a file by passing
false
totracing_subscriber::fmt::Layer::with_ansi
. Here's an example that should work: