Crate tracing_gelf[][src]

Expand description

Provides Graylog structured logging using the tracing.

Usage

use std::net::SocketAddr;
use tracing_gelf::Logger;

#[tokio::main]
async fn main() {
    // Graylog address
    let address: SocketAddr = "127.0.0.1:12201".parse().unwrap();

    // Start tracing
    let bg_task = Logger::builder().init_tcp(address).unwrap();

    // Spawn background task
    // Any futures executor can be used
    tokio::spawn(bg_task);

    // Send a log to Graylog
    tracing::info!(message = "oooh, what's in here?");

    // Create a span
    let span = tracing::info_span!("cave");
    span.in_scope(|| {
        // Log inside a span
        let test = tracing::info_span!("deeper in cave", smell = "damp");
        test.in_scope(|| {
            tracing::warn!(message = "oh god, it's dark in here");
        })
    });

    // Log a structured log
    tracing::error!(message = "i'm glad to be out", spook_lvl = 3, ruck_sack = ?["glasses", "inhaler", "large bat"]);

}

GELF Encoding

Events are encoded into GELF format as follows:

  • Event fields are inserted as GELF additional fields, _field_name.
  • Event field named message is renamed to short_message.
  • If short_message (or message) Event field is missing then short_message is set to the empty string.
  • Event fields whose names collide with GELF required fields are coerced into the required types and overrides defaults given in the builder.
  • The hierarchy of spans is concatenated and inserted as span_a:span_b:span_c and inserted as an additional field _span.

Modules

This module contains lower-level primitives for visiting fields.

Structs

A builder for Logger.

Logger represents a Layer responsible for sending structured logs to Graylog.

Enums

The error type for Logger building.