4个版本 (2个稳定版)
1.1.0 | 2023年3月31日 |
---|---|
1.0.0 | 2021年12月5日 |
1.0.0-rc.2 | 2021年11月26日 |
#286 in 调试
218 每月下载量
32KB
451 行
ecs-logger
兼容Elastic Common Schema (ECS) 日志记录的Rust记录器。
功能
- 可以通过RUST_LOG环境变量进行配置。
- 底层使用env_logger。
- 默认情况下,除了错误级别,所有日志都被禁用。
- 默认将日志写入stderr。
安装
将以下内容添加到你的Cargo.toml
文件中
[dependencies]
log = "0.4"
ecs-logger = "1"
文档
示例
以下示例假设二进制文件为./example
。
基本日志记录
use log::{debug, error};
ecs_logger::init();
debug!(
"this is a debug {}, which is NOT printed by default",
"message"
);
error!("this is printed by default");
$ ./example
{"@timestamp":"2021-11-26T15:25:22.321002600Z","log.level":"ERROR","message":"this is printed by default","ecs.version":"1.12.1","log.origin":{"file":{"line":13,"name":"example.rs"},"rust":{"target":"example::tests","module_path":"example::tests","file_path":"tests/example.rs"}}}
$ RUST_LOG=debug ./example
{"@timestamp":"2021-11-26T15:26:13.524069Z","log.level":"DEBUG","message":"this is a debug message, which is NOT printed by default","ecs.version":"1.12.1","log.origin":{"file":{"line":9,"name":"example.rs"},"rust":{"target":"example::tests","module_path":"example::tests","file_path":"tests/example.rs"}}}
{"@timestamp":"2021-11-26T15:26:13.524193100Z","log.level":"ERROR","message":"this is printed by default","ecs.version":"1.12.1","log.origin":{"file":{"line":13,"name":"example.rs"},"rust":{"target":"example::tests","module_path":"example::tests","file_path":"tests/example.rs"}}}
更多过滤配置示例可在env_logger的文档中找到。
额外字段
您可以使用extra_fields
模块向日志输出添加额外字段。
use ecs_logger::extra_fields;
use serde::Serialize;
#[derive(Serialize)]
struct MyExtraFields {
my_field: String,
}
ecs_logger::init();
extra_fields::set_extra_fields(MyExtraFields {
my_field: "my_value".to_string(),
}).unwrap();
log::error!("Hello {}!", "world");
log::info!("Goodbye {}!", "world");
extra_fields::clear_extra_fields();
自定义日志记录
对于以下示例,您需要将env_logger
添加到您的Cargo.toml
文件中。
[dependencies]
log = "0.4"
env_logger = "0.9"
ecs-logger = "1"
写入stdout
use log::info;
// Initialize custom logger
env_logger::builder()
.format(ecs_logger::format) // Configure ECS logger
.target(env_logger::Target::Stdout) // Write to stdout
.init();
info!("Hello {}!", "world");
配置日志过滤器
use log::info;
// Initialize custom logger
env_logger::builder()
.parse_filters("info,my_app=debug") // Set filters
.format(ecs_logger::format) // Configure ECS logger
.init();
info!("Hello {}!", "world");
默认日志字段
{
"@timestamp": "2021-11-26T15:25:22.321002600Z",
"log.level": "ERROR",
"message": "this is printed by default",
"ecs.version": "1.12.1",
"log.origin": {
"file": {
"line": 13,
"name": "example.rs"
},
"rust": {
"target": "example::tests",
"module_path": "example::tests",
"file_path": "tests/example.rs"
}
}
}
许可证
许可方式为以下之一
- Apache许可证,版本2.0 (LICENSE-APACHE 或 http://www.apache.org/licenses/LICENSE-2.0)
- MIT许可证 (LICENSE-MIT 或 http://opensource.org/licenses/MIT)
任选其一。
贡献
除非您明确说明,否则根据Apache-2.0许可证定义的您有意提交以包含在作品中的任何贡献,应如上双许可,不附加任何额外条款或条件。
致谢
本软件的实现基于env_logger,它也采用双许可。
依赖项
~2.4–3.5MB
~67K SLoC