Skip to content

Commit cfdf6f5

Browse files
authored
Support RUST_LOG as variable to set the logging level. (#869)
Improve the documentation about log levels and formats. Signed-off-by: David Calavera <[email protected]>
1 parent 94f7e10 commit cfdf6f5

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

lambda-runtime-api-client/src/tracing.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,25 @@ pub use tracing::*;
1515
/// Re-export the `tracing-subscriber` crate to build your own subscribers.
1616
pub use tracing_subscriber as subscriber;
1717

18-
/// Initialize `tracing-subscriber` with default options.
19-
/// The subscriber uses `AWS_LAMBDA_LOG_LEVEL` as the environment variable to determine the log level for your function.
20-
/// It also uses [Lambda's advance logging controls](https://aws.amazon.com/blogs/compute/introducing-advanced-logging-controls-for-aws-lambda-functions/)
18+
const DEFAULT_LOG_LEVEL: &str = "INFO";
19+
20+
/// Initialize `tracing-subscriber` with default logging options.
21+
///
22+
/// This function uses environment variables set with [Lambda's advance logging controls](https://aws.amazon.com/blogs/compute/introducing-advanced-logging-controls-for-aws-lambda-functions/)
2123
/// if they're configured for your function.
22-
/// By default, the log level to emit events is `INFO`.
24+
///
25+
/// This subscriber sets the logging level based on environment variables:
26+
/// - if `AWS_LAMBDA_LOG_LEVEL` is set, it takes predecence over any other environment variables.
27+
/// - if `AWS_LAMBDA_LOG_LEVEL` is not set, check if `RUST_LOG` is set.
28+
/// - if none of those two variables are set, use `INFO` as the logging level.
29+
///
30+
/// The logging format can also be changed based on Lambda's advanced logging controls.
31+
/// If the `AWS_LAMBDA_LOG_FORMAT` environment variable is set to `JSON`, the log lines will be formatted as json objects,
32+
/// otherwise they will be formatted with the default tracing format.
2333
pub fn init_default_subscriber() {
2434
let log_format = env::var("AWS_LAMBDA_LOG_FORMAT").unwrap_or_default();
25-
let log_level = Level::from_str(&env::var("AWS_LAMBDA_LOG_LEVEL").unwrap_or_default()).unwrap_or(Level::INFO);
35+
let log_level_str = env::var("AWS_LAMBDA_LOG_LEVEL").or_else(|_| env::var("RUST_LOG"));
36+
let log_level = Level::from_str(log_level_str.as_deref().unwrap_or(DEFAULT_LOG_LEVEL)).unwrap_or(Level::INFO);
2637

2738
let collector = tracing_subscriber::fmt()
2839
.with_target(false)

0 commit comments

Comments
 (0)