Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/lambda-rs-logging/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ license = "MIT"
[lib]
name = "logging"
path = "src/lib.rs"

[dependencies]
once_cell = "1"
32 changes: 15 additions & 17 deletions crates/lambda-rs-logging/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! A simple logging library for lambda-rs crates.

use std::fmt::Debug;
use std::{fmt::Debug, sync::{Mutex, MutexGuard}};

use once_cell::sync::Lazy;

/// A trait for handling log messages.
pub mod handler;
Expand Down Expand Up @@ -33,21 +35,10 @@ impl Logger {
}
}

/// Returns the global logger.
pub fn global() -> &'static mut Self {
// TODO(vmarcella): Fix the instantiation for the global logger.
unsafe {
if LOGGER.is_none() {
LOGGER = Some(Logger {
level: LogLevel::TRACE,
name: "lambda-rs".to_string(),
handlers: vec![Box::new(handler::ConsoleHandler::new("lambda-rs"))],
});
}
};
return unsafe { &mut LOGGER }
.as_mut()
.expect("Logger not initialized");
/// Returns a handle to the global logger. The logger is lazily
/// initialized on first access.
pub fn global() -> MutexGuard<'static, Self> {
LOGGER.lock().expect("Logger mutex poisoned")
}

/// Adds a handler to the logger. Handlers are called in the order they
Expand Down Expand Up @@ -126,7 +117,14 @@ impl Logger {
}
}

pub(crate) static mut LOGGER: Option<Logger> = None;
/// Global logger instance used by the logging macros.
static LOGGER: Lazy<Mutex<Logger>> = Lazy::new(|| {
Mutex::new(Logger {
level: LogLevel::TRACE,
name: "lambda-rs".to_string(),
handlers: vec![Box::new(handler::ConsoleHandler::new("lambda-rs"))],
})
});

/// Trace logging macro using the global logger instance.
#[macro_export]
Expand Down
Loading