Hi,
Please help with the following problem. I have been struggling with it:
For my actix-web web server application, I am logging using tracing and rolling file appender crates:
It logs only for the first time when the subscriber was initialised before the web server.
When I call tracing::debug! in my middleware, nothing gets logged. These were println! before.
Relevant content of .env:
...
RUST_LOG=debug
Relevant content of Cargo.toml:
[dependencies]
dotenv = "0.15.0"
time = {version = "0.3", default-features = false, features = ["formatting", "macros", "serde", "parsing", "local-offset"]}
...
actix-web = {version = "4.4.0", features = ["openssl"]}
tracing = "0.1"
tracing-appender = "0.2"
tracing-subscriber = {version = "0.3", features = ["fmt", "std", "local-time", "time"]}
Content of src/helper/app_logger.rs ( WORK IN PROGRESS ):
use time::macros::format_description;
use tracing::Level;
use tracing_appender::{non_blocking::WorkerGuard, rolling::{self, RollingFileAppender, Rotation}};
use tracing_subscriber::fmt::writer::MakeWriterExt;
use tracing_subscriber::{
fmt::{time::LocalTime, Layer},
layer::SubscriberExt,
};
pub fn init_app_logger1() -> (WorkerGuard, WorkerGuard) {
let info_appender = RollingFileAppender::builder()
.rotation(Rotation::DAILY) // rotate log files once every hour
.filename_prefix("info") // log file names will be prefixed with `myapp.`
.filename_suffix("log") // log file names will be suffixed with `.log`
.build("./log") // try to build an appender that stores log files in `/var/log`
.expect("initializing rolling file appender failed");
let (info_appender, info_guard) = tracing_appender::non_blocking(info_appender);
let warn_appender = rolling::daily("./log", "warn");
let (warn_appender, warn_guard) = tracing_appender::non_blocking(warn_appender);
let timer1 = LocalTime::new(format_description!("[year][month][day]-[hour]:[minute]:[second]"));
let timer2 = LocalTime::new(format_description!("[year][month][day]-[hour]:[minute]:[second]"));
let subscriber = tracing_subscriber::registry()
.with(
Layer::new()
.with_timer(timer1)
.with_ansi(false)
.with_writer(info_appender.with_min_level(Level::INFO).with_max_level(Level::DEBUG))
)
.with(
Layer::new()
.with_timer(timer2)
.with_ansi(false)
.with_writer(warn_appender.with_max_level(Level::WARN))
);
tracing::subscriber::set_global_default(subscriber)
.expect("Unable to set a global subscriber");
(info_guard, warn_guard)
}
The skeleton of src/main.rs:
async fn main() -> Result<(), std::io::Error> {
dotenv().ok();
...
let _guards = helper::app_logger::init_app_logger1();
tracing::warn!("tracing warn lib.rs");
tracing::debug!("tracing debug lib.rs");
let server = HttpServer::new(move || {
...
})
.listen_openssl(listener, ssl_builder())?
.run();
Ok(server)
}
When run, regardless of how many times I am accessing the routes. Nothing get logged. The content of the log files stays fixed, they are always:
Content of log/info.2024-03-08.log:
20240308-23:59:41 DEBUG learn_actix_web: tracing debug lib.rs
20240308-23:59:41 DEBUG sqlx::query: summary="SET sql_mode=(SELECT CONCAT(@@sql_mode, ',PIPES_AS_CONCAT,NO_ENGINE_SUBSTITUTION')),time_zone='+00:00',NAMES …" db.statement="\n\nSET\n sql_mode =(\n SELECT\n CONCAT(\n @ @sql_mode,\n ',PIPES_AS_CONCAT,NO_ENGINE_SUBSTITUTION'\n )\n ),\n time_zone = '+00:00',\n NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;\n" rows_affected=0 rows_returned=0 elapsed=1.565ms
20240308-23:59:41 INFO actix_server::builder: starting 8 workers
Content of log/warn.2024-03-08:
20240308-23:59:41 WARN learn_actix_web: tracing warn lib.rs
So the two initial logs:
tracing::warn!("tracing warn lib.rs");
tracing::debug!("tracing debug lib.rs");
Work as expected. But repeated calls to tracing::debug! in my middleware module does not do anything.
I understand that, we should keep the WorkerGuard, _guards for the life of the application.
let _guards = helper::app_logger::init_app_logger1();
Does _guards gets out of scope and dropped in this case, please?
If so, how do we keep it alive, please?
Or have I got the code completely wrong? Could you please point to some relevant documentation, please?
Thank you and best regards,
...behai.