#logger #performance #level #ultra-simple #change #set #focus

yanked simplest-logger

Ultra-simple logger with a focus on performance. No bells or whistles, just a fast simple no-fuss logger.

1.0.0 Oct 24, 2023
0.2.0 Oct 24, 2023

#87 in #focus

MIT/Apache

11KB
132 lines

SimplestLogger

Inspired by SimpleLogger, this is a crate for ultra-simple logging. I created it because there were no crates that focused on being ultra lightweight and no-effort to use.

# Cargo.toml

[dependencies]
simplest-logger = "1.0.0"
fn main()
{
	SimplestLogger::initalize(); // info by default
	// SimplestLogger::set_level(LevelFilter::LEVEL) to change

	// your code
}

Roadmap

  • impl Log for SimplestLogger so it works with the log macros.
  • Unit testing
    • Errors
    • Panics (only one!)
    • Benchmarks
  • Integration testing (with tests/)
    • Errors
    • Panics (only one!)
    • Benchmarks
  • Attribute feature to change log level per-function #[log(Level)]. (Maybe not sicne this may hurt performance)

Performance

TODO (tl;dr: super fast!)


lib.rs:

An ultra simple no-configuration logger with colored output and zero dependencies.Just add it to dependencies and initalize it as soon as possible.

use log::*;
use SimplestLogger;

fn main
{
SimplestLogger::initalize(LevelFilter::Info);

// your code!
}

SimplestLogger gives lots of line length for longer messages, and uses easily-distingusishable but theme respecting (ANSI) terminal colors.

We also have some constants you can use to more easily line things up.

In your Cargo.toml you will need:

log = { version = "0.4", features = ["max_level_trace", "release_max_level_warn"] }

Those feature flags aren't really "features", what they do is allows all logs in debug, but tells the rust compiler to remove any and all trace! and debug! macros invocations at compile time, meaning you have exactly zero overhead or performance loss for those macros: only info, warning, and error have any processing power required in production.

In fact, if you want you can even do release_max_level_off and get no cost for any logs in production! Useful for embedded systems and tight storage constraints--since you can omit the entire log and SimplestLogger crates.

Initalize your logger. Note that any log macros used before this is called will simply be ignored, and that only one logger may be initalized and attempting to initalize a logger when there already is one will cause the program to panic.

It is best practice to make this line 1 of your main function, and use it nowhere else so it is easy to know which cases need to be removed.

Note that this function is always inlined as it must only be called once, so this micro-optimisation saves you some clock cycles when the rust compiler would have otherwise decided not to inline this function.

Initalizes to info by default. Use SimpelstLogger::set_level() to change the level.

Clean interface to set the log level. You can also fdo this directly with log::set_max_level(LevelFilter::YourLevel). This function has the inline attribute so it should cause no performance loss as opposed to doing it directly.

this function is never called. It is just a requirement for the implementation.

Dependencies

~87KB