I have a problem with Log4J (version 1.2.17) configuration .properties file in my Spring (3.2.5) standalone application.

This is my configuration file, logging to a consoles works fine but RollingFileAppender doesn't append messages into logs/application_log.file. I have tried to change almost everything - file name, ConversionPattern, create the file manually and set filesystem rights (OS X Mavericks) for writing to all but nothing works.

log4j.rootLogger=INFO,CA,FA

#Console Appender
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%d{ISO8601} %-5p [%c:%L] - %m%n

#Rolling File Appender    
log4j.appender.FA=org.apache.log4j.RollingFileAppender
log4j.appender.FA.File=logs/application_log.log
log4j.appender.FA.MaxFileSize=50MB
log4j.appender.FA.layout.ConversionPattern=%d{ISO8601} %-5p [%c:%L] - %m%n
log4j.appender.FA.Append=true
log4j.appender.FA.MaxBackupIndex=10
log4j.appender.FA.layout=org.apache.log4j.PatternLayout

What am I doing wrong? Do you see something wrong what I don't see?

有帮助吗?

解决方案

It looks like you're using wrong class for your appender. You should use org.apache.log4j.DailyRollingFileAppender (you're missing word Daily).

But personally I prefer using log4j.xml instead of log4j.properties. For example:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true" xmlns:log4j='http://jakarta.apache.org/log4j/'>

    <appender name="log-app" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="File" value="C:/Temp/my-log.log"/>
        <param name="DatePattern" value="'.'yyyy-MM-dd"/>

        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n"/>
        </layout>
    </appender>

    <root>
        <level value="debug"/>
        <appender-ref ref="log-app"/>
    </root>

</log4j:configuration>

jUnit test:

import org.apache.log4j.Logger;
import org.junit.Test;

public class FakeTest {
    private final static Logger log = Logger.getLogger(FakeTest.class);

    @Test
    public void testTestMe() throws Exception {
        log.debug("Debug message");
        log.error("Error message");

    }
}

Result in my-app.log:

2013-12-20 09:40:40,589 [main] DEBUG my.package.FakeTest - Debug message
2013-12-20 09:40:40,589 [main] ERROR my.package.FakeTest - Error message
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top