Struct rs_logger::Logger

source ·
pub struct Logger { /* private fields */ }
Expand description

The Logger struct.

Logger::new() returns a LoggerBuilder. Call the build method on the LoggerBuilder instance to get a Logger instance.

§Example

let mut logger = Logger::new()
                        .level(LoggingLevel::Error)
                        .filename(PathBuf::from("Logs.json"))
                        .build()
                        .unwrap();

logger.info("informative msg".to_string());
logger.critical("CRITICAL ERROR".to_string());

“informative msg” will not be printed as it is info, but level is set to error. “CRITICAL ERROR” will be printed as critical has a higher precendece than error.

If logging to a file is not needed

§Example

let mut logger = Logger::new()
                            .level(LoggingLevel::Trace)
                            .build()
                            .unwrap();

logger.info("Informative message");

The build method will not return an error here as no file is being opened. The fields for file, json, filename, and json_object will be set None.

Json is parsed differently compared to normal parsing.

An example of normal parsing. Date is in YYYY-MM-DD format. All timestamps are in local timezone based on system time.

§Example

2024-03-06 12:04:01 CRITICAL: msg
2024-03-06 12:04:01 ERROR: msg

An example of json parsing

{
  "logs": [
    {
      "date": "2024-03-06",
      "time": "12:01:53",
      "message": "msg",
      "type": "CRITICAL"
    },
    {
      "date": "2024-03-06",
      "time": "12:01:53",
      "message": "msg",
      "type": "ERROR"
    }
  ]
}

Implementations§

source§

impl Logger

source

pub fn new() -> LoggerBuilder

Constructor function. Returns a LoggerBuilder instance.

source

pub fn get_filename(&self) -> Option<&PathBuf>

Returns a reference to the filename.

It will return None if log_to_file is false

source

pub fn set_level(&mut self, level: LoggingLevel)

Set the logging level

source

pub fn critical(&mut self, msg: String)

Critical

Ignores logging level

source

pub fn error(&mut self, msg: String)

Error

Only works until and for LoggingLevel::Error.

It is ignored by LoggingLevel::Critical

source

pub fn info(&mut self, msg: String)

Info

Only works until and for LoggingLevel::Info

source

pub fn warning(&mut self, msg: String)

Warning

Only works until and for LoggingLevel::Warning

source

pub fn debug(&mut self, msg: String)

Debug

Only for LoggingLevel::Trace

Trait Implementations§

source§

impl Debug for Logger

source§

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

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

impl Drop for Logger

source§

fn drop(&mut self)

Executes the destructor for this type. 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>,

§

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

§

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.