[][src]Crate log4rs_gelf

log4rs_gelf

log4rs is a highly configurable logging framework modeled after Java's Logback and log4j libraries.

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. You could use GELF to send every exception as a log message to your Graylog cluster.

This crate provides the GELF support in log4rs.

Examples

Configuration via a YAML file:

appenders:
  ldp:
    additional_fields:
      component: rust-cs
    buffer_duration: 5
    buffer_size: 5
    hostname: 127.0.0.1
    kind: buffer
    level: Informational
    null_character: true
    port: 12202
    use_tls: false
root:
  appenders:
  - ldp
  level: info
log4rs_gelf::init_file("/tmp/log4rs.yml", None).unwrap();

Programmatically constructing a configuration:

use serde_gelf::GelfLevel;
use serde_value::Value;
use log4rs::config::{Config, Appender, Root};
use log::LevelFilter;

fn main() {
   let buffer = log4rs_gelf::BufferAppender::builder()
       .set_level(GelfLevel::Informational)
       .set_hostname("localhost")
       .set_port(12202)
       .set_use_tls(false)
       .set_null_character(true)
       .set_buffer_size(Some(5))
       .set_buffer_duration(Some(5))
       .put_additional_field("component", Value::String("rust-cs".to_string()))
       .build()
       .unwrap();

   let config = Config::builder()
       .appender(Appender::builder().build("gelf", Box::new(buffer)))
       .build(Root::builder().appender("gelf").build(LevelFilter::Info))
       .unwrap();

   log4rs_gelf::init_config(config).unwrap();

   // Do whatever

   log4rs_gelf::flush().expect("Failed to send buffer, log records can be lost !");
}

Structs

BufferAppender

Struct to handle the GELF buffer.

BufferAppenderBuilder

Builder for BufferAppender.

Functions

flush

Force current logger record buffer to be sent to the remote server.

init_config

Initializes the global logger as a log4rs logger with the provided config.

init_file

Initializes the global logger as a log4rs logger configured via a file.