Log4rs : Filter crates logs

Hi everybody,

I just put in place 'log' and 'log4rs' crates on my project.
But when i run the app, I get more than my project logs.

Configuration :

fn init_logger(logs_dirpath: PathBuf, logs_level: &str) {
    let date = chrono::Utc::now();

    let mut logs_filepath = logs_dirpath.clone();
    logs_filepath.push(format!(
        "arius-adbridge-api-{}.log",
        date.format("%Y-%m-%d")
    ));

    let stdout = ConsoleAppender::builder().build();

    let logfile = FileAppender::builder()
        .encoder(Box::new(PatternEncoder::default()))
        .build(logs_filepath)
        .unwrap();

    let level_filter = match logs_level {
        "OFF" => LevelFilter::Off,
        "TRACE" => LevelFilter::Trace,
        "DEBUG" => LevelFilter::Debug,
        "INFO" => LevelFilter::Info,
        "WARN" => LevelFilter::Warn,
        "ERROR" => LevelFilter::Error,
        _ => LevelFilter::Info,
    };

    let config = Config::builder()
        .appender(Appender::builder().build("stdout", Box::new(stdout)))
        .appender(Appender::builder().build("logsfile", Box::new(logfile)))
        .logger(
            Logger::builder()
                .appender("logsfile")
                .additive(false)
                .build("arius_adbridge_api", level_filter),
        )
        .build(Root::builder().appender("logsfile").build(level_filter))
        .unwrap();

    log4rs::init_config(config).unwrap();
}

Result :

