Coder Social home page Coder Social logo

Accept default log level about env_logger HOT 9 CLOSED

rust-cli avatar rust-cli commented on June 2, 2024 17
Accept default log level

from env_logger.

Comments (9)

mleonhard avatar mleonhard commented on June 2, 2024 18

Thanks for implementing this feature. Here's how I'm using it:

env_logger::init_from_env(
    env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"));

A complete example:

// main.rs
pub mod mylib {
    pub fn error() { log::error!("mylib"); }

    pub fn warn() { log::warn!("mylib"); }

    pub fn info() { log::info!("mylib"); }

    pub fn debug() { log::debug!("mylib"); }

    pub fn trace() { log::trace!("mylib"); }
}

fn main() {
    // https://rust-lang-nursery.github.io/rust-cookbook/development_tools/debugging/config_log.html
    // https://github.com/sebasmagri/env_logger/releases/tag/v0.5.8
    env_logger::init_from_env(
        env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"));
    log::error!("main");
    log::warn!("main");
    log::info!("main");
    log::debug!("main");
    log::trace!("main");
    mylib::error();
    mylib::warn();
    mylib::info();
    mylib::debug();
    mylib::trace();

    // $ cargo run
    // [2020-04-01T20:25:25Z ERROR example] main
    // [2020-04-01T20:25:25Z WARN  example] main
    // [2020-04-01T20:25:25Z INFO  example] main
    // [2020-04-01T20:25:25Z ERROR example::mylib] mylib
    // [2020-04-01T20:25:25Z WARN  example::mylib] mylib
    // [2020-04-01T20:25:25Z INFO  example::mylib] mylib

    // You can change the default log level:
    // $ RUST_LOG=debug cargo run
    // [2020-04-01T20:25:39Z ERROR example] main
    // [2020-04-01T20:25:39Z WARN  example] main
    // [2020-04-01T20:25:39Z INFO  example] main
    // [2020-04-01T20:25:39Z DEBUG example] main
    // [2020-04-01T20:25:39Z ERROR example::mylib] mylib
    // [2020-04-01T20:25:39Z WARN  example::mylib] mylib
    // [2020-04-01T20:25:39Z INFO  example::mylib] mylib
    // [2020-04-01T20:25:39Z DEBUG example::mylib] mylib

    // You can set the default log level and module-specific log levels:
    // $ RUST_LOG="info,example::mylib=debug" cargo run
    // [2020-04-01T20:25:55Z ERROR example] main
    // [2020-04-01T20:25:55Z WARN  example] main
    // [2020-04-01T20:25:55Z INFO  example] main
    // [2020-04-01T20:25:55Z ERROR example::mylib] mylib
    // [2020-04-01T20:25:55Z WARN  example::mylib] mylib
    // [2020-04-01T20:25:55Z INFO  example::mylib] mylib
    // [2020-04-01T20:25:55Z DEBUG example::mylib] mylib

    // Be sure to include the default level.  Without it, only the specified
    // modules can log.
    // $ RUST_LOG="example::mylib=error" cargo run
    // [2020-04-01T20:26:11Z ERROR example::mylib] mylib
}
# Cargo.toml
[package]
name = "example"
version = "0.1.0"
[dependencies]
env_logger = "~0.7.1"
log = "~0.4.8"

from env_logger.

KodrAus avatar KodrAus commented on June 2, 2024 4

We now have support for specifying default environment variables in 0.5.8. There are some more details in the release notes there.

I'd be happy to revisit our complete builder story in a future breaking release.

from env_logger.

frol avatar frol commented on June 2, 2024 1

This is not as short as env_logger::init(), but still short enough:

env_logger::Builder::from_default_env().parse("info").init();

from env_logger.

tailhook avatar tailhook commented on June 2, 2024

I do this in every other application. Usually I use env::set_var, but it looks ugly.

Mostly my use case is setting log level to warn or info (instead of error which is default) so I'm not sure that parsing string is most useful thing here, but it would work for me.

from env_logger.

KodrAus avatar KodrAus commented on June 2, 2024

I think it would be good to have a nice way to default the filter without having to dig into std::env yourself. In #46 I've introduced an Env type, because we now have multiple environment variables controlling the logger (one for the filter, and one for color output). That still needs some more design work, but I think that could impact how we approach this. Maybe after that's done we should revisit this and come up with a nice way to support it that'll scale with other new features?

from env_logger.

KodrAus avatar KodrAus commented on June 2, 2024

My thinking for this issue is that we could use the Env type to do something like this strawman API:

env_logger::init_from_env(Env::default().default_filter_value("info"));

That is, we store an optional or_else value alongside each environment variable that's returned if the environment variable isn't present. We can bikeshed the actual API, it would be nice to specify a default without having to specify the environment variable, and keep the methods chainable so Envs can be ergonomically built up and passed to from_env methods.

What do you think?

from env_logger.

tailhook avatar tailhook commented on June 2, 2024

Looks okay. I might also prefer a type-safe variant:

env_logger::init_from_env(Env::default().default_level(Info));

from env_logger.

kpcyrd avatar kpcyrd commented on June 2, 2024

Would work for me, but seems somewhat verbose compared to the nice and easy env_logger::init();

My usecase is specifically that I set the log level for parts of my program, so I don't clutter stdout with info! lines from a library I use internally:

env_logger::init_from_env(Env::default().default_filter_value("something=info"));

from env_logger.

KodrAus avatar KodrAus commented on June 2, 2024

@frol unfortunately that will clobber any value read from the environment with info. I've got a PR open now that should close this one off.

from env_logger.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.