Struct Record

Source
pub struct Record { /* private fields */ }
Expand description

A log record containing all information about a log message

This struct is designed to be thread-safe and can be safely shared between threads. All methods that modify the record take ownership and return a new instance, ensuring thread safety without the need for locks.

Implementations§

Source§

impl Record

Source

pub fn new( level: LogLevel, message: impl Into<String>, module: Option<String>, file: Option<String>, line: Option<u32>, ) -> Self

Create a new log record

Source

pub fn level(&self) -> LogLevel

Get the log level

Source

pub fn message(&self) -> &str

Get the log message

Source

pub fn module(&self) -> &str

Get the module path

Source

pub fn file(&self) -> &str

Get the file name

Source

pub fn line(&self) -> u32

Get the line number

Source

pub fn timestamp(&self) -> DateTime<Utc>

Get the timestamp

Source

pub fn metadata(&self) -> &HashMap<String, String>

Get the metadata

Source

pub fn context(&self) -> &HashMap<String, Value>

Get the context data

Source

pub fn with_metadata( self, key: impl Into<String>, value: impl Into<String>, ) -> Self

Add metadata to the record

Source

pub fn with_structured_data<T: Serialize + ?Sized>( self, key: &str, value: &T, ) -> Result<Self, Error>

Adds structured data to the record’s metadata.

The data will be serialized to JSON and stored with the given key. Returns a Result indicating success or failure of serialization.

§Example
use rust_loguru::{Record, LogLevel};
use serde_json::json;

let record = Record::new(LogLevel::Info, "test message", Some("test".to_string()), Some("test.rs".to_string()), Some(42));
let result = record.with_structured_data("user", &json!({
    "id": 123,
    "name": "test"
}));
assert!(result.is_ok());
Source

pub fn with_context(self, key: impl Into<String>, value: Value) -> Self

Adds structured context data to the record.

The data will be stored as a serde_json::Value and can be used for structured logging and analysis.

§Example
use rust_loguru::{Record, LogLevel};
use serde_json::json;

let record = Record::new(LogLevel::Info, "test message", None, None, None);
let record = record.with_context("user", json!({
    "id": 123,
    "name": "test"
}));
Source

pub fn with_deferred_format<F>(self, format_fn: F) -> Self
where F: Fn(&Record) -> String + Send + Sync + 'static,

Sets a deferred formatting function for the record.

This allows for lazy evaluation of the record’s string representation, which can improve performance when the record is not actually displayed.

§Example
use rust_loguru::{Record, LogLevel};

let record = Record::new(LogLevel::Info, "test message", None, None, None);
let record = record.with_deferred_format(|r| {
    format!("[{}] {} - {}", r.timestamp(), r.level(), r.message())
});
Source

pub fn get_metadata(&self, key: &str) -> Option<&str>

Returns the value associated with the given key, if any.

Source

pub fn get_context(&self, key: &str) -> Option<&Value>

Returns the context value associated with the given key, if any.

Source

pub fn has_context(&self) -> bool

Returns true if the record has any structured context data.

Source

pub fn has_metadata(&self) -> bool

Returns true if the record has any metadata.

Source

pub fn has_formatter(&self) -> bool

Returns true if the record has a deferred formatter.

Source

pub fn context_len(&self) -> usize

Returns the number of context entries in the record.

Source

pub fn metadata_len(&self) -> usize

Returns the number of metadata entries in the record.

Trait Implementations§

Source§

impl Clone for Record

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Record

Source§

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

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

impl<'de> Deserialize<'de> for Record

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Record

Source§

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

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

impl Serialize for Record

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl Freeze for Record

§

impl !RefUnwindSafe for Record

§

impl Send for Record

§

impl Sync for Record

§

impl Unpin for Record

§

impl !UnwindSafe for Record

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,