Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SLF4J-Log4j logger not logging

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:

  1. I don't have log4j.properties configured right, or
  2. I don't have slf4j-log4j configured right, or
  3. This is a classpath issue and perhaps the WAR can't find log4j.properties (although I would imagine I would see errors or warnings for this), or
  4. I'm not using (the API itself) SLF4J correctly, or
  5. I never actually created something called MonitorAppender, so I'm wondering if this is the problem; I was just following an example I saw online

Can anybody spot where I'm going awrye, or help me troubleshoot this? Thanks in advance!

like image 254
IAmYourFaja Avatar asked Oct 18 '12 11:10

IAmYourFaja


People also ask

Is SLF4J affected by log4j?

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.

Will SLF4J work without log4j?

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.

How does SLF4J work with log4j?

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.


1 Answers

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.

like image 152
Ceki Avatar answered Oct 20 '22 03:10

Ceki