I am trying to use SLF4J-Log4j for the first time. In every Java class, I define a logger like so:
private org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(<TheClass>.class);
(And of course, I make sure that the slf4-log4j12-1.6.4.jar
JAR is on the classpath!)
But whenever I go to use the logger, like logger.debug("Something interesting happened");
or logger.error("An error occurred");
, I don't see their output in my log files. However, no exceptions occur and the app (its actually a WAR deployed to Tomcat) runs fine.
Here is the log4j.properties
file included in the project:
# Set the root logger to DEBUG.
log4j.rootLogger=DEBUG
# MonitorLog - used to log messages in the Monitor.log file.
log4j.appender.MonitorAppender=org.apache.log4j.FileAppender
log4j.appender.MonitorAppender.File=${catalina.base}/logs/MyAppMonitor.log
log4j.appender.MonitorAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.MonitorAppender.layout.ConversionPattern= %-4r [%t] %-5p %c %x - %m%n
# Use the MonitorAppender to log all messages.
log4j.logger.*=DEBUG,MonitorAppender
org.quartz.impl.StdSchedulerFactory=DEBUG,MonitorAppender
This WAR uses Quartz to cron a few jobs, which is why you see that last entry.
When I check Tomcat's logs/ directory, I see the MyAppMonitor.log
get created, but it has nothing in it (0 bytes). I've scanned all the typical catalina.out, catalina-, and localhost- logs as well, and none of my log statements are seeing the light of day.
I am thinking:
log4j.properties
configured right, orlog4j.properties
(although I would imagine I would see errors or warnings for this), orMonitorAppender
, so I'm wondering if this is the problem; I was just following an example I saw onlineCan anybody spot where I'm going awrye, or help me troubleshoot this? Thanks in advance!
The SLF4J API is just an API which lets message data go through. As such, using log4j 2. x, even via SLF4J does not mitigate the vulnerability.
So essentially, SLF4J does not replace log4j; they both work together. It removes the dependency on log4j from your application and makes it easy to replace it in the future with the more capable library.
SLF4J standardized the logging levels, which are different for the particular implementations. It drops the FATAL logging level (introduced in Log4j) based on the premise that in a logging framework we should not decide when to terminate an application. The logging levels used are ERROR, WARN, INFO, DEBUG and TRACE.
Your log4j.properties
configuration file is has a number of errors in it. Try with something simple like the following.
log4j.debug=true
log4j.rootLogger=DEBUG, CON
log4j.appender.CON=org.apache.log4j.ConsoleAppender
log4j.appender.CON.layout=org.apache.log4j.PatternLayout
log4j.appender.CON.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
As for the configuration file in your question, the root logger does not have an appender attached. Moreover, the line
log4j.logger.*=DEBUG,MonitorAppender
is not valid as '*' is not supported.
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