Crate tracing_gelf
source ·Expand description
Provides a tracing
Layer
for Graylog structured logging.
Usage
use std::net::SocketAddr;
use tracing_gelf::Logger;
#[tokio::main]
async fn main() {
// Graylog address
let address = "127.0.0.1:12201";
// Initialize subscriber
let mut conn_handle = Logger::builder().init_tcp(address).unwrap();
// Spawn background task
// Any futures executor can be used
tokio::spawn(async move { conn_handle.connect().await });
// 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(|| {
let test = tracing::info_span!("deeper in cave", smell = "damp");
test.in_scope(|| {
// Send a log to Graylog, inside a nested span
tracing::warn!(message = "oh god, it's dark in here");
})
});
// Send a log to Graylog
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 toshort_message
. - If
short_message
(ormessage
) Event field is missing thenshort_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
- A builder for
Logger
. - A sequence of errors which occurred during a connection attempt.
- Provides an interface for connecting (and reconnecting) to Graylog. Without an established connection logs will not be sent. Messages logged without an established connection will sit in the buffer until they can be drained.
- A
Layer
responsible for sending structured logs to Graylog. - A TCP connection to Graylog.
- A UDP connection to Graylog.
Enums
- The error type for
Logger
building.