[][src]Crate gelf_logger

gelf_logger

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

#[macro_use]
extern crate gelf_logger;
#[macro_use]
extern crate log;
#[macro_use]
extern crate serde_derive;
extern crate serde_value;

use serde_gelf::GelfLevel;

use gelf_logger::Config;

#[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(300)
        .set_buffer_size(500)
        .put_additional_field("myValue".into(), serde_value::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();
}

Macros

gelf_alert

Logs a message at the alert level (Should be corrected immediately).

gelf_critical

Logs a message at the critical level (Should be corrected immediately).

gelf_debug

Logs a message at the debug level (Mainly used by developers).

gelf_emergency

Logs a message at the emergency level (A "panic" condition).

gelf_error

Logs a message at the error level (Non-urgent failures).

gelf_info

Logs a message at the info level (Normal message).

gelf_log

The standard logging macro.

gelf_notice

Logs a message at the notice level (Unusual event).

gelf_warn

Logs a message at the warning level (Warning messages).

Structs

Config

Struct to manipulate configuration.

ConfigBuilder

Builder for Config.

Enums

Error

Enum to represent errors

Event

Enum used to send commands over the channel.

Functions

flush

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

init

Initialize the logger using the given Config.

init_from_file

Initialize the logger using a configuration file.