I use Log4j to write some log my program. I find and read many question and answer in this site, but i can't solve my problem.

Here my code:

1. log4j.xml

<appender name="rollingfileAppender" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="append" value="true"/>
    <param name="file" value="logs/process.log"/>
    <param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
    <layout class="org.apache.log4j.PatternLayout">
       <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss:SSS} %-5p [%c{1}] %m%n"/>
    </layout>
</appender>
<root>
    <level value="DEBUG"/>
    <appender-ref ref="rollingfileAppender"/>
    <appender-ref ref="stdout"/>
</root>

2. My java code

package TestPacket;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;


public class TestLog4jXML {
    static Logger logger = org.apache.log4j.Logger.getLogger(TestLog4jXML.class.getName());
    public TestLog4jXML() {
    }
    public static void main(String[] args) {
        try {
            DOMConfigurator.configure("log4j1.xml");
            logger.trace("Entering application.");
            logger.debug("Debug");
            logger.info("info");
            logger.warn("warn");
            logger.error("error");
            logger.fatal("fatal");
            lungtng();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void lungtng()
    {
        logger.fatal("some text here");
        logger.info("hello picaso");
    }
}

And i run my program, with eclipse, windows os. But the log file name only: process.log not in daily format: process.log.yyyy-MM-dd-HH

Who can explain this to me?

有帮助吗?

解决方案

The org.apache.log4j.DailyRollingFileAppender will create new log file for each day, each hour or each minute but file name of the current log always will be in the format that you've specified in the "file" parameter. In your example it's "process.log". The file names of the logs for the previous hours will be in format "process.log.yyyy-MM-dd-HH".

其他提示

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top