Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Record does not contain file location and module for log messages from isahc crate, but works with env_logger #262

Open
e00E opened this issue May 25, 2020 · 2 comments
Labels
slog-scope crate: slog-scope slog-syslog crate: slog-syslog

Comments

@e00E
Copy link

e00E commented May 25, 2020

When using isahc https://crates.io/crates/isahc log records are missing location information when logging them through slog. The information exists when logging through env_logger https://crates.io/crates/env_logger .

Cargo.toml:

[package]
name = "temp"
version = "0.1.0"
edition = "2018"

[dependencies]
env_logger = "0.7.1"
isahc = "0.9.3"
log = "0.4.8"
slog = "2.5.2"
slog-scope = "4.3.0"
slog-stdlog = "4.0.0"

main.rs:

pub struct CustomFormatter;
impl slog::Drain for CustomFormatter {
    type Ok = ();
    type Err = ();
    fn log(
        &self,
        record: &slog::Record,
        _values: &slog::OwnedKVList,
    ) -> std::result::Result<Self::Ok, Self::Err> {
        println!(
            "{} {} {} {}: {}",
            record.level(),
            record.module(),
            record.file(),
            record.line(),
            record.msg()
        );
        Ok(())
    }
}

fn env_logger_format(
    formatter: &mut env_logger::fmt::Formatter,
    record: &log::Record,
) -> std::io::Result<()> {
    use std::io::Write as _;
    writeln!(
        formatter,
        "{} {} {} {}: {}",
        record.level(),
        record.module_path().unwrap(),
        record.file().unwrap(),
        record.line().unwrap(),
        record.args()
    )
    .unwrap();
    Ok(())
}

fn setup_env_logger() {
    env_logger::Builder::new()
        .filter_level(log::LevelFilter::Trace)
        .format(env_logger_format)
        .init();
}

fn setup_slog_logger() -> slog_scope::GlobalLoggerGuard {
    use slog::Drain as _;
    let logger = slog::Logger::root(CustomFormatter {}.fuse(), slog::o!());
    let guard = slog_scope::set_global_logger(logger);
    slog_stdlog::init().unwrap();
    guard
}

fn main() {
    // setup_env_logger();
    let _guard = setup_slog_logger();
    log::info!("start");
    isahc::HttpClient::new().unwrap();
    log::info!("end");
}

cargo run:

INFO temp src/main.rs 32: start
INFO <unknown> <unknown> 505: new
INFO <unknown> <unknown> 505: -> new
INFO <unknown> <unknown> 372: build
...

Change which logger is enabled:

    setup_env_logger();
    // let _guard = setup_slog_logger();

cargo run:

INFO temp src/main.rs 58: start
INFO isahc::client /home/u/.cargo/registry/src/github.com-1ecc6299db9ec823/isahc-0.9.3/src/client.rs 505: new
INFO isahc::client /home/u/.cargo/registry/src/github.com-1ecc6299db9ec823/isahc-0.9.3/src/client.rs 505: -> new
INFO isahc::client /home/u/.cargo/registry/src/github.com-1ecc6299db9ec823/isahc-0.9.3/src/client.rs 372: build
...

I am not sure which crate is at fault but slog is most likely as it works with env_logger.

@dpc
Copy link
Collaborator

dpc commented May 26, 2020

slog/src/lib.rs

Line 2425 in 4868ddb

pub struct RecordLocation {

Historically log was using &'static str for module names. So slog used it to. Then log crate changed it to &'str, but changing it in slog would create a big split in the ecosystem.

It is possible to no one got to it yet, and log did (or is planing to?) add structured logging support etc. so I'm not sure if it is worth the trouble.

Maybe we could just do string interning (+ clone + box + leak) to turn non-&'static strings into &'static ones? I mean - how many module strings can a program have ...

I think that would work well. PRs welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
slog-scope crate: slog-scope slog-syslog crate: slog-syslog
Projects
None yet
Development

No branches or pull requests

3 participants