tracing_log/
lib.rs

1//! Adapters for connecting unstructured log records from the `log` crate into
2//! the `tracing` ecosystem.
3//!
4//! # Overview
5//!
6//! [`tracing`] is a framework for instrumenting Rust programs with context-aware,
7//! structured, event-based diagnostic information. This crate provides
8//! compatibility layers for using `tracing` alongside the logging facade provided
9//! by the [`log`] crate.
10//!
11//! This crate provides:
12//!
13//! - [`AsTrace`] and [`AsLog`] traits for converting between `tracing` and `log` types.
14//! - [`LogTracer`], a [`log::Log`] implementation that consumes [`log::Record`]s
15//!   and outputs them as [`tracing::Event`].
16//!
17//! *Compiler support: [requires `rustc` 1.65+][msrv]*
18//!
19//! [msrv]: #supported-rust-versions
20//!
21//! # Usage
22//!
23//! ## Convert log records to tracing `Event`s
24//!
25//! To convert [`log::Record`]s as [`tracing::Event`]s, set `LogTracer` as the default
26//! logger by calling its [`init`] or [`init_with_filter`] methods.
27//!
28//! ```rust
29//! # use std::error::Error;
30//! use tracing_log::LogTracer;
31//! use log;
32//!
33//! # fn main() -> Result<(), Box<Error>> {
34//! LogTracer::init()?;
35//!
36//! // will be available for Subscribers as a tracing Event
37//! log::trace!("an example trace log");
38//! # Ok(())
39//! # }
40//! ```
41//!
42//! This conversion does not convert unstructured data in log records (such as
43//! values passed as format arguments to the `log!` macro) to structured
44//! `tracing` fields. However, it *does* attach these new events to to the
45//! span that was currently executing when the record was logged. This is the
46//! primary use-case for this library: making it possible to locate the log
47//! records emitted by dependencies which use `log` within the context of a
48//! trace.
49//!
50//! ## Convert tracing `Event`s to logs
51//!
52//! Enabling the ["log" and "log-always" feature flags][flags] on the `tracing`
53//! crate will cause all `tracing` spans and events to emit `log::Record`s as
54//! they occur.
55//!
56//! ## Caution: Mixing both conversions
57//!
58//! Note that `log::Logger` implementations that convert log records to trace events
59//! should not be used with `Subscriber`s that convert trace events _back_ into
60//! `log` records, as doing so will result in the event recursing between the subscriber
61//! and the logger forever (or, in real life, probably overflowing the call stack).
62//!
63//! If the logging of trace events generated from log records produced by the
64//! `log` crate is desired, either the `log` crate should not be used to
65//! implement this logging, or an additional layer of filtering will be
66//! required to avoid infinitely converting between `Event` and `log::Record`.
67//!
68//! ## Feature Flags
69//!
70//! * `std`: enables features that require the Rust standard library (on by default)
71//! * `log-tracer`: enables the `LogTracer` type (on by default)
72//! * `interest-cache`: makes it possible to configure an interest cache for
73//!   logs emitted through the `log` crate (see [`Builder::with_interest_cache`]); requires `std`
74//!
75//! ## Supported Rust Versions
76//!
77//! Tracing is built against the latest stable release. The minimum supported
78//! version is 1.65. The current Tracing version is not guaranteed to build on
79//! Rust versions earlier than the minimum supported version.
80//!
81//! Tracing follows the same compiler support policies as the rest of the Tokio
82//! project. The current stable Rust compiler and the three most recent minor
83//! versions before it will always be supported. For example, if the current
84//! stable compiler version is 1.69, the minimum supported version will not be
85//! increased past 1.66, three minor versions prior. Increasing the minimum
86//! supported compiler version is not considered a semver breaking change as
87//! long as doing so complies with this policy.
88//!
89//! [`init`]: LogTracer::init
90//! [`init_with_filter`]: LogTracer::init_with_filter
91//! [`tracing`]: https://crates.io/crates/tracing
92//! [`tracing::Subscriber`]: https://docs.rs/tracing/latest/tracing/trait.Subscriber.html
93//! [`Subscriber`]: https://docs.rs/tracing/latest/tracing/trait.Subscriber.html
94//! [`tracing::Event`]: https://docs.rs/tracing/latest/tracing/struct.Event.html
95//! [flags]: https://docs.rs/tracing/latest/tracing/#crate-feature-flags
96//! [`Builder::with_interest_cache`]: log_tracer::Builder::with_interest_cache
97#![doc(
98    html_logo_url = "https://raw.githubusercontent.com/tokio-rs/tracing/main/assets/logo-type.png",
99    issue_tracker_base_url = "https://github.com/tokio-rs/tracing/issues/"
100)]
101#![cfg_attr(docsrs, feature(doc_cfg), deny(rustdoc::broken_intra_doc_links))]
102#![warn(
103    missing_debug_implementations,
104    missing_docs,
105    rust_2018_idioms,
106    unreachable_pub,
107    bad_style,
108    dead_code,
109    improper_ctypes,
110    non_shorthand_field_patterns,
111    no_mangle_generic_items,
112    overflowing_literals,
113    path_statements,
114    patterns_in_fns_without_body,
115    private_interfaces,
116    private_bounds,
117    unconditional_recursion,
118    unused,
119    unused_allocation,
120    unused_comparisons,
121    unused_parens,
122    while_true
123)]
124use once_cell::sync::Lazy;
125
126use std::{fmt, io};
127
128use tracing_core::{
129    callsite::{self, Callsite},
130    dispatcher,
131    field::{self, Field, Visit},
132    identify_callsite,
133    metadata::{Kind, Level},
134    subscriber, Event, Metadata,
135};
136
137#[cfg(feature = "log-tracer")]
138#[cfg_attr(docsrs, doc(cfg(feature = "log-tracer")))]
139pub mod log_tracer;
140
141#[cfg(feature = "log-tracer")]
142#[cfg_attr(docsrs, doc(cfg(feature = "log-tracer")))]
143#[doc(inline)]
144pub use self::log_tracer::LogTracer;
145
146pub use log;
147
148#[cfg(all(feature = "interest-cache", feature = "log-tracer", feature = "std"))]
149mod interest_cache;
150
151#[cfg(all(feature = "interest-cache", feature = "log-tracer", feature = "std"))]
152#[cfg_attr(
153    docsrs,
154    doc(cfg(all(feature = "interest-cache", feature = "log-tracer", feature = "std")))
155)]
156pub use crate::interest_cache::InterestCacheConfig;
157
158/// Format a log record as a trace event in the current span.
159pub fn format_trace(record: &log::Record<'_>) -> io::Result<()> {
160    dispatch_record(record);
161    Ok(())
162}
163
164// XXX(eliza): this is factored out so that we don't have to deal with the pub
165// function `format_trace`'s `Result` return type...maybe we should get rid of
166// that in 0.2...
167pub(crate) fn dispatch_record(record: &log::Record<'_>) {
168    dispatcher::get_default(|dispatch| {
169        let filter_meta = record.as_trace();
170        if !dispatch.enabled(&filter_meta) {
171            return;
172        }
173
174        let (_, keys, meta) = loglevel_to_cs(record.level());
175
176        let log_module = record.module_path();
177        let log_file = record.file();
178        let log_line = record.line();
179
180        let module = log_module.as_ref().map(|s| s as &dyn field::Value);
181        let file = log_file.as_ref().map(|s| s as &dyn field::Value);
182        let line = log_line.as_ref().map(|s| s as &dyn field::Value);
183
184        dispatch.event(&Event::new(
185            meta,
186            &meta.fields().value_set(&[
187                (&keys.message, Some(record.args() as &dyn field::Value)),
188                (&keys.target, Some(&record.target())),
189                (&keys.module, module),
190                (&keys.file, file),
191                (&keys.line, line),
192            ]),
193        ));
194    });
195}
196
197/// Trait implemented for `tracing` types that can be converted to a `log`
198/// equivalent.
199pub trait AsLog: crate::sealed::Sealed {
200    /// The `log` type that this type can be converted into.
201    type Log;
202    /// Returns the `log` equivalent of `self`.
203    fn as_log(&self) -> Self::Log;
204}
205
206/// Trait implemented for `log` types that can be converted to a `tracing`
207/// equivalent.
208pub trait AsTrace: crate::sealed::Sealed {
209    /// The `tracing` type that this type can be converted into.
210    type Trace;
211    /// Returns the `tracing` equivalent of `self`.
212    fn as_trace(&self) -> Self::Trace;
213}
214
215impl crate::sealed::Sealed for Metadata<'_> {}
216
217impl<'a> AsLog for Metadata<'a> {
218    type Log = log::Metadata<'a>;
219    fn as_log(&self) -> Self::Log {
220        log::Metadata::builder()
221            .level(self.level().as_log())
222            .target(self.target())
223            .build()
224    }
225}
226impl crate::sealed::Sealed for log::Metadata<'_> {}
227
228impl<'a> AsTrace for log::Metadata<'a> {
229    type Trace = Metadata<'a>;
230    fn as_trace(&self) -> Self::Trace {
231        let cs_id = identify_callsite!(loglevel_to_cs(self.level()).0);
232        Metadata::new(
233            "log record",
234            self.target(),
235            self.level().as_trace(),
236            None,
237            None,
238            None,
239            field::FieldSet::new(FIELD_NAMES, cs_id),
240            Kind::EVENT,
241        )
242    }
243}
244
245struct Fields {
246    message: field::Field,
247    target: field::Field,
248    module: field::Field,
249    file: field::Field,
250    line: field::Field,
251}
252
253static FIELD_NAMES: &[&str] = &[
254    "message",
255    "log.target",
256    "log.module_path",
257    "log.file",
258    "log.line",
259];
260
261impl Fields {
262    fn new(cs: &'static dyn Callsite) -> Self {
263        let fieldset = cs.metadata().fields();
264        let message = fieldset.field("message").unwrap();
265        let target = fieldset.field("log.target").unwrap();
266        let module = fieldset.field("log.module_path").unwrap();
267        let file = fieldset.field("log.file").unwrap();
268        let line = fieldset.field("log.line").unwrap();
269        Fields {
270            message,
271            target,
272            module,
273            file,
274            line,
275        }
276    }
277}
278
279macro_rules! log_cs {
280    ($level:expr, $cs:ident, $meta:ident, $ty:ident) => {
281        struct $ty;
282        static $cs: $ty = $ty;
283        static $meta: Metadata<'static> = Metadata::new(
284            "log event",
285            "log",
286            $level,
287            ::core::option::Option::None,
288            ::core::option::Option::None,
289            ::core::option::Option::None,
290            field::FieldSet::new(FIELD_NAMES, identify_callsite!(&$cs)),
291            Kind::EVENT,
292        );
293
294        impl callsite::Callsite for $ty {
295            fn set_interest(&self, _: subscriber::Interest) {}
296            fn metadata(&self) -> &'static Metadata<'static> {
297                &$meta
298            }
299        }
300    };
301}
302
303log_cs!(
304    tracing_core::Level::TRACE,
305    TRACE_CS,
306    TRACE_META,
307    TraceCallsite
308);
309log_cs!(
310    tracing_core::Level::DEBUG,
311    DEBUG_CS,
312    DEBUG_META,
313    DebugCallsite
314);
315log_cs!(tracing_core::Level::INFO, INFO_CS, INFO_META, InfoCallsite);
316log_cs!(tracing_core::Level::WARN, WARN_CS, WARN_META, WarnCallsite);
317log_cs!(
318    tracing_core::Level::ERROR,
319    ERROR_CS,
320    ERROR_META,
321    ErrorCallsite
322);
323
324static TRACE_FIELDS: Lazy<Fields> = Lazy::new(|| Fields::new(&TRACE_CS));
325static DEBUG_FIELDS: Lazy<Fields> = Lazy::new(|| Fields::new(&DEBUG_CS));
326static INFO_FIELDS: Lazy<Fields> = Lazy::new(|| Fields::new(&INFO_CS));
327static WARN_FIELDS: Lazy<Fields> = Lazy::new(|| Fields::new(&WARN_CS));
328static ERROR_FIELDS: Lazy<Fields> = Lazy::new(|| Fields::new(&ERROR_CS));
329
330fn level_to_cs(level: Level) -> (&'static dyn Callsite, &'static Fields) {
331    match level {
332        Level::TRACE => (&TRACE_CS, &*TRACE_FIELDS),
333        Level::DEBUG => (&DEBUG_CS, &*DEBUG_FIELDS),
334        Level::INFO => (&INFO_CS, &*INFO_FIELDS),
335        Level::WARN => (&WARN_CS, &*WARN_FIELDS),
336        Level::ERROR => (&ERROR_CS, &*ERROR_FIELDS),
337    }
338}
339
340fn loglevel_to_cs(
341    level: log::Level,
342) -> (
343    &'static dyn Callsite,
344    &'static Fields,
345    &'static Metadata<'static>,
346) {
347    match level {
348        log::Level::Trace => (&TRACE_CS, &*TRACE_FIELDS, &TRACE_META),
349        log::Level::Debug => (&DEBUG_CS, &*DEBUG_FIELDS, &DEBUG_META),
350        log::Level::Info => (&INFO_CS, &*INFO_FIELDS, &INFO_META),
351        log::Level::Warn => (&WARN_CS, &*WARN_FIELDS, &WARN_META),
352        log::Level::Error => (&ERROR_CS, &*ERROR_FIELDS, &ERROR_META),
353    }
354}
355
356impl crate::sealed::Sealed for log::Record<'_> {}
357
358impl<'a> AsTrace for log::Record<'a> {
359    type Trace = Metadata<'a>;
360    fn as_trace(&self) -> Self::Trace {
361        let cs_id = identify_callsite!(loglevel_to_cs(self.level()).0);
362        Metadata::new(
363            "log record",
364            self.target(),
365            self.level().as_trace(),
366            self.file(),
367            self.line(),
368            self.module_path(),
369            field::FieldSet::new(FIELD_NAMES, cs_id),
370            Kind::EVENT,
371        )
372    }
373}
374
375impl crate::sealed::Sealed for tracing_core::Level {}
376
377impl AsLog for tracing_core::Level {
378    type Log = log::Level;
379    fn as_log(&self) -> log::Level {
380        match *self {
381            tracing_core::Level::ERROR => log::Level::Error,
382            tracing_core::Level::WARN => log::Level::Warn,
383            tracing_core::Level::INFO => log::Level::Info,
384            tracing_core::Level::DEBUG => log::Level::Debug,
385            tracing_core::Level::TRACE => log::Level::Trace,
386        }
387    }
388}
389
390impl crate::sealed::Sealed for log::Level {}
391
392impl AsTrace for log::Level {
393    type Trace = tracing_core::Level;
394    #[inline]
395    fn as_trace(&self) -> tracing_core::Level {
396        match self {
397            log::Level::Error => tracing_core::Level::ERROR,
398            log::Level::Warn => tracing_core::Level::WARN,
399            log::Level::Info => tracing_core::Level::INFO,
400            log::Level::Debug => tracing_core::Level::DEBUG,
401            log::Level::Trace => tracing_core::Level::TRACE,
402        }
403    }
404}
405
406impl crate::sealed::Sealed for log::LevelFilter {}
407
408impl AsTrace for log::LevelFilter {
409    type Trace = tracing_core::LevelFilter;
410    #[inline]
411    fn as_trace(&self) -> tracing_core::LevelFilter {
412        match self {
413            log::LevelFilter::Off => tracing_core::LevelFilter::OFF,
414            log::LevelFilter::Error => tracing_core::LevelFilter::ERROR,
415            log::LevelFilter::Warn => tracing_core::LevelFilter::WARN,
416            log::LevelFilter::Info => tracing_core::LevelFilter::INFO,
417            log::LevelFilter::Debug => tracing_core::LevelFilter::DEBUG,
418            log::LevelFilter::Trace => tracing_core::LevelFilter::TRACE,
419        }
420    }
421}
422
423impl crate::sealed::Sealed for tracing_core::LevelFilter {}
424
425impl AsLog for tracing_core::LevelFilter {
426    type Log = log::LevelFilter;
427    #[inline]
428    fn as_log(&self) -> Self::Log {
429        match *self {
430            tracing_core::LevelFilter::OFF => log::LevelFilter::Off,
431            tracing_core::LevelFilter::ERROR => log::LevelFilter::Error,
432            tracing_core::LevelFilter::WARN => log::LevelFilter::Warn,
433            tracing_core::LevelFilter::INFO => log::LevelFilter::Info,
434            tracing_core::LevelFilter::DEBUG => log::LevelFilter::Debug,
435            tracing_core::LevelFilter::TRACE => log::LevelFilter::Trace,
436        }
437    }
438}
439/// Extends log `Event`s to provide complete `Metadata`.
440///
441/// In `tracing-log`, an `Event` produced by a log (through [`AsTrace`]) has an hard coded
442/// "log" target and no `file`, `line`, or `module_path` attributes. This happens because `Event`
443/// requires its `Metadata` to be `'static`, while [`log::Record`]s provide them with a generic
444/// lifetime.
445///
446/// However, these values are stored in the `Event`'s fields and
447/// the [`normalized_metadata`] method allows to build a new `Metadata`
448/// that only lives as long as its source `Event`, but provides complete
449/// data.
450///
451/// It can typically be used by `Subscriber`s when processing an `Event`,
452/// to allow accessing its complete metadata in a consistent way,
453/// regardless of the source of its source.
454///
455/// [`normalized_metadata`]: NormalizeEvent#normalized_metadata
456pub trait NormalizeEvent<'a>: crate::sealed::Sealed {
457    /// If this `Event` comes from a `log`, this method provides a new
458    /// normalized `Metadata` which has all available attributes
459    /// from the original log, including `file`, `line`, `module_path`
460    /// and `target`.
461    /// Returns `None` is the `Event` is not issued from a `log`.
462    fn normalized_metadata(&'a self) -> Option<Metadata<'a>>;
463    /// Returns whether this `Event` represents a log (from the `log` crate)
464    fn is_log(&self) -> bool;
465}
466
467impl crate::sealed::Sealed for Event<'_> {}
468
469impl<'a> NormalizeEvent<'a> for Event<'a> {
470    fn normalized_metadata(&'a self) -> Option<Metadata<'a>> {
471        let original = self.metadata();
472        if self.is_log() {
473            let mut fields = LogVisitor::new_for(self, level_to_cs(*original.level()).1);
474            self.record(&mut fields);
475
476            Some(Metadata::new(
477                "log event",
478                fields.target.unwrap_or("log"),
479                *original.level(),
480                fields.file,
481                fields.line.map(|l| l as u32),
482                fields.module_path,
483                field::FieldSet::new(&["message"], original.callsite()),
484                Kind::EVENT,
485            ))
486        } else {
487            None
488        }
489    }
490
491    fn is_log(&self) -> bool {
492        self.metadata().callsite() == identify_callsite!(level_to_cs(*self.metadata().level()).0)
493    }
494}
495
496struct LogVisitor<'a> {
497    target: Option<&'a str>,
498    module_path: Option<&'a str>,
499    file: Option<&'a str>,
500    line: Option<u64>,
501    fields: &'static Fields,
502}
503
504impl<'a> LogVisitor<'a> {
505    // We don't actually _use_ the provided event argument; it is simply to
506    // ensure that the `LogVisitor` does not outlive the event whose fields it
507    // is visiting, so that the reference casts in `record_str` are safe.
508    fn new_for(_event: &'a Event<'a>, fields: &'static Fields) -> Self {
509        Self {
510            target: None,
511            module_path: None,
512            file: None,
513            line: None,
514            fields,
515        }
516    }
517}
518
519impl Visit for LogVisitor<'_> {
520    fn record_debug(&mut self, _field: &Field, _value: &dyn fmt::Debug) {}
521
522    fn record_u64(&mut self, field: &Field, value: u64) {
523        if field == &self.fields.line {
524            self.line = Some(value);
525        }
526    }
527
528    fn record_str(&mut self, field: &Field, value: &str) {
529        unsafe {
530            // The `Visit` API erases the string slice's lifetime. However, we
531            // know it is part of the `Event` struct with a lifetime of `'a`. If
532            // (and only if!) this `LogVisitor` was constructed with the same
533            // lifetime parameter `'a` as the event in question, it's safe to
534            // cast these string slices to the `'a` lifetime.
535            if field == &self.fields.file {
536                self.file = Some(&*(value as *const _));
537            } else if field == &self.fields.target {
538                self.target = Some(&*(value as *const _));
539            } else if field == &self.fields.module {
540                self.module_path = Some(&*(value as *const _));
541            }
542        }
543    }
544}
545
546mod sealed {
547    pub trait Sealed {}
548}
549
550#[cfg(test)]
551mod test {
552    use super::*;
553
554    fn test_callsite(level: log::Level) {
555        let record = log::Record::builder()
556            .args(format_args!("Error!"))
557            .level(level)
558            .target("myApp")
559            .file(Some("server.rs"))
560            .line(Some(144))
561            .module_path(Some("server"))
562            .build();
563
564        let meta = record.as_trace();
565        let (cs, _keys, _) = loglevel_to_cs(record.level());
566        let cs_meta = cs.metadata();
567        assert_eq!(
568            meta.callsite(),
569            cs_meta.callsite(),
570            "actual: {:#?}\nexpected: {:#?}",
571            meta,
572            cs_meta
573        );
574        assert_eq!(meta.level(), &level.as_trace());
575    }
576
577    #[test]
578    fn error_callsite_is_correct() {
579        test_callsite(log::Level::Error);
580    }
581
582    #[test]
583    fn warn_callsite_is_correct() {
584        test_callsite(log::Level::Warn);
585    }
586
587    #[test]
588    fn info_callsite_is_correct() {
589        test_callsite(log::Level::Info);
590    }
591
592    #[test]
593    fn debug_callsite_is_correct() {
594        test_callsite(log::Level::Debug);
595    }
596
597    #[test]
598    fn trace_callsite_is_correct() {
599        test_callsite(log::Level::Trace);
600    }
601}