Function bones_framework::logging::setup_logging

source ·
pub fn setup_logging(settings: LogSettings) -> Option<LogFileGuard>
Expand description

Setup the global tracing subscriber, add hook for tracing panics, and optionally enable logging to file system.

This function sets panic hook to call tracing_panic_hook, and then call previous hook. This writes panics to tracing subscribers. This is helpful for recording panics when logging to file system.

if LogFileConfig was provided in settings and is supported on this platform (cannot log to file system on wasm), this function will return a LogFileGuard. This must be kept alive for duration of process to capture all logs, see LogFileGuard docs.

Examples below show direct usage and short-hand with setup_logs macro.

§Examples

§Default without logging to file

use bones_framework::logging::prelude::*;
fn main() {
    let _log_guard = bones_framework::logging::setup_logging(LogSettings::default());
}

or

use bones_framework::logging::prelude::*;
fn main() {
    setup_logs!();
}

§Enable logging including logging to files:

use bones_framework::prelude::*;
fn main() {
    let log_file =
        match LogPath::find_app_data_dir(("org", "fishfolk", "jumpy")) {
            Ok(log_path) => Some(LogFileConfig {
                log_path,
                rotation: LogFileRotation::Daily,
                file_name_prefix: "Jumpy.log".to_string(),
                max_log_files: Some(7),
            }),
            Err(err) => {
                // Cannot use error! macro as logging not configured yet.
                eprintln!("Failed to configure file logging: {err}");
                None
            }
        };

    // _log_guard will be dropped when main exits, remains alive for duration of program.
    let _log_guard = bones_framework::logging::setup_logging(LogSettings {
        log_file,
        ..default()
    });
}

or logging to file with defaults:

use bones_framework::logging::prelude::*;
fn main() {
    let _log_guard = bones_framework::logging::setup_logging_default(("org", "fishfolk", "jumpy"));
}

same with macros::setup_logs macro:

use bones_framework::prelude::*;
fn main() {
    setup_logs!("org", "fishfolk", "jumpy");
}