Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change logback log level programmatically in Java? [duplicate]

Tags:

I have some performance unit tests that create an unreasonable amount of logs that I don't really care for and are actually impacting performance. Can I somehow raise the log level in logback in the Before method for that unit test only (and restore it in After)? Or is there a better way of addressing that?

like image 930
naumcho Avatar asked Mar 09 '12 00:03

naumcho


People also ask

How do you change the log level?

To change log levels as a root user, perform the following: To enable debug logging, run the following command: /subsystem=logging/root-logger=ROOT:change-root-log-level(level=DEBUG) To disable debug logging, run the following command: /subsystem=logging/root-logger=ROOT:change-root-log-level(level=INFO)

How do you change the log level in log4j2 at runtime?

You can set a logger's level with the class Configurator from Log4j Core. BUT be aware that the Configurator class is not part of the public API. If you wish to change the root logger level, do something like this : LoggerContext ctx = (LoggerContext) LogManager.


1 Answers

This works for me:

public static void setLoggingLevel(Level level) {     ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) org.slf4j.LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);     root.setLevel(level); } 

Then in your test, if you want all levels for example:

setLoggingLevel(Level.ALL); 
like image 133
assylias Avatar answered Sep 21 '22 01:09

assylias