Java Application: Getting Log4j To Work Within Eclipse Environment

38,752

Solution 1

There may be another log4j.properties or log4j.xml file in the classpath ahead of your log4j.properties. Open the run configuration for your project, and add -Dlog4j.debug=true as a VM Argument for your project. This will instruct log4j to print a lot of additional information on the console, including the config file that it is using.

Solution 2

If you are using the logging façade slf4j, then you need to specify exactly one logging backend by including the corresponding jar file for that backend. In your case, you have installed slf4j-jdk14-x.x.x.jar on your classpath, which is just a generic logger backend.

In order to use the log4j backend, you need to remove slf4j-jdk14-x.x.x.jar and replace it with slf4j-log4j12-x.x.x.jar. If you don't remove it, slf4j must choose only one backend jar, and probably not the one you want.

Of course you will also need the actual log4j-x.x.x.jar file on your classpath too.

Once these jars are properly in place, then the VM parameter of -Dlog4j.debug will actually work and be useful in debugging where your logging configs are coming from.

Solution 3

You need to tell your code to use the properties file. Before any logging is done please put

PropertyConfigurator.configure("log4j/log4j.properties");
Share:
38,752
Bryce
Author by

Bryce

Updated on July 05, 2022

Comments

  • Bryce
    Bryce almost 2 years

    I've done my best to setup Eclipse and my Java application to use a log4j.properties file. However, it does not seem to be using the properties file and I'm not sure why.

    Libraries: slf4j-api-1.6.1, slf4j-jdk14-1.6.1

    Within the application the logging works fine. I am able to print info, warnings, and errors into the Eclipse console.

    What I would like to be able to do is change the log level to debug and print all logging messages to both the console and a log file.

    I have created a log4j.properties file that looks like this:

    log4j.rootLogger=DEBUG,console,file
    log4j.rootCategory=DEBUG, R, O
    
    # Stdout
    log4j.appender.O=org.apache.log4j.ConsoleAppender
    
    # File
    log4j.appender.R=org.apache.log4j.RollingFileAppender
    log4j.appender.R.File=log4j.log
    
    # Control the maximum log file size
    log4j.appender.R.MaxFileSize=100KB
    
    # Archive log files (one backup file here)
    log4j.appender.R.MaxBackupIndex=5
    log4j.appender.file.File=checkLog.log
    log4j.appender.file.threshold=DEBUG
    
    log4j.appender.R.layout=org.apache.log4j.PatternLayout
    log4j.appender.O.layout=org.apache.log4j.PatternLayout
    
    log4j.appender.R.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n
    log4j.appender.O.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n
    

    My directory structure looks like this:

    My Project
    --src/
    ----MYProject/
    ------*.java
    --bin/
    ----MYProject/
    ------*.class
    --log4j/
    ----log4j.properties
    

    In Eclipse I this this:

    Run Configurations -> Classpath (tab) ->, right clicked on User Entries -> Added "log4j" as a new folder, and saved.

    Then in my code I call the logger like this (sample code to demonstrate my approach so it may have syntax errors):

    package MYProject;
    import org.slf4j.LoggerFactory;
    
    public class MyClass{
    
      final org.slf4j.Logger test_logger = LoggerFactory.getLogger(MyClass.class);
    
      public MyClass(){}
    
      public someMethod(){
        test_logger.debug("Some Debug");
        test_logger.info("Some Info");
        test_logger.warn("Some Warning");
        test_logger.error("An Error");
      }
    
    }
    

    I then call someMethod and it prints INFO, WARN, ERROR to the Eclipse console. It won't print DEBUG and won't print to a file.

    I'd appreciate any suggestions on what I may be doing wrong.

  • Bryce
    Bryce over 12 years
    I've played around with both your comment and apines and this is what I've figured out. I cannot add PropertyConfigurator with my current setup because it is a property of the log4j library and not a part of the slf4j-jdk14 (slf4j's log4j binder as I understand it) library. Setting the VM parameters to true does not return any extra debugging.
  • Bryce
    Bryce over 12 years
    If I then reference the actual log4j library in addition to slf4j-jdk14 I start getting debug output. Just adding the reference makes my log4j.properties file start working. This would seem to get me about 90% of what I need. This increases the debugging but the commands I print out in the given class in the initial post no longer print (test_logger.debug, test_logger.info, etc). I think this is because the log4j library I added is overwriting org.slf4j.Logger that I was previously using. I'm a little confused on how to get these two libraries to work better together, any suggestions?
  • Jason Day
    Jason Day over 12 years
    slf4j is just a facade for logging, you still have to add the actual logging library (log4j in your case) to your classpath. See this link: slf4j.org/manual.html#binding
  • abarisone
    abarisone almost 9 years
    Could you please elaborate more your answer adding a little more description about the solution you provide?
  • Mikko Rantalainen
    Mikko Rantalainen over 2 years
    And when you know what CVE-2021-44228 (a.k.a. Log4Shell) is, you'll be happy that log4j was not used by default for your installation.