log4j can´t create the log file

I am trying to use log4j in my Java web application. I am using Tomcat 6. The problem is that the log file is never created.

The log4j.properties is in the classpath, this means:

In the source code I am using:

private static Logger log = Logger.getLogger(myclass.class);

And then when i am trying to write a line:

log.error("here the error message", e);

Also here my log4j.properties file:

 log4j.rootLogger=A1,file

log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d{ISO8601} %5p [%t] %c{1}:%L - %m%n

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.maxFileSize=5000KB
log4j.appender.file.maxBackupIndex=20
log4j.appender.file.File=URLFILE/file.log
log4j.appender.file.threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ISO8601} %5p [%t] %c{1}:%L - %m%n

Why when i am running this, the log file is never created?

Upvotes: 1

Views: 4423

Answers (3)

herrtim
herrtim

Reputation: 2755

I cannot help you with your log4j.properties file, but I can give you a working log4j.xml file that I use:

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

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

  <appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="File" value="${catalina.home}/logs/Log.log"/>
    <param name="DatePattern" value="'.'yyyy-MM-dd"/>
    <param name="Append" value="true"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %p - %m%n"/>
    </layout>
  </appender>

    <!--                           -->
    <!-- setup log4j's root logger -->
    <!--                           -->
    <root>
        <level value="DEBUG" />
        <appender-ref ref="FILE"/>
    </root>

</log4j:configuration>

I believe the xml configuration is the preferred way to configure log4j anyway.

Upvotes: 1

YMomb
YMomb

Reputation: 2397

Is there a log4j.jar in your classpath?

Maybe you also have to set up the root logger level. log4j.rootLogger= DEBUG, A1,file

Upvotes: 0

Soumya
Soumya

Reputation: 21

You have already put a log4J.property file in the src, thats gud.. Just put a log4J.jar file in lib also...

Upvotes: 0

Related Questions