|
12 | 12 | //! Refer to the [configuration crate documentation](https://docs.rs/torrust-tracker-configuration) to know how to change log settings. |
13 | 13 | use std::sync::Once; |
14 | 14 |
|
15 | | -use log::{info, LevelFilter}; |
16 | 15 | use torrust_tracker_configuration::{Configuration, LogLevel}; |
| 16 | +use tracing::info; |
| 17 | +use tracing::level_filters::LevelFilter; |
17 | 18 |
|
18 | 19 | static INIT: Once = Once::new(); |
19 | 20 |
|
20 | 21 | /// It redirects the log info to the standard output with the log level defined in the configuration |
21 | 22 | pub fn setup(cfg: &Configuration) { |
22 | | - let level = config_level_or_default(&cfg.core.log_level); |
| 23 | + let tracing_level = config_level_or_default(&cfg.core.log_level); |
23 | 24 |
|
24 | | - if level == log::LevelFilter::Off { |
| 25 | + if tracing_level == LevelFilter::OFF { |
25 | 26 | return; |
26 | 27 | } |
27 | 28 |
|
28 | 29 | INIT.call_once(|| { |
29 | | - stdout_config(level); |
| 30 | + tracing_stdout_init(tracing_level, &TraceStyle::Default); |
30 | 31 | }); |
31 | 32 | } |
32 | 33 |
|
33 | 34 | fn config_level_or_default(log_level: &Option<LogLevel>) -> LevelFilter { |
34 | 35 | match log_level { |
35 | | - None => log::LevelFilter::Info, |
| 36 | + None => LevelFilter::INFO, |
36 | 37 | Some(level) => match level { |
37 | | - LogLevel::Off => LevelFilter::Off, |
38 | | - LogLevel::Error => LevelFilter::Error, |
39 | | - LogLevel::Warn => LevelFilter::Warn, |
40 | | - LogLevel::Info => LevelFilter::Info, |
41 | | - LogLevel::Debug => LevelFilter::Debug, |
42 | | - LogLevel::Trace => LevelFilter::Trace, |
| 38 | + LogLevel::Off => LevelFilter::OFF, |
| 39 | + LogLevel::Error => LevelFilter::ERROR, |
| 40 | + LogLevel::Warn => LevelFilter::WARN, |
| 41 | + LogLevel::Info => LevelFilter::INFO, |
| 42 | + LogLevel::Debug => LevelFilter::DEBUG, |
| 43 | + LogLevel::Trace => LevelFilter::TRACE, |
43 | 44 | }, |
44 | 45 | } |
45 | 46 | } |
46 | 47 |
|
47 | | -fn stdout_config(level: LevelFilter) { |
48 | | - if let Err(_err) = fern::Dispatch::new() |
49 | | - .format(|out, message, record| { |
50 | | - out.finish(format_args!( |
51 | | - "{} [{}][{}] {}", |
52 | | - chrono::Local::now().format("%+"), |
53 | | - record.target(), |
54 | | - record.level(), |
55 | | - message |
56 | | - )); |
57 | | - }) |
58 | | - .level(level) |
59 | | - .chain(std::io::stdout()) |
60 | | - .apply() |
61 | | - { |
62 | | - panic!("Failed to initialize logging.") |
63 | | - } |
| 48 | +fn tracing_stdout_init(filter: LevelFilter, style: &TraceStyle) { |
| 49 | + let builder = tracing_subscriber::fmt().with_max_level(filter); |
| 50 | + |
| 51 | + let () = match style { |
| 52 | + TraceStyle::Default => builder.init(), |
| 53 | + TraceStyle::Pretty(display_filename) => builder.pretty().with_file(*display_filename).init(), |
| 54 | + TraceStyle::Compact => builder.compact().init(), |
| 55 | + TraceStyle::Json => builder.json().init(), |
| 56 | + }; |
64 | 57 |
|
65 | 58 | info!("logging initialized."); |
66 | 59 | } |
| 60 | + |
| 61 | +#[derive(Debug)] |
| 62 | +pub enum TraceStyle { |
| 63 | + Default, |
| 64 | + Pretty(bool), |
| 65 | + Compact, |
| 66 | + Json, |
| 67 | +} |
| 68 | + |
| 69 | +impl std::fmt::Display for TraceStyle { |
| 70 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 71 | + let style = match self { |
| 72 | + TraceStyle::Default => "Default Style", |
| 73 | + TraceStyle::Pretty(path) => match path { |
| 74 | + true => "Pretty Style with File Paths", |
| 75 | + false => "Pretty Style without File Paths", |
| 76 | + }, |
| 77 | + TraceStyle::Compact => "Compact Style", |
| 78 | + TraceStyle::Json => "Json Format", |
| 79 | + }; |
| 80 | + |
| 81 | + f.write_str(style) |
| 82 | + } |
| 83 | +} |
0 commit comments