captains_log/lib.rs
1//! # captains-log
2//!
3//! A light-weight logger for rust, implementation base on the crate `log`.
4//!
5//! ## Features
6//!
7//! * Allow customize log format and time format.
8//!
9//! * Supports signal listening for log-rotate.
10//!
11//! * Supports multiple log files, each with its own log level.
12//!
13//! * Supports hook on panic.
14//!
15//! * On default supports multi-process/thread/coroutine log into the same file.
16//! Atomic line appending can be done on Linux
17//!
18//! * Provides `LogFilter` for coroutine-based programs. You can set req_id in LogFilter and
19//! output to log files
20//!
21//! ## Dependency
22//!
23//! ``` toml
24//! [dependencies]
25//! log = { version = "0.4", features = ["std", "kv_unstable"] }
26//! captains_log = "0.1"
27//! ```
28//!
29//! ## Fast setup eample:
30//!
31//! ```rust
32//! /// #[macro_use]
33//! /// extern crate captains_log;
34//! /// #[macro_use]
35//! /// extern crate log;
36//! use log::{debug, info, error};
37//! use captains_log::*;
38//! use captains_log::recipe::split_error_file_logger;
39//!
40//! let log_builder = split_error_file_logger("/tmp", "test", log::Level::Debug);
41//! setup_log(log_builder);
42//!
43//! // non-error msg will only appear in /tmp/test.log
44//! debug!("Set a course to Sol system");
45//! info!("Engage");
46//!
47//! // will appear in both /tmp/test.log and /tmp/test.log.wf
48//! error!("Engine over heat!");
49//!
50//! ```
51//!
52//! ## Customize format example
53//!
54//! ``` rust
55//! extern crate signal_hook;
56//! extern crate chrono;
57//! use captains_log::*;
58
59//! fn format_f(r: FormatRecord) -> String {
60//! let time = r.time();
61//! let level = r.level();
62//! let file = r.file();
63//! let line = r.line();
64//! let msg = r.msg();
65//! format!("{time}|{level}|{file}:{line}|{msg}\n").to_string()
66//! }
67//! let debug_format = LogFormat::new(
68//! "%Y%m%d %H:%M:%S%.6f",
69//! format_f,
70//! );
71//! let debug_file = LogFile::new(
72//! "/tmp", "test.log", log::Level::Trace, debug_format);
73//! let config = Builder::default()
74//! .signal(signal_hook::consts::SIGINT)
75//! .file(debug_file);
76//!
77//! setup_log(config);
78//! ```
79
80
81
82extern crate log;
83extern crate captains_log_helper;
84extern crate signal_hook;
85
86#[macro_use]
87extern crate enum_dispatch;
88
89mod config;
90mod time;
91mod formatter;
92mod file_impl;
93mod log_impl;
94
95pub mod recipe;
96pub mod macros;
97
98mod log_filter;
99
100pub use log::{Level as LogLevel, LevelFilter as LogLevelFilter};
101pub use captains_log_helper::logfn;
102
103pub use self::{
104 config::{Builder, LogFile},
105 formatter::{LogFormat, FormatRecord},
106 log_filter::*,
107 log_impl::{setup_log},
108};
109
110#[cfg(test)]
111mod tests;