首页 > 解决方案 > tinylog2 - 在运行期间重新配置

问题描述

在我已经记录了一些行之后,有没有办法重新配置 tinylog2 属性?我想在运行期间更改日志文件名,当用户更改选择(使用不同的文件)时,无需重新启动我的程序。

我的代码只在第一次工作:

private void initLogger(String fileName) {
//        log to file
        Configuration.set("writer","file");
//        set log file name
        if (inFileCheckBox.isSelected()){  // log file name is working file name
            Configuration.set("writer.file",fileName);
        }else{ // log file name by month (MM-YYYY)
            Configuration.set("writer.file", new SimpleDateFormat("MM-yyyy").format(new Date()));
        }
        Logger.info("yow it's : {}", fileName);
    }

在更改参数之前,我尝试手动关闭它,但没有帮助:

ProviderRegistry.getLoggingProvider().shutdown();

这是我第二次运行该方法时遇到的错误:

Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Configuration cannot be changed after applying to tinylog

谢谢!

标签: tinylog

解决方案


tinylog 的本机日志记录提供程序不支持在发布第一个日志条目后更改配置。但是,您可以通过扩展或包装 tinylog 的本机日志记录提供程序TinylogLoggingProvider来创建自定义日志记录实现,并自行添加所需的运行时更改逻辑。

您可以在 tinylog 网站上找到如何注册和使用自定义日志记录提供程序的文档:https ://tinylog.org/v2/extending/#custom-logging-provider


推荐阅读