Crate gelf [] [src]

A GELF library for Rust.

The library can be used either as a standalone logging library or it can be used as a logging-subsystem with the log-crate

Use

In general you should only use this crate in applications. Libraries should only develop against log. This then allows applications to use a custom logging-framework (like gelf) to do the actual logging.

Standalone

Standalone usage is recommended if the lightweight log-crate's features are not sufficient. In this case you need to inject/use an instance of gelf::Logger directly. This allows for sending custom built gelf::Message-objects. These messages can use all bells and whistles of GELF.

Example

extern crate gelf;

use gelf::{Logger, UdpBackend, Message, Level};

pub fn main() {
    // Set up logging
    let backend = UdpBackend::new("127.0.0.1:12201")
        .expect("Failed to create UDP backend");
    let mut logger = Logger::new(Box::new(backend))
        .expect("Failed to determine hostname");
    logger.set_default_metadata(String::from("facility"),
         String::from("example-rust-app"));

    // Create a (complex) message
    let mut message = Message::new(String::from("Custom message!"));
    message
        .set_full_message(String::from("The full message text is more descriptive"))
        .set_metadata("foo", String::from("bar")).unwrap()
        .set_metadata("baz", String::from("bat")).unwrap();

    // Log it
    logger.log_message(message);
}

With log

Usage with log allows to log easily with the help of its macros. There is no need to inject or access the logger object anywhere in your application.

All the context information (line, file, etc.) the log-crate provides is added as metadata to the logged GELF message.

#[macro_use]
extern crate log;

extern crate gelf;

use gelf::{Logger, UdpBackend, Message, Level};
use log::LogLevelFilter;

pub fn main() {
    let backend = UdpBackend::new("127.0.0.1:12201")
        .expect("Failed to create UDP backend");

    // Init logging system
    let logger = Logger::new(Box::new(backend))
        .expect("Failed to determine hostname");
    logger.install(LogLevelFilter::Trace)
        .expect("Failed to install logger");

    info!("Descend into our program!");
    somewhere()
}

pub fn somewhere() {
    trace!("Trace something here!");
    over::the_rainbow();
}

mod over {
    pub fn the_rainbow() {
        error!("Oh well...");
    }
}

Structs

Error

The Error type.

Logger

Logger for sending log-messages

Message

Message is thre representation of a GELF message.

NullBackend

The NullBackend is a utility backend which discards all messages

TcpBackend

TcpBackend is a simple GELF over TCP backend.

UdpBackend

UdpBackend is the default and standard GELF backend

Enums

ChunkSize

ChunkSize is a value type representing the size of a message-chunk

ErrorKind

The kind of an error.

Level

GELF's representation of an error level

MessageCompression

MessageCompression represents all possible compression algorithms in GELF.

Traits

Backend

A trait for a GELF backend

Type Definitions

Result

Convenient wrapper around std::Result.