Struct ContextValue

Source
pub struct ContextValue(/* private fields */);
Expand description

Represents a type of value that can be stored in the log context.

The ContextValue type is a flexible container designed to hold various kinds of data that can be associated with a log entry. It supports primitive types, strings, and more complex types such as those implementing std::fmt::Debug, std::fmt::Display, std::error::Error, or serde::Serialize.

This allows for rich and structured logging, enabling developers to attach meaningful context to log messages.

§Examples

use context_logger::ContextValue;

let value = ContextValue::display("example string");
let number = ContextValue::from(42);
let debug_value = ContextValue::debug(vec![1, 2, 3]);

Implementations§

Source§

impl ContextValue

Source

pub fn null() -> Self

Creates a null context value.

Source

pub fn serde<S>(value: S) -> Self
where S: Serialize + Send + Sync + 'static,

Creates a context value from a serde::Serialize.

Source

pub fn display<T>(value: T) -> Self
where T: Display + Send + Sync + 'static,

Creates a context value from a std::fmt::Display.

Source

pub fn debug<T>(value: T) -> Self
where T: Debug + Send + Sync + 'static,

Creates a context value from a std::fmt::Debug.

Source

pub fn error<T>(value: T) -> Self
where T: Error + Send + Sync + 'static,

Creates a context value from a std::error::Error.

Source

pub fn as_log_value(&self) -> Value<'_>

Represents a context value that can be used with the log crate.

Trait Implementations§

Source§

impl Debug for ContextValue

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for ContextValue

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&str> for ContextValue

Source§

fn from(value: &str) -> Self

Converts to this type from the input type.
Source§

impl From<Cow<'_, str>> for ContextValue

Source§

fn from(value: Cow<'_, str>) -> Self

Converts to this type from the input type.
Source§

impl From<String> for ContextValue

Source§

fn from(value: String) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for ContextValue

Source§

fn from(value: bool) -> Self

Converts to this type from the input type.
Source§

impl From<char> for ContextValue

Source§

fn from(value: char) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for ContextValue

Source§

fn from(value: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i128> for ContextValue

Source§

fn from(value: i128) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for ContextValue

Source§

fn from(value: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for ContextValue

Source§

fn from(value: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for ContextValue

Source§

fn from(value: i64) -> Self

Converts to this type from the input type.
Source§

impl From<i8> for ContextValue

Source§

fn from(value: i8) -> Self

Converts to this type from the input type.
Source§

impl From<u128> for ContextValue

Source§

fn from(value: u128) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for ContextValue

Source§

fn from(value: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for ContextValue

Source§

fn from(value: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for ContextValue

Source§

fn from(value: u64) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for ContextValue

Source§

fn from(value: u8) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.