tinylog icon indicating copy to clipboard operation
tinylog copied to clipboard

Reasign system property value for writer

Open pab2d opened this issue 4 years ago • 2 comments

Hi,

in my case, i have to reasign a system property value on runtime and i would like to see the new value in my log file. Is there a way to achive that easily?

tinylog.properties (System property -> app.namespace)

writer2			= rolling file
writer2.level		= info
writer2.format		= {date} {level} [ns: #{app.namespace}] [th: {thread}] {class}.{method}:\n{message}
writer2.file	...

Custom logger

public class MyLogger {

    private static final LoggingProvider provider = ProviderRegistry.getLoggingProvider();

    private static final int minimumLevel = provider.getMinimumLevel(null).ordinal();
    private static final boolean infoEnabled = minimumLevel <= Level.INFO.ordinal();
    private static final boolean errorEnabled = minimumLevel <= Level.ERROR.ordinal();

    public static void info(String message) {
        if (infoEnabled) {
            provider.log(2, null, Level.INFO, null, null, message);
        }
    }

    public static void error(Throwable exception) {
        if (errorEnabled) {
            provider.log(2, null, Level.ERROR, exception, null, null);
        }
    }

}

The default configuration in ProviderRegistry.getLoggingProvider(); loads all properies once and does a mapping (tinylog / system properties) -> so my logging format is fix.

pab2d avatar Mar 29 '22 13:03 pab2d

Do you need the updated system property only in the format pattern? If yes, this could be solved by implementing a new token.

The syntax could be for example: writer.format = {dynamic-system-property: app.namespace}. A good example for a token implementation is PlainTextToken.java. In your use case, you could resolve the system property dynamically in render() and apply(). The name of the system property could be passed as string in the constructor. Finally, the new token has to be registered in FormatPatternParser.createPlainToken().

If you can implement such token and provide as pull request, I could publish it together with tinylog 2.5.

PS: Why do you use a custom logger?

pmwmedia avatar Mar 29 '22 19:03 pmwmedia

Yes i do. Thank you for your idee. My custom logger does an user and a technical logging

pab2d avatar May 03 '22 06:05 pab2d