Question

I have the log4j2 jar files in my class path. Below is my Log4j2.xml. I want to write the logs to my local file "G:/test/apps.log", but I'm having two issues:

  1. When I run the web application, the output is printed only to the console and not to the file.
  2. I have seen a few answers which suggested putting the Log4j2.xml in WEB-INF/classes. However, when I do a clean build, the file will get deleted, so where should I put the configuration file?

    Note : I use Tomcat : 7.0.34, Servlet 3. I have removed the reference of Log4J in Cantalina properties.

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration status="warn">
      <appenders>
    
        <Console name="Console" target="SYSTEM_OUT">
          <PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
    
        <File name="MyFile" fileName="G:/test/apps.log">
          <PatternLayout pattern="%d{yyyy-mm-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </File>  
    
      </appenders>
    
      <loggers>
    
        <root level="trace">
          <appender-ref ref="MyFile" level="trace" additivity="true"/>
        </root>
    
      </loggers>
    </configuration>
    
Was it helpful?

Solution

You need both the log4j-api-2.0 and the log4j-core-2.0 jar files in the classpath. If the core jar is not found, the api jar falls back to a SimpleLogger (console) implementation.

By changing the config to <configuration status="trace"> you can see log4j2 internal logging that may help trouble-shoot configuration issues. (This also requires the log4j-core-2.0 jar though.)

The log4j2.xml file can be in the classpath, or you can specify a location with system property -Dlog4j.configurationFile=/path/to/log4j2.xml,

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top