Skip to content

Commit

Permalink
update and clarify docs, make internal kv modules private
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Feb 16, 2024
1 parent d8dc6a8 commit f6b89c0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
6 changes: 5 additions & 1 deletion src/__private_api.rs
Expand Up @@ -93,7 +93,11 @@ mod kv_support {

pub type Value<'a> = kv::Value<'a>;

pub fn capture_to_value<'a, V: kv::value::ToValue + ?Sized>(v: &'a &'a V) -> Value<'a> {
// NOTE: Many functions here accept a double reference &&V
// This is so V itself can be ?Sized, while still letting us
// erase it to some dyn Trait (because &T is sized)

pub fn capture_to_value<'a, V: kv::ToValue + ?Sized>(v: &'a &'a V) -> Value<'a> {
v.to_value()
}

Expand Down
36 changes: 23 additions & 13 deletions src/kv/mod.rs
Expand Up @@ -15,9 +15,8 @@
//! # Structured logging in `log`
//!
//! Structured logging enhances traditional text-based log records with user-defined
//! attributes. Structured logs can be analyzed using a variety of tranditional
//! data processing techniques, without needing to find and parse attributes from
//! unstructured text first.
//! attributes. Structured logs can be analyzed using a variety of data processing
//! techniques, without needing to find and parse attributes from unstructured text first.
//!
//! In `log`, user-defined attributes are part of a [`Source`] on the log record.
//! Each attribute is a key-value; a pair of [`Key`] and [`Value`]. Keys are strings
Expand Down Expand Up @@ -49,13 +48,13 @@
//! - `:debug` will capture the value using `Debug`.
//! - `:%` will capture the value using `Display`.
//! - `:display` will capture the value using `Display`.
//! - `:error` will capture the value using `std::error::Error` (requires the `kv_unstable_error` feature).
//! - `:error` will capture the value using `std::error::Error` (requires the `kv_unstable_std` feature).
//! - `:sval` will capture the value using `sval::Value` (requires the `kv_unstable_sval` feature).
//! - `:serde` will capture the value using `serde::Serialize` (requires the `kv_unstable_serde` feature).
//!
//! ## Working with key-values on log records
//!
//! Use the [`LogRecord::key_values`] method to access key-values.
//! Use the [`Record::key_values`](../struct.Record.html#method.key_values) method to access key-values.
//!
//! Individual values can be pulled from the source by their key:
//!
Expand All @@ -75,7 +74,6 @@
//!
//! ```
//! # fn main() -> Result<(), log::kv::Error> {
//! # let record = log::Record::builder().key_values(&[("a", 1), ("b", 2), ("c", 3)]).build();
//! use std::collections::BTreeMap;
//!
//! use log::kv::{self, Source, Key, Value, VisitSource};
Expand All @@ -92,6 +90,7 @@
//!
//! let mut visitor = Collect(BTreeMap::new());
//!
//! # let record = log::Record::builder().key_values(&[("a", 1), ("b", 2), ("c", 3)]).build();
//! // info!(a = 1, b = 2, c = 3; "Something of interest");
//!
//! record.key_values().visit(&mut visitor)?;
Expand Down Expand Up @@ -189,11 +188,16 @@
//! # #[cfg(feature = "serde")]
//! # {
//! # use log::kv::Key;
//! # #[derive(serde::Serialize)] struct Data { a: i32, b: bool, c: &'static str }
//! #[derive(serde::Serialize)]
//! struct Data {
//! a: i32, b: bool,
//! c: &'static str,
//! }
//!
//! let data = Data { a: 1, b: true, c: "Some data" };
//!
//! # let source = [("a", log::kv::Value::from_serde(&data))];
//! # let record = log::Record::builder().key_values(&source).build();
//!
//! // info!(a = data; "Something of interest");
//!
//! let a = record.key_values().get(Key::from("a")).unwrap();
Expand All @@ -208,18 +212,24 @@
//! If you're in a no-std environment, you can use `sval`. In other cases, you can use `serde`.
//! Log producers and log consumers don't need to agree on the serialization framework.
//! A value can be captured using its `serde::Serialize` implementation and still be serialized
//! through `sval` without losing any structure.
//! through `sval` without losing any structure or data.
//!
//! Values can also always be formatted using the standard `Debug` and `Display`
//! traits:
//!
//! ```
//! # use log::kv::Key;
//! # #[derive(Debug)] struct Data { a: i32, b: bool, c: &'static str }
//! # #[derive(Debug)]
//! struct Data {
//! a: i32,
//! b: bool,
//! c: &'static str,
//! }
//!
//! let data = Data { a: 1, b: true, c: "Some data" };
//!
//! # let source = [("a", log::kv::Value::from_debug(&data))];
//! # let record = log::Record::builder().key_values(&source).build();
//!
//! // info!(a = data; "Something of interest");
//!
//! let a = record.key_values().get(Key::from("a")).unwrap();
Expand All @@ -229,8 +239,8 @@

mod error;
mod key;
pub mod source;
pub mod value;
mod source;
mod value;

pub use self::error::Error;
pub use self::key::{Key, ToKey};
Expand Down
3 changes: 1 addition & 2 deletions src/kv/source.rs
Expand Up @@ -81,8 +81,7 @@ pub trait Source {
/// A source that knows the number of key-values upfront may provide a more
/// efficient implementation.
///
/// A subsequent call to `visit` should yield the same number of key-values
/// to the visitor, unless that visitor fails part way through.
/// A subsequent call to `visit` should yield the same number of key-values.
fn count(&self) -> usize {
count_default(self)
}
Expand Down

0 comments on commit f6b89c0

Please sign in to comment.