Skip to content

Commit 6e06b2e

Browse files
committed
refactor: [#884] move from log to tracing crate
1 parent 3ccc0e4 commit 6e06b2e

File tree

37 files changed

+112
-141
lines changed

37 files changed

+112
-141
lines changed

packages/located-error/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use std::error::Error;
3333
use std::panic::Location;
3434
use std::sync::Arc;
3535

36-
use log::debug;
36+
use tracing::debug;
3737

3838
pub type DynError = Arc<dyn std::error::Error + Send + Sync>;
3939

src/app.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
//! - Tracker REST API: the tracker API can be enabled/disabled.
2424
use std::sync::Arc;
2525

26-
use log::warn;
2726
use tokio::task::JoinHandle;
2827
use torrust_tracker_configuration::Configuration;
28+
use tracing::warn;
2929

3030
use crate::bootstrap::jobs::{health_check_api, http_tracker, torrent_cleanup, tracker_apis, udp_tracker};
3131
use crate::servers::registar::Registar;

src/bootstrap/jobs/health_check_api.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
//! Refer to the [configuration documentation](https://docs.rs/torrust-tracker-configuration)
1515
//! for the API configuration options.
1616
17-
use log::info;
1817
use tokio::sync::oneshot;
1918
use tokio::task::JoinHandle;
2019
use torrust_tracker_configuration::HealthCheckApi;
20+
use tracing::info;
2121

2222
use super::Started;
2323
use crate::servers::health_check_api::server;

src/bootstrap/jobs/http_tracker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ use std::net::SocketAddr;
1414
use std::sync::Arc;
1515

1616
use axum_server::tls_rustls::RustlsConfig;
17-
use log::info;
1817
use tokio::task::JoinHandle;
1918
use torrust_tracker_configuration::HttpTracker;
19+
use tracing::info;
2020

2121
use super::make_rust_tls;
2222
use crate::core;

src/bootstrap/jobs/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ use std::panic::Location;
8989
use std::sync::Arc;
9090

9191
use axum_server::tls_rustls::RustlsConfig;
92-
use log::info;
9392
use thiserror::Error;
9493
use torrust_tracker_configuration::TslConfig;
9594
use torrust_tracker_located_error::{DynError, LocatedError};
95+
use tracing::info;
9696

9797
/// Error returned by the Bootstrap Process.
9898
#[derive(Error, Debug)]

src/bootstrap/jobs/torrent_cleanup.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
use std::sync::Arc;
1414

1515
use chrono::Utc;
16-
use log::info;
1716
use tokio::task::JoinHandle;
1817
use torrust_tracker_configuration::v1::core::Core;
18+
use tracing::info;
1919

2020
use crate::core;
2121

src/bootstrap/jobs/tracker_apis.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ use std::net::SocketAddr;
2424
use std::sync::Arc;
2525

2626
use axum_server::tls_rustls::RustlsConfig;
27-
use log::info;
2827
use tokio::task::JoinHandle;
2928
use torrust_tracker_configuration::{AccessTokens, HttpApi};
29+
use tracing::info;
3030

3131
use super::make_rust_tls;
3232
use crate::core;

src/bootstrap/jobs/udp_tracker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
//! > for the configuration options.
99
use std::sync::Arc;
1010

11-
use log::debug;
1211
use tokio::task::JoinHandle;
1312
use torrust_tracker_configuration::UdpTracker;
13+
use tracing::debug;
1414

1515
use crate::core;
1616
use crate::servers::registar::ServiceRegistrationForm;

src/bootstrap/logging.rs

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,55 +12,72 @@
1212
//! Refer to the [configuration crate documentation](https://docs.rs/torrust-tracker-configuration) to know how to change log settings.
1313
use std::sync::Once;
1414

15-
use log::{info, LevelFilter};
1615
use torrust_tracker_configuration::{Configuration, LogLevel};
16+
use tracing::info;
17+
use tracing::level_filters::LevelFilter;
1718

1819
static INIT: Once = Once::new();
1920

2021
/// It redirects the log info to the standard output with the log level defined in the configuration
2122
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);
2324

24-
if level == log::LevelFilter::Off {
25+
if tracing_level == LevelFilter::OFF {
2526
return;
2627
}
2728

2829
INIT.call_once(|| {
29-
stdout_config(level);
30+
tracing_stdout_init(tracing_level, &TraceStyle::Default);
3031
});
3132
}
3233

3334
fn config_level_or_default(log_level: &Option<LogLevel>) -> LevelFilter {
3435
match log_level {
35-
None => log::LevelFilter::Info,
36+
None => LevelFilter::INFO,
3637
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,
4344
},
4445
}
4546
}
4647

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+
};
6457

6558
info!("logging initialized.");
6659
}
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+
}

src/console/ci/e2e/docker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::process::{Command, Output};
44
use std::thread::sleep;
55
use std::time::{Duration, Instant};
66

7-
use log::{debug, info};
7+
use tracing::{debug, info};
88

99
/// Docker command wrapper.
1010
pub struct Docker {}

0 commit comments

Comments
 (0)