From 207d3be8620c04832bdb807f712336efb4b15dd1 Mon Sep 17 00:00:00 2001 From: ellttben Date: Thu, 21 Mar 2024 09:03:45 +0000 Subject: [PATCH] Add documentation --- src/encode/json.rs | 9 ++++++--- src/encode/pattern/mod.rs | 10 +++++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/encode/json.rs b/src/encode/json.rs index f1592b6f..53efae9c 100644 --- a/src/encode/json.rs +++ b/src/encode/json.rs @@ -24,6 +24,11 @@ //! } //! } //! ``` +//! If the `log_kv` feature is enabled, an additional `attributes` field will +//! contain a map of the record's [log::kv][log_kv] structured logging +//! attributes. +//! +//! [log_kv]: https://docs.rs/log/latest/log/kv/index.html use chrono::{ format::{DelayedFormat, Fixed, Item}, @@ -173,9 +178,7 @@ mod kv { pub(crate) type Map = BTreeMap; - pub(crate) fn get_attributes( - source: &dyn log::kv::Source, - ) -> anyhow::Result { + pub(crate) fn get_attributes(source: &dyn log::kv::Source) -> anyhow::Result { struct Visitor { inner: Map, } diff --git a/src/encode/pattern/mod.rs b/src/encode/pattern/mod.rs index b5a8f741..7c77f50f 100644 --- a/src/encode/pattern/mod.rs +++ b/src/encode/pattern/mod.rs @@ -73,6 +73,14 @@ //! defaults to the empty string. //! * `{X(user_id)}` - `123e4567-e89b-12d3-a456-426655440000` //! * `{X(nonexistent_key)(no mapping)}` - `no mapping` +//! * `K`, `key_value` - A value from a [log::kv][log_kv] structured logging +//! record attributes. The first argument specifies the key, and the second +//! argument specifies the default value if the key is not present in the +//! log record's attributes. The second argument is optional, and defaults +//! to the empty string. This formatter requires the `log_kv` feature to be +//! enabled. +//! * `{K(user_id)}` - `123e4567-e89b-12d3-a456-426655440000` +//! * `{K(nonexistent_key)(no mapping)}` - `no mapping` //! * An "unnamed" formatter simply formats its argument, applying the format //! specification. //! * `{({l} {m})}` - `INFO hello` @@ -120,6 +128,7 @@ //! level `DEBUG` will be truncated to `DEBUG hello, wo`. //! //! [MDC]: https://crates.io/crates/log-mdc +//! [log_kv]: https://docs.rs/log/latest/log/kv/index.html use chrono::{Local, Utc}; use derivative::Derivative; @@ -804,7 +813,6 @@ mod tests { use std::thread; use super::*; - use super::{Chunk, PatternEncoder}; #[cfg(feature = "simple_writer")] use crate::encode::writer::simple::SimpleWriter; #[cfg(feature = "simple_writer")]