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
impl Record
Sourcepub fn new(
level: LogLevel,
message: impl Into<String>,
module: Option<String>,
file: Option<String>,
line: Option<u32>,
) -> Self
pub fn new( level: LogLevel, message: impl Into<String>, module: Option<String>, file: Option<String>, line: Option<u32>, ) -> Self
Create a new log record
Sourcepub fn with_metadata(
self,
key: impl Into<String>,
value: impl Into<String>,
) -> Self
pub fn with_metadata( self, key: impl Into<String>, value: impl Into<String>, ) -> Self
Add metadata to the record
Sourcepub fn with_structured_data<T: Serialize + ?Sized>(
self,
key: &str,
value: &T,
) -> Result<Self, Error>
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());Sourcepub fn with_context(self, key: impl Into<String>, value: Value) -> Self
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"
}));Sourcepub fn with_deferred_format<F>(self, format_fn: F) -> Self
pub fn with_deferred_format<F>(self, format_fn: F) -> Self
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())
});Sourcepub fn get_metadata(&self, key: &str) -> Option<&str>
pub fn get_metadata(&self, key: &str) -> Option<&str>
Returns the value associated with the given key, if any.
Sourcepub fn get_context(&self, key: &str) -> Option<&Value>
pub fn get_context(&self, key: &str) -> Option<&Value>
Returns the context value associated with the given key, if any.
Sourcepub fn has_context(&self) -> bool
pub fn has_context(&self) -> bool
Returns true if the record has any structured context data.
Sourcepub fn has_metadata(&self) -> bool
pub fn has_metadata(&self) -> bool
Returns true if the record has any metadata.
Sourcepub fn has_formatter(&self) -> bool
pub fn has_formatter(&self) -> bool
Returns true if the record has a deferred formatter.
Sourcepub fn context_len(&self) -> usize
pub fn context_len(&self) -> usize
Returns the number of context entries in the record.
Sourcepub fn metadata_len(&self) -> usize
pub fn metadata_len(&self) -> usize
Returns the number of metadata entries in the record.