Struct Logger

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

This is the work-horse, providing the primary methods of the crate.

Implementations§

Source§

impl Logger

Source

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

Returns a LoggerBuilder for further configuring.

§Examples
extern crate flogging;
use flogging::*;

let mut log = Logger::builder(module_path!())
    .add_console_handler()
    .build();
Source

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

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

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

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 using module_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.

Source

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  ] Entry
Source

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

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  ] Return
Source

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

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 using std::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.
Source

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

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

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

pub fn fn_name(&self) -> String

Get the current function/method name.

Source

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

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

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

pub fn level(&self) -> &Level

Obtain the current default logging level for this Log instance.

Source

pub fn reset_level(&mut self) -> &mut Self

Reset this Logger instance’s default logging level.

Returns itself for chaining purposes.

See Level

Source

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.

Source

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.

Source

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

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

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

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.

Trait Implementations§

Source§

impl Display for Logger

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !Freeze for Logger

§

impl !RefUnwindSafe for Logger

§

impl Send for Logger

§

impl !Sync for Logger

§

impl Unpin for Logger

§

impl !UnwindSafe for Logger

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.