Skip to content

Commit

Permalink
Use event filter (#925)
Browse files Browse the repository at this point in the history
* Use LevelFilter instead of Level

Originally a `Level` was parsed from one of two environment variables (or
defaulted) and then converted into a `LevelFilter` before initializing the
subscriber.

However, this precludes using `RUST_LOG=off` since `Level` does not
recognize that as valid, resulting in `Level::INFO` (the default) being used.

`LevelFilter` (to which the above is converted anyway) _does_ allow the value to
be `off` - so it seems a little more flexible (and very very minutely faster) to
parse the env var or default value directly into a `LevelFilter`.

* rustfmt

---------

Co-authored-by: Martin Bartlett <jbartle9@dxc.com>
  • Loading branch information
bassmanitram and Martin Bartlett authored Sep 16, 2024
1 parent 27191d0 commit 20206d7
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lambda-runtime-api-client/src/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ const DEFAULT_LOG_LEVEL: &str = "INFO";
pub fn init_default_subscriber() {
let log_format = env::var("AWS_LAMBDA_LOG_FORMAT").unwrap_or_default();
let log_level_str = env::var("AWS_LAMBDA_LOG_LEVEL").or_else(|_| env::var("RUST_LOG"));
let log_level = Level::from_str(log_level_str.as_deref().unwrap_or(DEFAULT_LOG_LEVEL)).unwrap_or(Level::INFO);
let log_level =
LevelFilter::from_str(log_level_str.as_deref().unwrap_or(DEFAULT_LOG_LEVEL)).unwrap_or(LevelFilter::INFO);

let collector = tracing_subscriber::fmt()
.with_target(false)
.without_time()
.with_env_filter(
EnvFilter::builder()
.with_default_directive(LevelFilter::from_level(log_level).into())
.with_default_directive(log_level.into())
.from_env_lossy(),
);

Expand Down

0 comments on commit 20206d7

Please sign in to comment.