25个版本
新 0.2.7 | 2024年8月24日 |
---|---|
0.2.6 | 2024年6月12日 |
0.1.16 | 2024年6月6日 |
0.1.11 | 2024年5月30日 |
#20 in #log-messages
1,957 monthly downloads
38KB
751 lines
Session_log
包
This crate tries to provide a easy to use logging system that can group / nest logs in a session like manner. This is useful for systems that having multiple threads and you want to group logs by session.
使用方法
对于大多数基本使用,你只需要从 session_log::prelude
导入所有内容,然后就可以开始了。
use session_log::prelude::*;
fn main() {
// Create a new logging entry point with default settings
let logger = Logger::new("main");
// Log a message directly to the logger
logger.info("Hello, world!");
// Create a new session
{
let session = logger.session();
// Log some messages in the session
session.info("Hello, session!");
// Create a new session in the session
{
let session = session.session();
// Log some messages in the session
session.info("Hello, nested session!");
}
// Log some messages in the session
session.info("Goodbye, session!");
}
}
创建日志记录器的成本非常低,本质上它只是一个围绕 String
的全局映射表中的键的包装器。
use session_log::prelude::*;
fn main() {
// Create a new logging entry point with default settings
let logger1 = Logger::new("main");
logger1.set_level(Level::Verbose);
// Create a new logger with the same name
let logger2 = Logger::new("main");
// They are the same logger
assert_eq!(
logger1.get_level(),
logger2.get_level());
}
每个入口点都有自己的日志级别,输出目录可以在初始化或运行时设置。
use session_log::{
prelude::*,
Level,
};
fn main() {
{ // Create a new logger with custom settings
let logger1 = Logger::with_options("1",
Level::Verbose, "logs/1").unwrap();
// If you try to use `with_options` to create a logger with the same name
// but different settings, it will return an error
assert_eq!(
Logger::with_options("1",
Level::Info, "logs/1").is_err(),
true);
// Same options will not return an error
assert_eq!(
Logger::with_options("1",
Level::Verbose, "logs/1").is_err(),
false);
}
{ // Create a new logger with custom settings that may be omitted
// If "2" is never been created before, it will use the given options
let logger2 = Logger::try_with_options("2",
Level::Verbose, "logs/2");
// If "2" is already created, it will omit the given options
let logger2 = Logger::try_with_options("2",
Level::Verbose, "logs/2");
}
{ // Do the setting after initialization
// This will create a new logger with default settings or retrieve the existing one
// regardless of the settings. Default setting: (Level::Info, "logs")
let logger2 = Logger::new("3");
// Configure the logger afterward
logger2.set_level(Level::Verbose);
logger2.set_directory("logs/3");
}
}
依赖
~1.5MB
~22K SLoC