If what you want to do is to check if the class under test writes correct logs, another way is to configure a ListAppender
(which is already provided by LogBack) and point your logger name to that appender in your logging configuration. The appender writes the logs to a java.util.List
and then you can get the appender via the logging framework's API. Then you can check the contents of the list.
LogBack's default ListAppender is not thread-safe so you may want to write something similar with any synchronization if necessary.
Here is how we do it with SLF4j and logback:
import ch.qos.logback.classic.Logger
import ch.qos.logback.core.read.ListAppender
....
Logger logger = (Logger) LoggerFactory.getLogger("some.logger")
ListAppender appender = (ListAppender) logger.getAppender("SOME_LIST")
In your logback.xml file, you configure the appender:
<appender name="SOME_LIST" class="ch.qos.logback.core.read.ListAppender">
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>
<logger name="some.logger" level="DEBUG">
<appender-ref ref="SOME_LIST" />
</logger>