[][src]Crate tracing_gelf

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:12202".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 = "our dreams feel real while we're in them");

    // Create a span
    let span = tracing::info_span!("level 1");
    span.in_scope(|| {
        // Log inside a span
        tracing::warn!(message = "we need to go deeper");

        // Create an nested span
        let inner_span = tracing::info_span!("level 5");
        inner_span.in_scope(|| {
            // Log inside nested span
            tracing::error!(message = "you killed me");
        });
    });


    // Log a structured log
    tracing::info!(message = "he's out", spinning_top = true);

    // Don't exit
    loop {}
}

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.

Structs

Builder

A builder for Logger.

Logger

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

Enums

BuilderError

The error type for Logger building.