donnie4w / tklog

lightweight and efficient rust structured log library with support for log levels, file segmentation, compressed archiving
https://crates.io/crates/tklog
Apache License 2.0
56 stars 3 forks source link

添加功能,可以按等级手动过滤依赖库的日志信息 #2

Open miaomiao1992 opened 3 months ago

miaomiao1992 commented 3 months ago

如果我想将项目里面的sea-orm日志级别按info过滤,把hyper日志完全过滤,能否开发filter_module功能?

donnie4w commented 3 months ago

如果需要在不同模块中,设置独立规则的日志实例,可以用多实例的方式打印日志,针对模块设置相应的的规则,这样可能比较简单灵活,如

 let mut ormlog = tklog::Logger::new();
    ormlog.set_console(false)
        .set_level(LEVEL::Info) //定义日志级别为Info
        .set_cutmode_by_time("ormlog.log", MODE::DAY, 10, true);

    let mut logger = Arc::clone(&Arc::new(Mutex::new(ormlog)));
    let lorm = logger.borrow_mut();

    debugs!(lorm, "debugs>>>>", "BBBBBBBBB", 1, 2, 3, 5);
    infos!(lorm, "infos>>>>", "CCCCCCCCC", 1, 2, 3, 5);

     let mut hylog = tklog::Logger::new();
     hylog.set_console(false)
        .set_level(LEVEL::Info) //定义日志级别为Info
        .set_cutmode_by_time("hylog.log", MODE::DAY, 10, true);

    let mut logger = Arc::clone(&Arc::new(Mutex::new(hylog)));
    let lhy = logger.borrow_mut();

    debugs!(lhy, "debugs>>>>", "BBBBBBBBB", 1, 2, 3, 5);
    infos!(lhy, "infos>>>>", "CCCCCCCCC", 1, 2, 3, 5);

可以针对 ormlog 与 hylog 设置不同的参数,也可以动态修改,实现项目运行时修改日志打印

miaomiao1992 commented 2 months ago

就差这个功能,就能上生产环境了

donnie4w commented 2 months ago

@miaomiao1992 多实例是否适用于你的应用场景? 或者你可以叙述更具体一点,可能是我错误理解你的问题

miaomiao1992 commented 2 months ago

@miaomiao1992 多实例是否适用于你的应用场景? 或者你可以叙述更具体一点,可能是我错误理解你的问题

多实例日志对业务代码入侵太严重了,而且arc也影响性能,如果能全局设置指定模块的日志等级,当然会更好

donnie4w commented 2 months ago

@miaomiao1992

donnie4w commented 2 months ago

@miaomiao1992 Rust高性能日志库tklog0.0.8—支持mod设置参数 文章介绍了mod设置的方法

miaomiao1992 commented 2 months ago

大佬,啥原因呢,sqlx-mysql和sqlx都试过了,无法过滤啊

 use tklog::Format;
    use tklog::LEVEL;
    use tklog::MODE;
    use tklog::LogOption;
    use tklog::handle::FileTimeMode;

    tklog::LOG.set_console(true)
    .set_level(LEVEL::Debug)
    .set_format(Format::LevelFlag | Format::Time |Format::Date| Format::LongFileName)
    .set_cutmode_by_time("tklog.log", MODE::DAY, 10, true)  
    .set_formatter("{time} {level} {file} => {message}\n")
    .set_mod_option("sea_orm",LogOption{level:Some(LEVEL::Info),console: Some(true),format:Some(Format::LevelFlag | Format::Time |Format::Date| Format::LongFileName),formatter:Some("{time} {level} {file} => {message}\n".to_string()),fileoption: Some(Box::new(FileTimeMode::new("sea-orm.log", tklog::MODE::DAY, 10,true)))})
    .set_mod_option("sqlx_mysql",LogOption{level:None,console: Some(false),format:None,formatter:None,fileoption: None})
    .uselog();  

222

donnie4w commented 2 months ago

@miaomiao1992 可能是 mod名问题。可以在目标mod中(即sqlx_mysql,sea_orm 中)调用 module_path!(),打印一下完整的mod名。

miaomiao1992 commented 2 months ago

大佬,我发现模块路径必须是全程路径,不能简写,比如以下是路径全称,可以屏蔽控制台输出。

.set_mod_option(
            "sqlx_mysql::connection::tls",
            LogOption {
                level: Some(LEVEL::Info),
                console: Some(false),
                format: None,
                formatter: None,
                fileoption: None,
            },
        )

sqlx_mysql::*,如果这样写也能过滤,是不是会增加日志框架复杂度,影响性能,我看到模块过滤式通过HashMap处理的,无法进行模糊过滤对吧?

donnie4w commented 2 months ago

@miaomiao1992 sqlx_mysql::,这种匹配方式的确会对性能有一点影响。 初看起来,要实现它可能与http框架的url路由有些类似。可能不是简单的Map的键匹配。比如 A::B::
A::B::C:: A:: 这些模块对于 A::B::C::D 模块都是适用的,不过要逐层对应。

具体对性能的影响还需要 后期版本再做优化和测试才能确定。