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
impl Logger
Sourcepub fn new(filename: &str) -> Result<Logger>
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.
Sourcepub fn write(
&mut self,
level: LogLevel,
source_filename: &str,
line: u32,
args: Arguments<'_>,
) -> Result<()>
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.