2022-02-02T22:59:07.798984700+01:00 INFO arius_adbridge_api - Launched !
2022-02-02T22:59:07.801101700+01:00 INFO launch - 🔧 Configured for development.
2022-02-02T22:59:07.801146700+01:00 INFO launch_ - address: e[1;49;39mlocalhoste[0m
2022-02-02T22:59:07.801182700+01:00 INFO launch_ - port: e[1;49;39m8000e[0m
2022-02-02T22:59:07.801214900+01:00 INFO launch_ - log: e[1;49;39mnormale[0m
2022-02-02T22:59:07.801246200+01:00 INFO launch_ - workers: e[1;49;39m16e[0m
2022-02-02T22:59:07.801277300+01:00 INFO launch_ - secret key: e[1;49;39mgeneratede[0m
2022-02-02T22:59:07.801309400+01:00 INFO launch_ - limits: e[1;49;39mforms = 32KiBe[0m
2022-02-02T22:59:07.801347600+01:00 INFO launch_ - keep-alive: e[1;49;39m5se[0m
2022-02-02T22:59:07.803324+01:00 INFO launch_ - read timeout: e[1;49;39m5se[0m
2022-02-02T22:59:07.803373800+01:00 INFO launch_ - write timeout: e[1;49;39m5se[0m
2022-02-02T22:59:07.803403+01:00 INFO launch_ - tls: e[1;49;39mdisablede[0m
2022-02-02T22:59:07.803879900+01:00 INFO rocket::rocket - 🛰  e[35mMountinge[0m e[34m/e[0me[35m:e[0m
2022-02-02T22:59:07.804013100+01:00 INFO _ - e[32mGETe[0m e[34m/adse[0m e[36m(e[0me[35madse[0me[36m)e[0m
2022-02-02T22:59:07.810227600+01:00 INFO launch - 🚀 e[1;49;39mRocket has launched frome[0m e[1;4;49;39mhttp://e[0me[1;4;49;39mlocalhost:8000e[0m
2022-02-02T22:59:07.810290400+01:00 DEBUG hyper::server - threads = 16
2022-02-02T23:08:24.556758100+01:00 DEBUG hyper::server - Incoming stream
2022-02-02T23:08:24.558987900+01:00 TRACE hyper::buffer - get_buf []
2022-02-02T23:08:24.559252100+01:00 TRACE hyper::buffer - read_into_buf buf[0..4096]
2022-02-02T23:08:24.559868900+01:00 DEBUG hyper::server - Incoming stream
2022-02-02T23:08:24.560332800+01:00 TRACE hyper::buffer - get_buf []
2022-02-02T23:08:24.560394500+01:00 TRACE hyper::buffer - get_buf []
2022-02-02T23:08:24.560446200+01:00 TRACE hyper::buffer - read_into_buf buf[0..4096]
2022-02-02T23:08:24.560504400+01:00 DEBUG hyper::server - ioerror in keepalive loop = Custom { kind: UnexpectedEof, error: "end of stream before headers finished" }
2022-02-02T23:08:24.560599+01:00 DEBUG hyper::server - keep_alive loop ending for [::1]:56015
2022-02-02T23:08:24.560972100+01:00 TRACE hyper::buffer - get_buf [u8; 4096][0..204]
2022-02-02T23:08:24.561025300+01:00 TRACE hyper::http::h1 - try_parse([71, 69, 84, 32, 47, 97, 100, 115, 32, 72, 84, 84, 80, 47, 49, 46, 49, 13, 10, 85, 115, 101, 114, 45, 65, 103, 101, 110, 116, 58, 32, 80, 111, 115, 116, 109, 97, 110, 82, 117, 110, 116, 105, 109, 101, 47, 55, 46, 50, 57, 46, 48, 13, 10, 65, 99, 99, 101, 112, 116, 58, 32, 42, 47, 42, 13, 10, 80, 111, 115, 116, 109, 97, 110, 45, 84, 111, 107, 101, 110, 58, 32, 97, 101, 53, 57, 54, 101, 102, 54, 45, 100, 56, 97, 101, 45, 52, 56, 101, 51, 45, 56, 52, 99, 49, 45, 48, 48, 101, 55, 50, 97, 48, 48, 48, 55, 53, 48, 13, 10, 72, 111, 115, 116, 58, 32, 108, 111, 99, 97, 108, 104, 111, 115, 116, 58, 56, 48, 48, 48, 13, 10, 65, 99, 99, 101, 112, 116, 45, 69, 110, 99, 111, 100, 105, 110, 103, 58, 32, 103, 122, 105, 112, 44, 32, 100, 101, 102, 108, 97, 116, 101, 44, 32, 98, 114, 13, 10, 67, 111, 110, 110, 101, 99, 116, 105, 111, 110, 58, 32, 107, 101, 101, 112, 45, 97, 108, 105, 118, 101, 13, 10, 13, 10])
2022-02-02T23:08:24.561101200+01:00 TRACE hyper::http::h1 - Request.try_parse([Header; 100], [u8; 204])
2022-02-02T23:08:24.561154900+01:00 TRACE hyper::http::h1 - Request.try_parse Complete(204)
2022-02-02T23:08:24.561213900+01:00 TRACE hyper::header - raw header: "User-Agent"=[80, 111, 115, 116, 109, 97, 110, 82, 117, 110, 116, 105, 109, 101, 47, 55, 46, 50, 57, 46, 48]
2022-02-02T23:08:24.561291700+01:00 TRACE hyper::header - raw header: "Accept"=[42, 47, 42]
2022-02-02T23:08:24.561322200+01:00 TRACE hyper::header - raw header: "Postman-Token"=[97, 101, 53, 57, 54, 101, 102, 54, 45, 100, 56, 97, 101, 45, 52, 56, 101, 51, 45, 56, 52, 99, 49, 45, 48, 48, 101, 55, 50, 97, 48, 48, 48, 55, 53, 48]
2022-02-02T23:08:24.561353300+01:00 TRACE hyper::header - raw header: "Host"=[108, 111, 99, 97, 108, 104, 111, 115, 116, 58, 56, 48, 48, 48]
2022-02-02T23:08:24.561435600+01:00 TRACE hyper::header - raw header: "Accept-Encoding"=[103, 122, 105, 112, 44, 32, 100, 101, 102, 108, 97, 116, 101, 44, 32, 98, 114]
2022-02-02T23:08:24.561525900+01:00 TRACE hyper::header - raw header: "Connection"=[107, 101, 101, 112, 45, 97, 108, 105, 118, 101]
2022-02-02T23:08:24.561568100+01:00 DEBUG hyper::server::request - Request Line: Get AbsolutePath("/ads") Http11
2022-02-02T23:08:24.561601+01:00 DEBUG hyper::server::request - Headers { User-Agent: PostmanRuntime/7.29.0
, Accept: */*
, Postman-Token: ae596ef6-d8ae-48e3-84c1-00e72a000750
, Host: localhost:8000
, Accept-Encoding: gzip, deflate, br
, Connection: keep-alive
, }
2022-02-02T23:08:24.561692+01:00 TRACE hyper::http - should_keep_alive( Http11, Some(Connection([KeepAlive])) )
2022-02-02T23:08:24.562148100+01:00 TRACE _ - Data::new(EmptyReader)
2022-02-02T23:08:24.562186900+01:00 TRACE _ - Filled peek buf with 0 bytes.
2022-02-02T23:08:24.562208800+01:00 TRACE _ - Peek bytes: 0/512 bytes.
2022-02-02T23:08:24.562241300+01:00 INFO rocket::rocket - e[32mGETe[0m e[34m/adse[0m:
2022-02-02T23:08:24.562433900+01:00 TRACE _ - Routing the request: e[32mGETe[0m e[34m/adse[0m
2022-02-02T23:08:24.562476700+01:00 TRACE _ - All matches: [Route { name: Some("ads"), method: Get, base: Origin { source: None, path: "/", query: None, segment_count: [uninitialized storage] }, uri: Origin { source: None, path: "/ads", query: None, segment_count: [uninitialized storage] }, rank: -4, format: None, metadata: Metadata { path_segments: [RouteSegment { string: "ads", kind: Static, name: "ads", index: Some(0), _part: PhantomData }], query_segments: None, fully_dynamic_query: true } }]
2022-02-02T23:08:24.562520100+01:00 INFO _ - Matched: e[32mGETe[0m e[34m/adse[0m e[36m(e[0me[35madse[0me[36m)e[0m
2022-02-02T23:08:24.562641+01:00 INFO _ - e[1;49;39mOutcome:e[0m e[49;32mSuccesse[0m
2022-02-02T23:08:24.562710800+01:00 TRACE hyper::header - Headers.append_raw( "Content-Type", [97, 112, 112, 108, 105, 99, 97, 116, 105, 111, 110, 47, 106, 115, 111, 110] )
2022-02-02T23:08:24.562752800+01:00 TRACE hyper::header - Headers.append_raw( "Server", [82, 111, 99, 107, 101, 116] )
2022-02-02T23:08:24.562789600+01:00 TRACE hyper::header - Headers.set( "Content-Length", ContentLength(2) )
2022-02-02T23:08:24.562826100+01:00 DEBUG hyper::server::response - writing head: Http11 Ok
2022-02-02T23:08:24.562873200+01:00 TRACE hyper::header - Headers.set( "Date", Date(HttpDate(Tm { tm_sec: 24, tm_min: 8, tm_hour: 22, tm_mday: 2, tm_mon: 1, tm_year: 122, tm_wday: 3, tm_yday: 32, tm_isdst: 0, tm_utcoff: 0, tm_nsec: 562554100 })) )
2022-02-02T23:08:24.562930700+01:00 DEBUG hyper::server::response - headers [
Headers { Content-Type: application/json
, Server: Rocket
, Content-Length: 2
, Date: Wed, 02 Feb 2022 22:08:24 GMT
, }]
2022-02-02T23:08:24.563011700+01:00 DEBUG hyper::server::response - write 2 bytes
2022-02-02T23:08:24.563039500+01:00 TRACE hyper::server::response - ending
2022-02-02T23:08:24.563122900+01:00 INFO _ - e[32mResponse succeeded.e[0m
2022-02-02T23:08:24.563182700+01:00 TRACE hyper::http - should_keep_alive( Http11, None )
2022-02-02T23:08:24.563209+01:00 DEBUG hyper::server - keep_alive = true for [::1]:56016
2022-02-02T23:08:24.563237100+01:00 TRACE hyper::buffer - get_buf []
2022-02-02T23:08:24.563258100+01:00 TRACE hyper::buffer - reserved 0
2022-02-02T23:08:24.563277300+01:00 TRACE hyper::buffer - read_into_buf buf[0..0]
2022-02-02T23:08:24.563295700+01:00 TRACE hyper::buffer - read_into_buf at full capacity
2022-02-02T23:08:24.563313900+01:00 TRACE hyper::buffer - get_buf []
2022-02-02T23:08:24.563335300+01:00 DEBUG hyper::server - ioerror in keepalive loop = Custom { kind: UnexpectedEof, error: "end of stream before headers finished" }
2022-02-02T23:08:24.563359600+01:00 DEBUG hyper::server - keep_alive loop ending for [::1]:56016

I don't want 'hyper::server' logs, and just 'Rocket' INFO (maybe in stdout and not in the file ?)

Is it possible please ?

Can we get JNDI support? :wink:

Thanks kinggoesgaming for your answer,

I've just look about JNDI, and remembder what is it.
And found there is no equivalent in Rust.

So I suppose I can't do what I had explain in this topic... :persevere:
I juste have to put INFO/WARN level and that's all, doesn't it ?

And that's good, in fact, since the post above was not about the problem in topic, but about recently uncovered CVE in log4j.

Why so? Filtering is not connected with remote code execution in any way.

Just configure your loggers:

  • one - for your own code, with name equal to your crate name, logging to appender("logsfile") with the level you want;
  • one - for Rocket, named rocket; logging to appender("stdout") with level INFO;
  • and the root logger, which logs nothing.
1 Like

Hi Cerber-Ursi and thanks for your answer,

It put me on a track, and there is what I want to do :

let config = Config::builder()
        .appender(Appender::builder().build("stdout", Box::new(stdout)))
        .appender(Appender::builder().build("app-logs", Box::new(logfile)))
        .logger(
            Logger::builder()
                .appender("app-logs")
                .additive(true)
                .build("arius_adbridge_api", level_filter),
        )
        .build(
            Root::builder()
                .appender("app-logs")
                .build(LevelFilter::Info),
        )
        .unwrap();

I log my app my the level, and I keep crates logs in INFO level.
That's better !

Thank you again :smiley:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.