Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 768
alexis Avatar asked Jul 30 '13 10:07

alexis


2 Answers

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

like image 95
alexis Avatar answered Oct 14 '22 15:10

alexis


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.

like image 39
Mastering_the_Object Avatar answered Oct 14 '22 14:10

Mastering_the_Object