#logging #log-level #log #logger #elasticsearch #log-error #env-var

ecs-logger

兼容Elastic Common Schema (ECS) 日志记录的记录器

4个版本 (2个稳定版)

1.1.0 2023年3月31日
1.0.0 2021年12月5日
1.0.0-rc.22021年11月26日

#286 in 调试

Download history 201/week @ 2024-03-13 163/week @ 2024-03-20 225/week @ 2024-03-27 168/week @ 2024-04-03 118/week @ 2024-04-10 123/week @ 2024-04-17 214/week @ 2024-04-24 184/week @ 2024-05-01 87/week @ 2024-05-08 51/week @ 2024-05-15 27/week @ 2024-05-22 29/week @ 2024-05-29 36/week @ 2024-06-05 69/week @ 2024-06-12 67/week @ 2024-06-19 40/week @ 2024-06-26

218 每月下载量

MIT/Apache

32KB
451

ecs-logger

CI status crate version docs online MIT or Apache 2.0 Licenses

兼容Elastic Common Schema (ECS) 日志记录的Rust记录器。

功能

  • 可以通过RUST_LOG环境变量进行配置。
    • 底层使用env_logger
    • 默认情况下,除了错误级别,所有日志都被禁用。
  • 默认将日志写入stderr。

安装

将以下内容添加到你的Cargo.toml文件中

[dependencies]
log = "0.4"
ecs-logger = "1"

文档

在docs.rs上可用

示例

以下示例假设二进制文件为./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许可证定义的您有意提交以包含在作品中的任何贡献,应如上双许可,不附加任何额外条款或条件。

致谢

本软件的实现基于env_logger,它也采用双许可。

依赖项

~2.4–3.5MB
~67K SLoC