alexis
alexis

Reputation: 1201

Getting log4j2 to work with eclipse

I know there is allot of questions been asked on this but i have been trying to get this to work for few day's and i am not any more forward then when i started.

i have tried to use -Dlog4j.configuration=file:/path/to/log4j.properties and -Dlog4j.debug in eclipse vm arguments (under debug & run) and get no output

I have tried to use .properties and .xml but no joy

Tried to put the .xml and .properties files at the root, in the src and in an external folder which i added to my classpath ... still no joy

I think its using another .xml or .properties files in another lib/jar but because i cant get any debug to work i am finding very difficult to track what i am doing wrong here...

any help would be great! below is the code .. only the error message get's printed.

I have download (http://logging.apache.org/log4j/2.x/download.html) and imported into my app the log4j-api-2.0-beta8.jar log4j-core-2.0-beta8

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class CucmServMonitor 
{
 private static final Logger logger  = LogManager.getLogger(CucmServMonitor.class.getName())
  public static void main(String[] args) 
  {
    logger.error("testing ERROR level");
    logger.trace("exiting application");
    System.out.println(logger.getName());   
  }
}

the xml file i am using just now log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
 <configuration status="WARN">
  <appenders>
   <Console name="Console" target="SYSTEM_OUT">
    <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
   </Console>
  </appenders>
 <loggers>
  <root level="debug">
   <appender-ref ref="Console"/>
  </root>
 </loggers>
</configuration>

Upvotes: 9

Views: 28679

Answers (5)

MIN KIM
MIN KIM

Reputation: 1

According to Alexis comment, I setup it, but eclipse still can not find log4j2.xml. finally, I solved it by removing the other jars imported, only keep log4j-api and log4j-core. before I imported all the jars downloaded from the Apache website.

I solved my project for log4j->log4j2 version upgrading.

Upvotes: 0

jet.lau
jet.lau

Reputation: 21

according to Alexis comment, I setup it, but eclipse still can not find log4j2.xml. finally, I solved it by removing the other jars imported, only keep log4j-api and log4j-core. before I imported all the jars downloaded from the Apache website. hope this can help someone.

Upvotes: 0

Mastering_the_Object
Mastering_the_Object

Reputation: 1033

Or... just create a resources directory like src/test/resources and add the log4j.xml file to that dir and then make that directory a source folder. Then eclipse will automatically copy the file to the the classes dir and there you have it.

Upvotes: 3

alexis
alexis

Reputation: 1201

Manage to figure this one out. The hint was here.

http://pic.dhe.ibm.com/infocenter/wasinfo/v6r0/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-112.htm

I needed to add a "class folder" of where the log4j2.xml was located and then make sure it was at TOP of the list:

Right click on your project and go to properties Right click on your project and go to properties

Then follow the step shown below. After adding the folder make sure its at the top and then click ok enter image description here

Upvotes: 11

Kunalan S
Kunalan S

Reputation: 154

1) Create the log4j.properties file inside the root folder

log4j.rootCategory=DEBUG, CONSOLE

# Appender writes to console
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=INFO
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n

2) Modify the code like this

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class CucmServMonitor {
    private static final Logger logger = Logger.getLogger(CucmServMonitor.class);
    public static void main(String[] args) {
        PropertyConfigurator.configure("log4j.properties"); 
        logger.error("testing ERROR level");
        logger.trace("exiting application");
        System.out.println(logger.getName());  
    }
}

Upvotes: 0

Related Questions