Crates.io | gelf_logger |
lib.rs | gelf_logger |
version | 0.3.0 |
source | src |
created_at | 2019-05-14 15:43:44.978084 |
updated_at | 2024-06-25 13:10:25.311984 |
description | Minimal rust logger to send log entries in GELF. |
homepage | |
repository | https://github.com/ovh/rust-gelf_logger |
max_upload_size | |
id | 134319 |
size | 78,171 |
The Graylog Extended Log Format (GELF) is a log format that avoids the shortcomings of classic log formats. GELF is a great choice for logging from within applications. There are libraries and appenders for many programming languages and logging frameworks so it is easy to implement. You could use GELF to send every exception as a log message to your Graylog cluster.
The logger will:
use std::time::Duration;
use gelf_logger::{gelf_warn, Config, GelfLevel};
use log::info;
use serde_derive::Serialize;
#[derive(Serialize)]
struct Myapp {
name: String,
version: String,
}
impl Default for Myapp {
fn default() -> Myapp {
Myapp {
name: env!("CARGO_PKG_NAME").into(),
version: env!("CARGO_PKG_VERSION").into(),
}
}
}
fn main() {
let cfg = Config::builder()
.set_hostname("localhost".into())
.set_port(12202)
.set_level(GelfLevel::Informational)
.set_buffer_duration(Duration::from_millis(300))
.set_buffer_size(500)
.put_additional_field("myValue".into(), gelf_logger::Value::I64(10))
.set_null_character(true)
.build();
// Initialize logger
gelf_logger::init(cfg).unwrap();
// Send log using a macro defined in the create log
info!("common message");
// Use a macro from gelf_logger to send additional data
gelf_warn!(extra: &Myapp::default(), "My app info");
// make sure all buffered records are sent before exiting
gelf_logger::flush().unwrap();
}
Licensed under BSD 3-Clause License or (https://opensource.org/licenses/BSD-3-Clause)