pub struct CallLogger { /* private fields */ }Expand description
The CallLogger implements Log and provides some simple builder methods to help configure what and how to log.
Some sensible defaults are provided to perform the simple case of calling the echo program for all error level
logs with a JSON representation of the logged item. The logger then needs to be initialized (.init()) before use.
§Example - The simple logger that calls echo
CallLogger::new().init();Implementations§
Source§impl CallLogger
impl CallLogger
Sourcepub fn new() -> CallLogger
pub fn new() -> CallLogger
Creates a new CallLogger, use this along with the builder methods and then call init to set up the logger.
The default timestamp format is utc epoch (if the timestamps feature is enabled), and the default call app
that is called is echo.
§Example
CallLogger::new().init();Sourcepub fn with_level(self, level: LevelFilter) -> CallLogger
pub fn with_level(self, level: LevelFilter) -> CallLogger
The maximum log level that would be logged.
§Example
CallLogger::new()
.with_level(LevelFilter::Error)
.init();Sourcepub fn with_level_for<T: Into<String>>(
self,
target: T,
level: LevelFilter,
) -> Self
pub fn with_level_for<T: Into<String>>( self, target: T, level: LevelFilter, ) -> Self
The maximum log level that would be logged for a module where the target string is found in the log item’s target or module path.
§Example matching a module name
CallLogger::new()
.with_level_for("call_logger", LevelFilter::Error)
.init();
error!("test");§Example matching a target
CallLogger::new()
.with_level_for("call-target", LevelFilter::Error)
.init();
error!(target: "call-target", "test");Sourcepub fn with_call_target<T>(self, call_target: T) -> CallLogger
pub fn with_call_target<T>(self, call_target: T) -> CallLogger
Sets the command line application, script or URL that is called and passed the log details.
Example - Call an application with parameters
CallLogger::new()
.with_call_target("echo -n")
.init();Example - Call a URL
CallLogger::new()
.with_level(LevelFilter::Debug)
.with_call_target("https://postman-echo.com/post")
.init();Sourcepub fn with_epoch_ms_timestamp(self) -> CallLogger
pub fn with_epoch_ms_timestamp(self) -> CallLogger
Sets the timestamp to the number of milliseconds since the epoch.
Sourcepub fn with_epoch_us_timestamp(self) -> CallLogger
pub fn with_epoch_us_timestamp(self) -> CallLogger
Sets the timestamp to the number of microseconds since the epoch
Sourcepub fn with_utc_timestamp(self) -> CallLogger
pub fn with_utc_timestamp(self) -> CallLogger
Sets the timestamp to a the UTC timezone
Sourcepub fn with_local_timestamp(self) -> CallLogger
pub fn with_local_timestamp(self) -> CallLogger
Sets the timestamp to a the local timezone
Sourcepub fn with_formatted_timestamp<T>(
self,
timestamp_format: TimestampFormat,
format_string: T,
) -> CallLogger
pub fn with_formatted_timestamp<T>( self, timestamp_format: TimestampFormat, format_string: T, ) -> CallLogger
Formats the combined date and time as per the specified format string.
See the [crate::format::strftime] module for the supported escape sequences.
§Example
let logger = CallLogger::default()
.with_formatted_timestamp(TimestampFormat::Utc,"%H:%M:%S %d/%m/%Y %z");Sourcepub fn echo(self) -> CallLogger
pub fn echo(self) -> CallLogger
Writes each call to console before making the call, use for debugging.
Example
CallLogger::new()
.echo()
.init();Sourcepub fn to_file<P>(self, file: P) -> CallLogger
pub fn to_file<P>(self, file: P) -> CallLogger
Write the output of the call to a file
Example
CallLogger::new()
.to_file("my_app.log")
.init();Sourcepub fn format<F>(self, formatter: F) -> Self
pub fn format<F>(self, formatter: F) -> Self
Sets the formatter of this logger. The closure should accept a formatted
value for a timestamp, a message and a log record, and return a String
representation of the message that has been formatted.
Example usage:
let _ = call_logger::CallLogger::new()
.format(|timestamp, message, record| {
format!(
"{{ \"content\": \"{} [{}] {} - {}\" }}",
timestamp,
record.level(),
record.module_path().unwrap_or_default(),
message
)
})
.init();
log::info!("msg");