#session #logging #log-messages #thread #group #system #multiple

session_log

A simple session log library for Rust

25个版本

0.2.7 2024年8月24日
0.2.6 2024年6月12日
0.1.16 2024年6月6日
0.1.11 2024年5月30日

#20 in #log-messages

Download history 601/week @ 2024-05-04 37/week @ 2024-05-11 526/week @ 2024-05-18 193/week @ 2024-05-25 922/week @ 2024-06-01 458/week @ 2024-06-08 54/week @ 2024-06-15 1/week @ 2024-06-29 18/week @ 2024-07-06

1,957 monthly downloads

MIT license

38KB
751 lines

Session_log

This crate tries to provide a easy to use logging system that can group / nest logs in a session like manner. This is useful for systems that having multiple threads and you want to group logs by session.

使用方法

对于大多数基本使用,你只需要从 session_log::prelude 导入所有内容,然后就可以开始了。

use session_log::prelude::*;

fn main() {
    // Create a new logging entry point with default settings
    let logger = Logger::new("main");

    // Log a message directly to the logger
    logger.info("Hello, world!");

    // Create a new session
    {
        let session = logger.session();

        // Log some messages in the session
        session.info("Hello, session!");

        // Create a new session in the session
        {
            let session = session.session();

            // Log some messages in the session
            session.info("Hello, nested session!");
        }

        // Log some messages in the session
        session.info("Goodbye, session!");
    }
}

创建日志记录器的成本非常低,本质上它只是一个围绕 String 的全局映射表中的键的包装器。

use session_log::prelude::*;

fn main() {
    // Create a new logging entry point with default settings
    let logger1 = Logger::new("main");
    logger1.set_level(Level::Verbose);

    // Create a new logger with the same name
    let logger2 = Logger::new("main");

    // They are the same logger
    assert_eq!(
        logger1.get_level(),
        logger2.get_level());
}

每个入口点都有自己的日志级别,输出目录可以在初始化或运行时设置。

use session_log::{
    prelude::*,
    Level,
};

fn main() {
    { // Create a new logger with custom settings
        let logger1 = Logger::with_options("1",
            Level::Verbose, "logs/1").unwrap();

        // If you try to use `with_options` to create a logger with the same name
        // but different settings, it will return an error
        assert_eq!(
            Logger::with_options("1",
                Level::Info, "logs/1").is_err(),
            true);

        // Same options will not return an error
        assert_eq!(
            Logger::with_options("1",
                Level::Verbose, "logs/1").is_err(),
            false);
    }

    { // Create a new logger with custom settings that may be omitted
        // If "2" is never been created before, it will use the given options
        let logger2 = Logger::try_with_options("2",
            Level::Verbose, "logs/2");

        // If "2" is already created, it will omit the given options
        let logger2 = Logger::try_with_options("2",
            Level::Verbose, "logs/2");
    }

    { // Do the setting after initialization
        // This will create a new logger with default settings or retrieve the existing one
        // regardless of the settings. Default setting: (Level::Info, "logs")
        let logger2 = Logger::new("3");

        // Configure the logger afterward
        logger2.set_level(Level::Verbose);
        logger2.set_directory("logs/3");
    }
}

依赖

~1.5MB
~22K SLoC