Skip to main content

Logger

Struct Logger 

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

A simple logger that writes log entries to a specified file. Each log entry includes a log level, timestamp, source filename, line number, and message. The logger supports four log levels: FATAL, WARNING, INFO, and DEBUG.

§Example

 use judger::Logger;
 use std::fmt::Arguments;
 use judger::LogLevel;
 let mut logger = Logger::new("judger.log").expect("Failed to create logger");
 logger.write(LogLevel::Info, file!(), line!(), format_args!("This is an info message")).expect("Failed to write log");

§Errors

The new method returns an io::Error if the log file cannot be created or opened. The write method returns an io::Error if writing to the log file fails or if an invalid log level is provided.

Implementations§

Source§

impl Logger

Source

pub fn new(filename: &str) -> Result<Logger>

Creates a new logger that writes to the specified file. If the file does not exist, it will be created. If it exists, logs will be appended.

§Errors

Returns an io::Error if the file cannot be created or opened.

§Example
 use judger::Logger;
 let logger = Logger::new("judger.log").expect("Failed to create logger");
§Arguments
  • filename - The path to the log file.
§Returns

A Result containing the Logger or an io::Error.

Source

pub fn write( &mut self, level: LogLevel, source_filename: &str, line: u32, args: Arguments<'_>, ) -> Result<()>

Writes a log entry to the log file with the specified level, source filename, line number, and message.

§Errors

Returns an io::Error if writing to the log file fails or if an invalid log level is provided.

§Example
 use judger::Logger;
 use std::fmt::Arguments;
 use judger::LogLevel;
 let mut logger = Logger::new("judger.log").expect("Failed to create logger");
 logger.write(LogLevel::Info, file!(), line!(), format_args!("This is an info message")).expect("Failed to write log");
§Arguments
  • level - The log level of the entry.
  • source_filename - The source filename where the log entry is generated.
  • line - The line number in the source file.
  • args - The formatted message to log.
§Returns

A Result indicating success or containing an io::Error.

Trait Implementations§

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

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.