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 的本机日志记录提供程序 TinylogLoggingProvider 并自行添加所需的运行时更改逻辑来创建自定义日志记录实现。

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