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

ecs-logger

A logger compatible with Elastic Common Schema (ECS) Logging

4 releases (2 stable)

1.1.0 Mar 31, 2023
1.0.0 Dec 5, 2021
1.0.0-rc.2 Nov 26, 2021

#193 in Debugging

Download history 15/week @ 2023-12-23 47/week @ 2023-12-30 109/week @ 2024-01-06 128/week @ 2024-01-13 152/week @ 2024-01-20 227/week @ 2024-01-27 163/week @ 2024-02-03 201/week @ 2024-02-10 204/week @ 2024-02-17 197/week @ 2024-02-24 158/week @ 2024-03-02 207/week @ 2024-03-09 161/week @ 2024-03-16 185/week @ 2024-03-23 269/week @ 2024-03-30 89/week @ 2024-04-06

733 downloads per month

MIT/Apache

32KB
451 lines

ecs-logger

CI status crate version docs online MIT or Apache 2.0 Licenses

A Rust logger compatible with Elastic Common Schema (ECS) Logging.

Features

  • Configurable via the RUST_LOG environment variable.
    • Uses env_logger under the hood.
    • All logging is disabled except for the error level by default.
  • Logs are written to stderr by default.

Installation

Add the following to your Cargo.toml file:

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

Documentation

Available at docs.rs

Example

In the following examples we assume the binary is ./example.

Basic logging

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"}}}

More filtering config examples are available at env_logger’s documentation.

Extra Fields

You can add extra fields to the log output by using the extra_fields module.

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();

Custom logging

You need to add env_logger to your Cargo.toml for the following examples.

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

Write to 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");

Configure log filters

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");

Default log fields

{
    "@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"
        }
    }
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Acknowledgment

The implementation of this software is based on env_logger, which is dual licenced as well.

Dependencies

~2.5–3.5MB
~68K SLoC