Crate tracing_log[−][src]
Adapters for connecting unstructured log records from the log
crate into
the tracing
ecosystem.
Overview
tracing
is a framework for instrumenting Rust programs with context-aware,
structured, event-based diagnostic information. This crate provides
compatibility layers for using tracing
alongside the logging facade provided
by the log
crate.
This crate provides:
AsTrace
andAsLog
traits for converting betweentracing
andlog
types.LogTracer
, alog::Log
implementation that consumeslog::Record
s and outputs them astracing::Event
.- An
env_logger
module, with helpers for using theenv_logger
crate withtracing
(optional, enabled by theenv-logger
feature).
Usage
Convert log records to tracing Event
s
To convert log::Record
s as tracing::Event
s, set LogTracer
as the default
logger by calling its init
or init_with_filter
methods.
use tracing_log::LogTracer; use log; LogTracer::init()?; // will be available for Subscribers as a tracing Event log::trace!("an example trace log");
This conversion does not convert unstructured data in log records (such as
values passed as format arguments to the log!
macro) to structured
tracing
fields. However, it does attach these new events to to the
span that was currently executing when the record was logged. This is the
primary use-case for this library: making it possible to locate the log
records emitted by dependencies which use log
within the context of a
trace.
Convert tracing Event
s to logs
Enabling the [“log” and “log-always” feature flags][flags] on the tracing
crate will cause all tracing
spans and events to emit log::Record
s as
they occur.
Caution: Mixing both conversions
Note that logger implementations that convert log records to trace events
should not be used with Subscriber
s that convert trace events back into
log records (such as the TraceLogger
), as doing so will result in the
event recursing between the subscriber and the logger forever (or, in real
life, probably overflowing the call stack).
If the logging of trace events generated from log records produced by the
log
crate is desired, either the log
crate should not be used to
implement this logging, or an additional layer of filtering will be
required to avoid infinitely converting between Event
and log::Record
.
Feature Flags
trace-logger
: enables an experimentallog
subscriber, deprecated since version 0.1.1.log-tracer
: enables theLogTracer
type (on by default)env_logger
: enables theenv_logger
module, with helpers for working with theenv_logger
crate.
Modules
log_tracer | An adapter for converting |
trace_logger | Deprecated A |
Structs
LogTracer | A simple “logger” that converts all log records into |
TraceLogger | Deprecated A |
Traits
AsLog | Trait implemented for |
AsTrace | Trait implemented for |
NormalizeEvent | Extends log |
Functions
format_trace | Format a log record as a trace event in the current span. |