|
2 | 2 |  |
3 | 3 |  |
4 | 4 |
|
5 | | -A simple logger implementation for lamba-rs crates. Inspired by |
6 | | -python's [logging](https://docs.python.org/3/library/logging.html) module. |
| 5 | +Simple, lightweight logging for lambda-rs crates. Inspired by Python’s |
| 6 | +[logging](https://docs.python.org/3/library/logging.html) module. |
7 | 7 |
|
8 | 8 |
|
9 | | -# Installation |
10 | | -First, add the following to your `Cargo.toml`: |
| 9 | +## Installation |
| 10 | +Add to your `Cargo.toml`: |
11 | 11 | ```toml |
12 | 12 | [dependencies] |
| 13 | +# Option A: use the crate name in code as `lambda_rs_logging` |
13 | 14 | lambda-rs-logging = "2023.1.30" |
| 15 | + |
| 16 | +# Option B: rename dependency so you can write `use logging;` |
| 17 | +# logging = { package = "lambda-rs-logging", version = "2023.1.30" } |
14 | 18 | ``` |
15 | 19 |
|
16 | | -or run this command from your project directory: |
| 20 | +Or from your project directory: |
17 | 21 | ```bash |
18 | 22 | cargo add lambda-rs-logging |
19 | 23 | ``` |
20 | 24 |
|
21 | | -# Getting started |
22 | | -## Using the global logger |
| 25 | +Then in code, either import with the default name: |
| 26 | +```rust |
| 27 | +use lambda_rs_logging as logging; |
| 28 | +``` |
| 29 | +or, if you used the rename in Cargo.toml (Option B), simply: |
| 30 | +```rust |
| 31 | +use logging; // renamed in Cargo.toml |
| 32 | +``` |
| 33 | + |
| 34 | +## Getting Started |
| 35 | +### Global logger via macros |
23 | 36 | ```rust |
24 | | -use logging; |
| 37 | +use lambda_rs_logging as logging; |
25 | 38 |
|
26 | 39 | fn main() { |
27 | | - logging::trace!("Hello world"); |
28 | | - logging::debug!("Hello world"); |
29 | | - logging::info!("Hello world"); |
30 | | - logging::warn!("Hello world"); |
31 | | - logging::error!("Hello world"); |
32 | | - logging::fatal!("Hello world"); |
| 40 | + logging::trace!("trace {}", 1); |
| 41 | + logging::debug!("debug {}", 2); |
| 42 | + logging::info!("info {}", 3); |
| 43 | + logging::warn!("warn {}", 4); |
| 44 | + logging::error!("error {}", 5); |
| 45 | + logging::fatal!("fatal {}", 6); // note: does not exit |
33 | 46 | } |
34 | 47 | ``` |
35 | 48 |
|
36 | | -## Using an instance of the logger |
| 49 | +### Custom logger instance |
37 | 50 | ```rust |
38 | | -use logging::Logger; |
| 51 | +use lambda_rs_logging as logging; |
39 | 52 |
|
40 | 53 | fn main() { |
41 | | - let logger = Logger::new("my-logger"); |
42 | | - logger.trace("Hello world"); |
43 | | - logger.debug("Hello world"); |
44 | | - logger.info("Hello world"); |
45 | | - logger.warn("Hello world"); |
46 | | - logger.error("Hello world"); |
47 | | - logger.fatal("Hello world"); |
| 54 | + let logger = logging::Logger::builder() |
| 55 | + .name("my-app") |
| 56 | + .level(logging::LogLevel::INFO) |
| 57 | + .with_handler(Box::new(logging::handler::ConsoleHandler::new("my-app"))) |
| 58 | + .build(); |
| 59 | + |
| 60 | + logger.info("Hello world".to_string()); |
| 61 | + logger.warn("Be careful".to_string()); |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +### Initialize a custom global |
| 66 | +```rust |
| 67 | +use lambda_rs_logging as logging; |
| 68 | + |
| 69 | +fn main() { |
| 70 | + let logger = logging::Logger::builder() |
| 71 | + .name("app") |
| 72 | + .level(logging::LogLevel::DEBUG) |
| 73 | + .with_handler(Box::new(logging::handler::ConsoleHandler::new("app"))) |
| 74 | + .build(); |
| 75 | + |
| 76 | + // Set the global logger before any macros are used |
| 77 | + logging::Logger::init(logger).expect("global logger can only be initialized once"); |
| 78 | + |
| 79 | + logging::debug!("from global"); |
48 | 80 | } |
49 | 81 | ``` |
| 82 | + |
| 83 | +### Configure level from environment |
| 84 | +```rust |
| 85 | +use lambda_rs_logging as logging; |
| 86 | + |
| 87 | +fn main() { |
| 88 | + // LAMBDA_LOG can be: trace|debug|info|warn|error|fatal |
| 89 | + // Example: export LAMBDA_LOG=debug |
| 90 | + logging::env::init_global_from_env().ok(); |
| 91 | + |
| 92 | + logging::info!("respects env filter"); |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +## Notes |
| 97 | +- Thread-safe global with `OnceLock<Arc<Logger>>`. |
| 98 | +- Handlers are `Send + Sync` and receive a `Record` internally (phase 1 refactor). |
| 99 | +- `fatal!` logs at FATAL level but does not exit the process. Prefer explicit exits in your app logic. |
| 100 | +- Console handler colors only when attached to a TTY and writes WARN+ to stderr. |
| 101 | + |
| 102 | +## Examples |
| 103 | +This crate ships with examples. From the repository root: |
| 104 | +```bash |
| 105 | +cargo run -p lambda-rs-logging --example 01_global_macros |
| 106 | +cargo run -p lambda-rs-logging --example 02_custom_logger |
| 107 | +cargo run -p lambda-rs-logging --example 03_global_init |
| 108 | +``` |
| 109 | + |
| 110 | +### Environment example |
| 111 | +```bash |
| 112 | +LAMBDA_LOG=debug cargo run -p lambda-rs-logging --example 01_global_macros |
| 113 | +``` |
0 commit comments