Comments (9)
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.
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.
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.
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.
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.
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 Env
s can be ergonomically built up and passed to from_env
methods.
What do you think?
from env_logger.
Looks okay. I might also prefer a type-safe variant:
env_logger::init_from_env(Env::default().default_level(Info));
from env_logger.
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.
@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)
- Obsolete copyright info HOT 2
- Don't panic on broken pipes when built without termcolor HOT 6
- error!(...) exit error
- Space left after level HOT 7
- No color on Windows when using builder HOT 6
- Need to flush log output or disable buffering HOT 3
- Dynamically change log level after `init` HOT 5
- Regex version out of date, perhaps run a cargo upgrade? HOT 5
- `rust-toolchain` prevents building on architecture supported after 1.41 HOT 2
- Why no simple file logging example? HOT 1
- Always exclude certain dependencies from logging HOT 2
- Please push 0.9.1 tag HOT 1
- Hiding module path but displaying target HOT 3
- compile error in `BufferWriter::pipe` HOT 3
- atty advisory issued HOT 4
- Format builder's state as RUST_LOG-like value
- How to forcibly color output even when the output is not a terminal? HOT 2
- Priority issues between environment variable `RUST_LOG` and function call `filter_level` / `filter`. HOT 1
- Dependency Dashboard
- Is this library thread-safe? HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from env_logger.