Skip to content

Commit 163c7ed

Browse files
committed
[update] default log level
1 parent 8191149 commit 163c7ed

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

crates/lambda-rs-logging/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ use logging; // renamed in Cargo.toml
3737
use lambda_rs_logging as logging;
3838

3939
fn main() {
40+
// Default global level:
41+
// - Debug builds: DEBUG
42+
// - Release builds: INFO
43+
//
44+
// Override at runtime with LAMBDA_LOG=trace|debug|info|warn|error|fatal
4045
logging::trace!("trace {}", 1);
4146
logging::debug!("debug {}", 2);
4247
logging::info!("info {}", 3);

crates/lambda-rs-logging/src/lib.rs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
#![allow(clippy::needless_return)]
22
//! A simple logging library for lambda-rs crates.
3+
//!
4+
//! # Default global level
5+
//! - Debug builds (`cfg(debug_assertions)`): `DEBUG`
6+
//! - Release builds (`cfg(not(debug_assertions))`): `INFO`
7+
//!
8+
//! The global level can be overridden at runtime with `LAMBDA_LOG`
9+
//! (`trace|debug|info|warn|error|fatal`). For best results, call
10+
//! `lambda_rs_logging::env::init_global_from_env()` early in startup before
11+
//! any logging macros are used.
312
413
use std::{
514
fmt,
@@ -61,6 +70,20 @@ pub struct Logger {
6170
handlers: RwLock<Vec<Box<dyn handler::Handler>>>,
6271
}
6372

73+
static GLOBAL_LOGGER: OnceLock<Arc<Logger>> = OnceLock::new();
74+
75+
fn default_global_level() -> LogLevel {
76+
#[cfg(debug_assertions)]
77+
{
78+
return LogLevel::DEBUG;
79+
}
80+
81+
#[cfg(not(debug_assertions))]
82+
{
83+
return LogLevel::INFO;
84+
}
85+
}
86+
6487
impl Logger {
6588
/// Creates a new logger with the given log level and name.
6689
pub fn new(level: LogLevel, name: &str) -> Self {
@@ -79,9 +102,8 @@ impl Logger {
79102
/// Returns the global logger (thread-safe). Initializes with a default
80103
/// console handler if not explicitly initialized via `init`.
81104
pub fn global() -> &'static Arc<Self> {
82-
static GLOBAL: OnceLock<Arc<Logger>> = OnceLock::new();
83-
GLOBAL.get_or_init(|| {
84-
let logger = Logger::new(LogLevel::TRACE, "lambda-rs");
105+
GLOBAL_LOGGER.get_or_init(|| {
106+
let logger = Logger::new(default_global_level(), "lambda-rs");
85107
// Default console handler
86108
logger.add_handler(Box::new(handler::ConsoleHandler::new("lambda-rs")));
87109
Arc::new(logger)
@@ -90,8 +112,7 @@ impl Logger {
90112

91113
/// Initialize the global logger (first caller wins).
92114
pub fn init(logger: Logger) -> Result<(), InitError> {
93-
static GLOBAL: OnceLock<Arc<Logger>> = OnceLock::new();
94-
GLOBAL
115+
GLOBAL_LOGGER
95116
.set(Arc::new(logger))
96117
.map_err(|_| InitError::AlreadyInitialized)
97118
}
@@ -261,11 +282,20 @@ pub mod env {
261282
pub fn init_global_from_env() -> Result<(), super::InitError> {
262283
let logger = Logger::builder()
263284
.name("lambda-rs")
264-
.level(LogLevel::INFO)
285+
.level(super::default_global_level())
265286
.with_handler(Box::new(crate::handler::ConsoleHandler::new("lambda-rs")))
266287
.build();
267288
apply_env_level(&logger, Some("LAMBDA_LOG"));
268-
super::Logger::init(logger)
289+
match super::Logger::init(logger) {
290+
Ok(()) => {
291+
return Ok(());
292+
}
293+
Err(super::InitError::AlreadyInitialized) => {
294+
let global = super::Logger::global().clone();
295+
apply_env_level(global.as_ref(), Some("LAMBDA_LOG"));
296+
return Ok(());
297+
}
298+
}
269299
}
270300
}
271301
/// Returns whether the global logger would log at `level`.

0 commit comments

Comments
 (0)