pub struct Logger { /* private fields */ }Expand description
This is the work-horse, providing the primary methods of the crate.
Implementations§
Source§impl Logger
impl Logger
Sourcepub fn builder(mod_path: &str) -> LoggerBuilder
pub fn builder(mod_path: &str) -> LoggerBuilder
Create a new Logger instance.
Logging level is set to it’s default setting (INFO).
No handlers are set. Use the various methods of
LoggerBuilder to configure the new Logger.
§Parameters
mod_path- The module path. Can be set with:module_path!()
Returns a LoggerBuilder for further configuring.
§Examples
extern crate flogging;
use flogging::*;
let mut log = Logger::builder(module_path!())
.add_console_handler()
.build();Sourcepub fn config(&mut self, msg: &str)
pub fn config(&mut self, msg: &str)
Log a CONFIG message.
If the logger is currently enabled for the CONFIG message level then the given message is forwarded to all the registered output Handler objects.
§Parameters
msg- The string message.
§Examples
extern crate flogging;
use flogging::*;
let mut log = Logger::console_logger(module_path!());
log.set_level(Level::CONFIG);
log.config("Some text to store.");Sourcepub fn console_logger(mod_path: &str) -> Logger
pub fn console_logger(mod_path: &str) -> Logger
Create new Logger instance, with a ConsoleHandler.
Logging level is set to it’s default setting (INFO).
§Parameters
mod_path- The module path. Suggest usingmodule_path!().
Returns a configured Logger.
§Examples
extern crate flogging;
use flogging::*;
let mut log = Logger::console_logger(module_path!());
log.set_fn_name("main");
log.warning("Don't over do it.");Output:
|flogging->main| [WARNING] Don't over do it.Sourcepub fn custom_logger(
mod_path: &str,
label: &str,
custom: Box<dyn HandlerTrait>,
) -> Logger
pub fn custom_logger( mod_path: &str, label: &str, custom: Box<dyn HandlerTrait>, ) -> Logger
Create new Logger instance, with a custom handler.
§Parameters
mod_path- The module path. Suggest usingmodule_path!().label- Unique label for this custom handler.custom- The boxed custom handler.
Returns a configured Logger.
§Examples
extern crate flogging;
use flogging::*;
let mut log = Logger::custom_logger(
module_path!(),
"MockHandler",
Box::new(MockHandler::create("Some text").unwrap()),
);
log.set_fn_name("main");
log.warning("Don't over do it.");MockHandler doesn’t publish anything, as publish() is a NoOp method.
It is used here to make the example work.
However, it is expected that your custom handler will do a little more.
Sourcepub fn entering(&mut self)
pub fn entering(&mut self)
Log a method entry.
This is a convenience method that can be used to log entry to a method.
A LogEntry with message “Entry” and log level FINER, is logged.
§Examples
mod my_mod {
extern crate flogging;
use flogging::*;
use std::cell::{LazyCell, RefCell};
// Setting up the module level logger.
const LOGGER: LazyCell<RefCell<Logger>> = LazyCell::new(|| {
RefCell::new({
Logger::builder(module_path!())
.add_console_handler()
.set_level(Level::FINEST)
.build()
})
});
pub fn my_func(data: &str) {
let binding = LOGGER;
let mut log = binding.borrow_mut();
log.set_fn_name("my_func");
log.entering();
}
}
fn main() {
let data = "Some data";
my_mod::my_func(data);
}Output:
|flogging::my_mod->my_func| [FINER ] EntrySourcepub fn entering_with(&mut self, msg: &str)
pub fn entering_with(&mut self, msg: &str)
Log a method entry.
This is a convenience method that can be used to log entry to a method.
A LogEntry with message “Entry” and log level FINER, is logged.
§Parameters
msg- The string message.
§Examples
mod my_mod {
extern crate flogging;
use flogging::*;
use std::cell::{LazyCell, RefCell};
// Setting up the module level logger.
const LOGGER: LazyCell<RefCell<Logger>> = LazyCell::new(|| {
RefCell::new({
Logger::builder(module_path!())
.add_console_handler()
.set_level(Level::FINEST)
.build()
})
});
pub fn my_func(data: &str) {
let binding = LOGGER;
let mut log = binding.borrow_mut();
log.set_fn_name("my_func");
log.entering_with(&format!("data: \"{data}\""));
}
}
fn main() {
let data = "Some data";
my_mod::my_func(data);
}Output:
|flogging::my_mod->my_func| [FINER ] Entry: (data: "Some data")Sourcepub fn exiting(&mut self)
pub fn exiting(&mut self)
Log a method return.
This is a convenience method that can be used to log returning from a method.
A LogEntry with message “Return” and log level FINER, is logged.
§Examples
mod my_mod {
extern crate flogging;
use flogging::*;
use std::cell::{LazyCell, RefCell};
// Setting up the module level logger.
const LOGGER: LazyCell<RefCell<Logger>> = LazyCell::new(|| {
RefCell::new({
Logger::builder(module_path!())
.add_console_handler()
.set_level(Level::FINEST)
.build()
})
});
pub fn my_func(data: &str) {
let binding = LOGGER;
let mut log = binding.borrow_mut();
log.set_fn_name("my_func");
log.exiting();
}
}
fn main() {
let data = "Some data";
my_mod::my_func(data);
}Output:
|flogging::my_mod->my_func| [FINER ] ReturnSourcepub fn exiting_with(&mut self, msg: &str)
pub fn exiting_with(&mut self, msg: &str)
Log a method return.
This is a convenience method that can be used to log returning from a method.
A LogEntry with message “Return” and log level FINER, is logged.
§Parameters
msg- The string message.
§Examples
mod my_mod {
extern crate flogging;
use flogging::*;
use std::cell::{LazyCell, RefCell};
// Setting up the module level logger.
const LOGGER: LazyCell<RefCell<Logger>> = LazyCell::new(|| {
RefCell::new({
Logger::builder(module_path!())
.add_console_handler()
.set_level(Level::FINEST)
.build()
})
});
pub fn my_func(data: &str) -> bool {
let binding = LOGGER;
let mut log = binding.borrow_mut();
log.set_fn_name("my_func");
let rtn = true;
log.exiting_with(&format!("rtn: {rtn}"));
rtn
}
}
fn main() {
let data = "Some data";
my_mod::my_func(data);
}Output:
|flogging::my_mod->my_func| [FINER ] Return: (rtn: true)Sourcepub fn file_logger(mod_path: &str, filename: &str) -> Logger
pub fn file_logger(mod_path: &str, filename: &str) -> Logger
Create new Logger instance, with a FileHandler.
Logging level is set to it’s default setting (INFO).
§Parameters
mod_path- The module path. Suggest usingstd::module_path.filename- The name of the log file to use. Will be created if it doesn’t exist.
Returns a configured Logger.
§Examples
extern crate flogging;
use flogging::Logger;
let mut log = Logger::file_logger(module_path!(), "test.log");
log.set_fn_name("main");
log.info("Some text to store.");Output:
2025-07-18T12:14:47.322720683+08:00 |flogging->main| [INFO ] Some text to store.Sourcepub fn fine(&mut self, msg: &str)
pub fn fine(&mut self, msg: &str)
Log a FINE message.
If the logger is currently enabled for the FINE message level then the given message is forwarded to all the registered output Handler objects.
§Parameters
msg- The string message.
§Examples
extern crate flogging;
use flogging::*;
let mut log = Logger::console_logger(module_path!());
log.set_level(Level::FINEST);
log.set_fn_name("main");
log.fine("Some text to store.");Output:
|flogging->main| [FINE ] Some text to store.Sourcepub fn finer(&mut self, msg: &str)
pub fn finer(&mut self, msg: &str)
Log a FINER message.
If the logger is currently enabled for the FINER message level then the given message is forwarded to all the registered output Handler objects.
§Parameters
msg- The string message.
§Examples
extern crate flogging;
use flogging::*;
let mut log = Logger::console_logger(module_path!());
log.set_level(Level::FINEST);
log.set_fn_name("main");
log.finer("Some text to store.");Output:
|flogging->main| [FINER ] Some text to store.Sourcepub fn finest(&mut self, msg: &str)
pub fn finest(&mut self, msg: &str)
Log a FINEST message.
If the logger is currently enabled for the FINEST message level then the given message is forwarded to all the registered output Handler objects.
§Parameters
msg- The string message.
§Examples
extern crate flogging;
use flogging::*;
let mut log = Logger::console_logger(module_path!());
log.set_level(Level::FINEST);
log.set_fn_name("main");
log.finest("Some text to store.");Output:
|flogging->main| [FINEST ] Some text to store.Sourcepub fn get_handler(
&mut self,
handler: Handler,
) -> Option<Box<&mut dyn HandlerTrait>>
pub fn get_handler( &mut self, handler: Handler, ) -> Option<Box<&mut dyn HandlerTrait>>
Get required Handler.
§Parameters
handler- The enum of the required handler.
Returns Some boxed handler, or None.
§Examples
extern crate flogging;
use flogging::*;
let mut log = Logger::string_logger(module_path!());
log.set_fn_name("get_handler");
log.info("Some text to store.");
let h = log.get_handler(Handler::String).unwrap();
println!("{h}");Sourcepub fn has_handler(&self, handler: Handler) -> bool
pub fn has_handler(&self, handler: Handler) -> bool
Check if the required Handler has been added to this Logger.
§Parameters
handler- The enum of the required handler.
Returns true if it exists, false otherwise.
§Examples
extern crate flogging;
use flogging::*;
let mut log = Logger::string_logger(module_path!());
log.info("Some text to store.");
println!("This logger has a 'StringHandler': {}", log.has_handler(Handler::String));Sourcepub fn info(&mut self, msg: &str)
pub fn info(&mut self, msg: &str)
Log a INFO message.
If the logger is currently enabled for the INFO message level then the given message is forwarded to all the registered output Handler objects.
§Parameters
msg- The string message.
§Examples
extern crate flogging;
use flogging::*;
let mut log = Logger::console_logger(module_path!());
log.set_level(Level::FINEST);
log.set_fn_name("main");
log.info("Some text to store.");Output:
|flogging->main| [INFO ] Some text to store.Sourcepub fn reset_level(&mut self) -> &mut Self
pub fn reset_level(&mut self) -> &mut Self
Sourcepub fn set_fn_name(&mut self, fn_name: &str) -> &mut Self
pub fn set_fn_name(&mut self, fn_name: &str) -> &mut Self
Set the current function/method name.
§Parameters
fn_name- The name of the function/method in which you are logging.
Returns itself for chaining purposes.
Sourcepub fn set_level(&mut self, level: Level) -> &mut Self
pub fn set_level(&mut self, level: Level) -> &mut Self
Set default logging level for this Log instance.
§Parameters
level- The new logging level to set.
Returns itself for chaining purposes.
Sourcepub fn severe(&mut self, msg: &str)
pub fn severe(&mut self, msg: &str)
Log a SEVERE message.
If the logger is currently enabled for the SEVERE message level then the given message is forwarded to all the registered output Handler objects.
§Parameters
msg- The string message.
§Examples
extern crate flogging;
use flogging::*;
let mut log = Logger::console_logger(module_path!());
log.set_level(Level::FINEST);
log.set_fn_name("main");
log.severe("Some text to store.");Output:
|flogging->main| [SEVERE ] Some text to store.Sourcepub fn string_logger(mod_path: &str) -> Logger
pub fn string_logger(mod_path: &str) -> Logger
Create new Logger instance, with a ConsoleHandler.
Logging level is set to it’s default setting (INFO).
I expect this will be primarily used during unit testing of the logging output. Though, any requirement to pass-on the log entry, perhaps for further processing, would also be a valid use case.
§Parameters
mod_path- The module path. Suggest usingmodule_path!().
Returns a configured Logger.
§Examples
extern crate flogging;
use flogging::*;
let mut log = Logger::string_logger(module_path!());
log.set_fn_name("main");
log.warning("Don't over do it.");
let log_str = log.get_handler(Handler::String).unwrap().get_log();
println!("{log_str}");Output:
|flogging->main| [WARNING] Don't over do it.Sourcepub fn warning(&mut self, msg: &str)
pub fn warning(&mut self, msg: &str)
Log a WARNING message.
If the logger is currently enabled for the WARNING message level then the given message is forwarded to all the registered output Handler objects.
§Parameters
msg- The string message.
§Examples
extern crate flogging;
use flogging::*;
let mut log = Logger::console_logger(module_path!());
log.set_level(Level::FINEST);
log.set_fn_name("main");
log.warning("Some text to store.");Output:
|flogging->main| [WARNING] Some text to store.