1

is there a way to reconfigure tinylog2 properties after I already logged some lines ? I want to change the log file name during run, when the user changes selections (working with diferent files) ,without restart my program.

my code works only first time :

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);
    }

I tried to shutdown it manualy before I change parameter but it didn't help :

ProviderRegistry.getLoggingProvider().shutdown();

this is the error I get second time I run the method :

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

thanks!

orkiman
  • 11
  • 1

1 Answers1

0

tinylog's native logging provider does not support configuration changes after issuing the first log entry. However, you could create your custom logging implementation by extending or wrapping tinylog's native logging provider TinylogLoggingProvider and add your required runtime changing logic by yourself.

You can find a documentation how to register and use a custom logging provider on the tinylog website: https://tinylog.org/v2/extending/#custom-logging-provider

Martin
  • 598
  • 1
  • 4
  • 27
  • Understand since v2, the configuration is intentionally made immutable. However, what is the recommended way to change log level at runtime? E.g. I have many Docker container instances running in k8s cluster. At runtime I see some error logs and want to turn on Debug/Trace level. By default, do I have to rebuild the entire Java app and Docker image and redeploy to the running cluster? – q3769 Jul 24 '21 at 22:27
  • The recommend way is using a custom logging provider that extends the default TinylogLoggingProvider. Here is a Kotlin example: https://github.com/tinylog-org/tinylog/issues/215#issuecomment-870418371. Only a few lines of code are required. – Martin Jul 25 '21 at 23:06