Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Application: Getting Log4j To Work Within Eclipse Environment

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.

like image 731
Bryce Avatar asked Jan 18 '12 15:01

Bryce


People also ask

How do I get log4j in Eclipse?

Add Log4j Jars to Project Library in Eclipse:Right click the Project name and navigate to Build Path and select “Configure Build Path“. Click on Add External JARS and navigate to the folder where you have kept the Log4j jar files. Select the Executable Jar File and click Apply. Finally click Apply and Close.

How do I change log4j version in eclipse?

Adding the site https://download.eclipse.org/passage/updates/release/2.2.1/ to Window → Preferences → Install/Update → Available Sites and using Help → Check for Updates can be used to upgrade the version of Passage and thereby replace the vulnerable version of log4j2.

Can we use log4j in Java?

Log4j can be configured through Java code or in a configuration file. Configuration files can be written in XML, JSON, YAML, or properties file format.


2 Answers

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.

like image 158
broc.seib Avatar answered Sep 28 '22 04:09

broc.seib


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.

like image 29
Jason Day Avatar answered Sep 28 '22 04:09

Jason Day