-
-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Android: possibility to change severity Level at runtime ? #102
Comments
You could write your custom logging provider. It is not difficult. Here is an example that already almost does what you want: #100 (comment) |
Thanks for your quick answer. In Android I can't access the Map map = (Map) method.invoke(null);
map.put("level", newLogLevel);
Configuration.replace(map); But for the part registering provider as a service I don't know how I can manage this on Android. I don't understand how you define that my custom profider is named "reconfigurable" to use it in the |
Yes, this should work!
tinylog reads all
The mechanism is explained in detail on the basis of custom writers and policies in https://tinylog.org/v2/extending/ . However, custom logging providers work in the same way. |
Hi, thanks a lot for yours quick answers. I'm almost done. One problem since I use my custom LoggingProvider is that the log format Format mask:
Output with my custom provider:
Output with default profider:
Maybe I have missed something, here is my complete custom Provider class: public class ReconfigurableLoggingProvider implements LoggingProvider {
private TinylogLoggingProvider realProvider = new TinylogLoggingProvider();
private String logLevel;
public void reload(String filePath) throws InterruptedException, ReflectiveOperationException {
realProvider.shutdown();
Method method = Configuration.class.getDeclaredMethod("load");
method.setAccessible(true);
Map map = (Map) method.invoke(null);
map.put("level", logLevel.toLowerCase());
map.put("writer2.file", filePath);
Configuration.replace(map);
realProvider = new TinylogLoggingProvider();
}
@Override
public ContextProvider getContextProvider() {
return realProvider.getContextProvider();
}
@Override
public Level getMinimumLevel() {
return Level.TRACE;
}
@Override
public Level getMinimumLevel(final String tag) {
return Level.TRACE;
}
@Override
public boolean isEnabled(final int depth, final String tag, final Level level) {
return realProvider.isEnabled(depth, tag, level);
}
@Override
public void log(final int depth, final String tag, final Level level, final Throwable exception, final Object obj, final Object... arguments) {
realProvider.log(depth, tag, level, exception, obj, arguments);
}
@Override
public void log(final String loggerClassName, final String tag, final Level level, final Throwable exception, final Object obj, final Object... arguments) {
realProvider.log(loggerClassName, tag, level, exception, obj, arguments);
}
@Override
public void shutdown() throws InterruptedException {
realProvider.shutdown();
}
public void setLogLevel(String logLevel) {
this.logLevel = logLevel;
}
} |
You can fix the class name issue easily. Just add "1" to the stack trace depth: @Override
public boolean isEnabled(final int depth, final String tag, final Level level) {
return realProvider.isEnabled(depth + 1, tag, level);
}
@Override
public void log(final int depth, final String tag, final Level level, final Throwable exception, final Object obj, final Object... arguments) {
realProvider.log(depth + 1, tag, level, exception, obj, arguments);
} |
Auto-answer 😂 : @Override
public void log(final int depth, final String tag, final Level level, final Throwable exception, final Object obj, final Object... arguments) {
realProvider.log(depth /* -->*/ + 1 /*<--*/ , tag, level, exception, obj, arguments);
} |
This closed issue has been locked automatically. However, please feel free to file a new issue. |
Hello,
I use tinylog 2 in my app. In the preferences I want to allow the user to change the severity level of logs.
I see in the documentation that the configuration is immutable:
So, what solution can I use to achieve what I want ? A solution is possible ? This one is very important for my application.
The text was updated successfully, but these errors were encountered: