captains-log
A light-weight customizable logger implementation for rust
Features
-
Allow customize log format and time format. Refer to
LogFormat -
Supports multiple types of sink stacking, each with its own log level.
-
Builder::console(LogConsole): Console output to stdout/stderr. -
Builder::raw_file(LogRawFile): Support atomic appending from multi-process on linux
-
-
Log panic message by default.
-
Supports signal listening for log-rotate. Refer to
Builder::signal() -
Fine-grain module-level log control.
Provides
LogFilterto filter specified logs on-the-fly. -
API-level log handling.
Provides
LogFilterKVfor API logging with additional key.For example, you can set
req_idinLogFilterKV, and track the complete request handling procedure from log. -
For test suits usage:
Allow dynamic reconfigure logger setting in different test function.
(NOTE: currently signal_listener does not support reconfigure).
Provides an attribute macro #[logfn] to wrap test function. Logging test-start and test-end.
-
Provides a
parserto work on your log files.
Usage
Cargo.toml
[]
= { = "0.4", = ["std", "kv_unstable"] }
= "0.4"
lib.rs or main.rs:
#[macro_use]
extern crate captains_log;
#[macro_use]
extern crate log;
Production example
You can refer to various preset recipe in recipe module, including console & file output.
// #[macro_use]
// extern crate captains_log;
// #[macro_use]
// extern crate log;
use ;
use split_error_file_logger;
// You'll get /tmp/test.log with all logs, and /tmp/test.log.wf only with error logs.
let mut log_builder = split_error_file_logger;
// Builder::build() is equivalent of setup_log()
log_builder.build;
// non-error msg will only appear in /tmp/test.log
debug!;
info!;
// will appear in both /tmp/test.log and /tmp/test.log.wf
error!;
## Unit test example
To setup different log config on different tests.
call <font color=Blue> test </font> on ,
which enable dynamic log config and disable signal_hook.
```rust
use ;
use recipe;
Customize format example
extern crate signal_hook;
extern crate chrono;
use *;
let debug_format = new;
let debug_file = new;
let config = default
.signal
.raw_file;
config.build;
Fine-grain module-level log control
Place LogFilter in Arc and share among coroutines.
Log level can be changed on-the-fly.
use Arc;
use *;
set_max_level;
let logger_io = new;
let logger_req = new;
logger_io.set_level;
logger_req.set_level;
logger_debug!;
logger_debug!;
logger_error!;
API-level log handling
Request log can be track by custom key req_id, which kept in LogFilterKV.
use *;
let builder = raw_file_logger_custom;
builder.build.expect;
let logger = new;
logger_debug!;
logger_debug!;
logger_info!;
The log will be:
[2025-06-11 14:33:08.089090][DEBUG][request.rs:67] API service started
[2025-06-11 14:33:10.099092][DEBUG][request.rs:67] Req / received (000000000000007b)
[2025-06-11 14:33:10.099232][WARN][request.rs:68] header xxx (000000000000007b)
[2025-06-11 14:33:11.009092][DEBUG][request.rs:67] Req / 200 complete (000000000000007b)