#logging #console-log #debug-log

better-logger

Flexible sync/async logger with console and file output

3 releases

Uses new Rust 2024

new 0.1.2 Apr 23, 2025
0.1.1 Apr 21, 2025
0.1.0 Apr 20, 2025

#320 in Asynchronous

Download history 205/week @ 2025-04-16

205 downloads per month

MIT/Apache

27KB
525 lines

HOW TO USE

ONE: Settings

use better_logger::LoggerSettings;

let log_settings = LoggerSettings {
    terminal_logs: true,
    terminal_log_lvl: "trace".to_string(),
    file_logs: true,
    file_log_lvl: "error".to_string(),
    log_file_path: "/path/to/my/file.log".to_string(),
    debug_extra: true,
    async_logging: false,
};

TWO: Initialize

use better_logger::logger;

fn main() {
    logger::init(log_settings);
}

THREE: Log

use better_logger::logger::*;

fn my_function() {
    let debug_msg: &str = "world";
    let debugx_msg: String = format!(", world");
    let error: &str = r#""failed""#;

    trace!("hello");
    debug!("{}", debug_msg);
    debugx!("hello{}", debugx_msg);
    info!("hello message");
    warn!("world message");
    error!(r#""hello" "world" {}"#, error);
}
SETTING DESCRIPTION
terminal_logs Log to terminal
terminal_log_lvl Minimum level to display
file_logs Log to file
file_log_lvl Minimum level to write
log_file_path Path to log file
debug_extra Show debugx! logs
async_logging Enable async logging

INFORMATION

  • Console logging uses env_logger
  • File logging uses the same format as the console logs
  • trace! -> (debug!, debugx!) -> info! -> warn! -> error!
  • better-logger will automatically create the path and file if not already created
  • File logs are overwritten not appended
  • Async logging uses a “fire and forget” model:
    • It spawns a new async task on the current Tokio runtime for each message
  • All macros use format! under the hood, any string-like type is accepted
  • Log messages routed through env_logger are not written to the file
    • Only messages emitted via better-logger are persisted to the log file

Settings Alternate Namespace

Same exact settings, just a semantic difference

use better_logger::settings::Settings;

let log_settings = Settings {
    /* Same as above */
};

What is DEBUGX?

It is just a second debug, the debugx!() logs will be labeled as DEBUG when they print

Why would I want to use DEBUGX?

Let’s say you’re in development, so you want to see all your debug logs. However, some of your debug logs are massive and clutter your terminal.
You can mark those verbose logs as debugx!() and set debug_extra = false to hid them.
Later, if you're troubleshooting or need to view them, set debug_extra = true, and see your extra debug logs!

Future Plans

  • Append Setting
  • WASM Logging
  • Network Logging

Dependencies

~8–16MB
~187K SLoC