#log-format #log-entries #logging #log-messages #gelf #logs #logging-framework

gelf_logger

Minimal rust logger to send log entries in GELF

7 releases

0.3.0 Jun 25, 2024
0.2.0 Feb 14, 2024
0.1.4 Dec 2, 2019
0.1.3 Nov 29, 2019
0.1.2 May 23, 2019

#338 in Encoding

Download history 61/week @ 2024-07-27 35/week @ 2024-08-03 50/week @ 2024-08-10 180/week @ 2024-08-17 131/week @ 2024-08-24 205/week @ 2024-08-31 273/week @ 2024-09-07 538/week @ 2024-09-14 562/week @ 2024-09-21 302/week @ 2024-09-28 414/week @ 2024-10-05 275/week @ 2024-10-12 47/week @ 2024-10-19 66/week @ 2024-10-26 64/week @ 2024-11-02 18/week @ 2024-11-09

266 downloads per month
Used in log4rs-gelf

Custom license

52KB
959 lines

gelf_logger

Build Status Latest version Documentation License

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:

  1. serialize log entries using the serde_gelf crate.
  2. bufferize the result into memory.
  3. batch send over network using TCP/TLS.

Example

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

License

Licensed under BSD 3-Clause License or (https://opensource.org/licenses/BSD-3-Clause)

Dependencies

~4–33MB
~507K SLoC