SLF4J-Log4j logger not logging

38,679

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.

Share:
38,679
IAmYourFaja
Author by

IAmYourFaja

my father is a principal at burgoyne intnl and got me this job programming lisp and development. I aspire to unittesting with a concentration in mobile platforms.

Updated on October 19, 2020

Comments

  • IAmYourFaja
    IAmYourFaja over 3 years

    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!