1

I working on java web-application. It supposed to use log4j. According to standard maven directory layout I placed log4j.xml to

src\main\resources\log4j.xml

Then I added the following lines into my web.xml as suggested here:

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

When I run application (from IntelliJ IDEA, mvn jetty:run), the following console message appears

2015-02-20 12:11:59.727:INFO:/:Initializing log4j from [classpath:log4j.xml]

that proofs log4j.xml is found and loaded.

But all rules from log4j.xml not working.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
        <param name="Threshold" value="ALL" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %-5p  [%c{1}] %m %n" />
        </layout>
    </appender>

    <appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
        <param name="Threshold" value="ALL" />
        <param name="File" value="d:\\MyApp.log"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %-5p  [%c{1}] %m %n" />
        </layout>
    </appender>

    <root>
        <priority value="DEBUG"/>
        <appender-ref ref="consoleAppender" />
        <appender-ref ref="fileAppender" />
    </root>

</log4j:configuration>

File d:\MyApp.log not created at all (log.info invocations executed). I tried also d://MyApp.log, ./MyApp.log, MyApp.log - but log4j doesn't create MyApp.log anywhere. Very confused. Where is the error?

P.S.

  1. Yes, application has write permissions this location.

  2. Typical usage:

    public class LogExample { private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LogExample.class); }

Community
  • 1
  • 1
tmporaries
  • 1,523
  • 8
  • 25
  • 39

0 Answers0