Rust - Push logs with gelf_logger or log4rs-gelf

Last updated 16th January, 2023

Objective

This guide will show you how to push your logs to Logs Data Platform using Rust.

Rust has a logging implementation (log) which is widely used. OVHcloud has implemented this system to support the GELF format:

  • gelf_logger: This is a minimal logger.
  • log4rs-gelf: Based on gelf_logger, this implementation is compatible with the complex configurable framework log4rs.

Those loggers will:

  • serialize log entries using the serde_gelf crate.
  • bufferize the result into memory.
  • batch send over network using TCP/TLS.
  • a facility to ensure fields suits the LDP naming conventions.

Requirements

To complete this guide you will need:

gelf_logger

You can start using it by first adding it to your Cargo.toml:

[dependencies]
gelf_logger = { version = "0.1", features = ["ovh-ldp"] }
# or
[dependencies.gelf_logger]
version = "0.1"
features = ["ovh-ldp"]

End then in you main.rs:

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

fn main() {
    let cfg = Config::ldp("gra1.logs.ovh.com", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");

    // Initialize logger
    gelf_logger::init(cfg).unwrap();

    // Send log using a macro defined in the create log
    info!("common message");

    // make sure all buffered records are sent before exiting
    gelf_logger::flush().unwrap();
}

You could also look at the generated API documentaton.

log4rs-gelf

You can start using it by first adding it to your Cargo.toml:

[dependencies]
log4rs_gelf = { version = "0.1", features = ["ovh-ldp"] }
# or
[dependencies.log4rs_gelf]
version = "0.1"
features = ["ovh-ldp"]

Examples

From a YAML configuration 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

And then:

extern crate log4rs_gelf;

fn main() {
    log4rs_gelf::init_file("/tmp/log4rs.yml", None).unwrap();

    // Do whatever

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

Programmatically constructing a configuration

extern crate log;
extern crate log4rs;
extern crate log4rs_gelf;

use log4rs::config::{Config, Appender, Root};
use log::LevelFilter;

fn main() {
    let buffer = log4rs_gelf::BufferAppender::builder("gra1.logs.ovh.com","XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")
        .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 !");
}

You could also look at the generated API documentation.

Go further


Did you find this guide useful?

Please feel free to give any suggestions in order to improve this documentation.

Whether your feedback is about images, content, or structure, please share it, so that we can improve it together.

Your support requests will not be processed via this form. To do this, please use the "Create a ticket" form.

Thank you. Your feedback has been received.


These guides might also interest you...

OVHcloud Community

Access your community space. Ask questions, search for information, post content, and interact with other OVHcloud Community members.

Discuss with the OVHcloud community