Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reasign system property value for writer #245

Open
pab2d opened this issue Mar 29, 2022 · 2 comments
Open

Reasign system property value for writer #245

pab2d opened this issue Mar 29, 2022 · 2 comments

Comments

@pab2d
Copy link

pab2d commented Mar 29, 2022

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 pab2d added the question label Mar 29, 2022
@pmwmedia
Copy link
Member

pmwmedia commented Mar 29, 2022

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?

@pab2d
Copy link
Author

pab2d commented May 3, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants