[go: up one dir, main page]

CallLogger

Struct CallLogger 

Source
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

Source

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();
Source

pub fn with_level(self, level: LevelFilter) -> CallLogger

The maximum log level that would be logged.

§Example
CallLogger::new()
    .with_level(LevelFilter::Error)
    .init();
Source

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");
Source

pub fn with_call_target<T>(self, call_target: T) -> CallLogger
where T: Into<String>,

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();
Source

pub fn with_epoch_ms_timestamp(self) -> CallLogger

Sets the timestamp to the number of milliseconds since the epoch.

Source

pub fn with_epoch_us_timestamp(self) -> CallLogger

Sets the timestamp to the number of microseconds since the epoch

Source

pub fn with_utc_timestamp(self) -> CallLogger

Sets the timestamp to a the UTC timezone

Source

pub fn with_local_timestamp(self) -> CallLogger

Sets the timestamp to a the local timezone

Source

pub fn with_formatted_timestamp<T>( self, timestamp_format: TimestampFormat, format_string: T, ) -> CallLogger
where T: Into<String>,

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");
Source

pub fn echo(self) -> CallLogger

Writes each call to console before making the call, use for debugging.

Example

CallLogger::new()
    .echo()
    .init();
Source

pub fn to_file<P>(self, file: P) -> CallLogger
where P: AsRef<Path>,

Write the output of the call to a file

Example

CallLogger::new()
    .to_file("my_app.log")
    .init();
Source

pub fn format<F>(self, formatter: F) -> Self
where F: Fn(String, &Arguments<'_>, &Record<'_>) -> String + Sync + Send + 'static,

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");
Source

pub fn init(self) -> Result<(), SetLoggerError>

This needs to be called after the builder has set up the logger.

§Example
CallLogger::new().init();

Trait Implementations§

Source§

impl Debug for CallLogger

Source§

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

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

impl Default for CallLogger

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Log for CallLogger

Source§

fn enabled(&self, metadata: &Metadata<'_>) -> bool

Determines if a log message with the specified metadata would be logged. Read more
Source§

fn log(&self, record: &Record<'_>)

Logs the Record. Read more
Source§

fn flush(&self)

Flushes any buffered records. Read more

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, 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.