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?
Asked
Active
Viewed 2.7k times
1 Answers
63
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);

assylias
- 321,522
- 82
- 660
- 783
-
15It is just sloppy programming that the makers of these applications don't provide an API to set the log level and instead force the user to go find the implementation that is in use. – Jason K. Jun 06 '17 at 00:22
-
Fully agree with that! – Duncan Krebs May 12 '19 at 02:27
-
Hi, I'm testing this solution and I it works if you initially are in DEBUG mode for example and you set to ERROR level, but if you initially are in ERROR mode and you try to set to DEBUG mode, it seems not working.Thanks.... – rjaraba May 29 '20 at 12:38