I'm using SLF4J for logging (with Log4J). The used appender is configured using an xml.
<appender name="business" class="org.apache.log4j.RollingFileAppender">
<param name="maxFileSize" value="10MB" />
<param name="maxBackupIndex" value="10" />
<param name="File" value="${jboss.server.log.dir}/business.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %p [%t] %c - %m%n" />
</layout>
</appender>
<category name="BusinessLogger" additivity="false">
<level value="INFO" />
<appender-ref ref="business" />
</category>
The log process is called in an interceptor. Now, I'm trying to test the output of the log. I call the logger at runtime using:
private static final Logger BUSINESS_LOGGER = LoggerFactory.getLogger("BusinessLogger");
In order to test the log (using junit), the output of the log needs to be saved somewhere as a variable. I have an idea about creating a custom appender which saves the last log into variable, but I can't seem to add an appender or change the appender because the Logger class in slf4j is an interface. Does anyone maybe know any workaround for this?
slf4j is just a facade on the top of log4j/log4j2/logback. Once you declare the slf4j logger object:
private static final Logger BUSINESS_LOGGER = LoggerFactory.getLogger("BusinessLogger");
You can add your log4j appender using log4j LogManager.
LogManager.getLogger("BusinessLogger").addAppender(consoleAppender);
So I kind of solved it with the help from alterfox.
When I called the logger using slf4j:
private static final Logger BUSINESS_LOGGER = LoggerFactory.getLogger("BusinessLogger");
It returns me an implementation of the logger adapter from slf4j which doesn't have the add appender method. So I called the logger using log4j Logger class
private static final Logger BUSINESS_LOGGER = Logger.getLogger("BusinessLogger")
Even though the class names are "Logger", they are from different package. The latter one, which is Log4J method, returns the actual Logger object from Log4J. Then the addAppender method becomes available. Well, I tried this, and it doesn't work (except it does). The thing is, I forgot to set the log level. So after adding the appender, I set the log level and then I see the logs coming through the second appender.
This may seem simple but I found when I encountered this error I had forgotten to place the binder package in my POM for my application.
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With