Here’s a quick snippet that shows how, using java.util.logging
, to ensure that log messages actually show up on your console without the hassle of editing or specifying a logging.properties
file somewhere. This is useful particularly for unit tests featuring the javax.ejb.embeddable.EJBContainer
class.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final Logger topLogger = Logger.getLogger(""); | |
assert topLogger != null; | |
Handler consoleHandler = null; | |
final Handler[] handlers = topLogger.getHandlers(); | |
if (handlers != null && handlers.length > 0) { | |
for (final Handler handler : topLogger.getHandlers()) { | |
if (handler instanceof ConsoleHandler) { | |
consoleHandler = handler; | |
break; | |
} | |
} | |
} | |
if (consoleHandler == null) { | |
consoleHandler = new ConsoleHandler(); | |
topLogger.addHandler(consoleHandler); | |
} | |
consoleHandler.setLevel(Level.FINEST); |