Actix-web tracing-subscriber does not write to log file from other modules

Hi @jofas,

I have finally worked it out! It was a simple overlook which I should have realised after finished writing the post asking for help.

In my original post, src/main.rs is actually src/lib.rs, and

async fn main() -> Result<(), std::io::Error>

is actually

pub async fn run(listener: TcpListener) -> Result<Server, std::io::Error>

And the skeleton code remains as is:

The full src/main.rs is:

use std::net::TcpListener;
use learn_actix_web::run;

#[actix_web::main]
async fn main() -> Result<(), std::io::Error> {
    let listener = TcpListener::bind("0.0.0.0:5000").expect("Failed to bind port 5000");
    // We retrieve the port assigned to us by the OS
    let port = listener.local_addr().unwrap().port();
    println!("Server is listening on port {}", port);

    let server = run(listener).await.unwrap();
    server.await
}

Cargo.toml has the following declaration also:

...
[lib]
path = "src/lib.rs"

[[bin]]
path = "src/main.rs"
name = "learn_actix_web"
...

I thought I could reduce sample code to make the post a little simpler. I completely forgot this module.

Move let _guards = helper::app_logger::init_app_logger1(); to src/main.rs and it works as expected:

...
use learn_actix_web::helper::app_logger::init_app_logger1;

#[actix_web::main]
async fn main() -> Result<(), std::io::Error> {
    let _guards = init_app_logger1();

    let listener = TcpListener::bind("0.0.0.0:5000").expect("Failed to bind port 5000");
...	

This would explain it. run gets out of scope, while main is not, therefore _guards is still around.

Thank you for your helps and best regards,

...behai.

1 Like