简体   繁体   English

log4j仅使用log4j.xml在文件名中创建带有时间戳的日志文件

[英]log4j create log file with timestamp in filename using ONLY log4j.xml

I am trying to create a log file with a date appended to the filename such as application.log.yyyymmdd . 我正在尝试创建一个日志文件,并在文件名后附加日期,例如application.log.yyyymmdd

This is my current log4j.xml. 这是我当前的log4j.xml。

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

    <!-- Appenders -->
    <appender name="console" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="File" value="application.log" />
        <param name="DatePattern" value=".yyyy-MM-dd" />
        <layout class="org.apache.log4j.PatternLayout"> 
          <param name="ConversionPattern" 
              value="%d{yyyy-MMM-dd HH:mm:ss,SSS} [%t] %c %x%n  %-5p %m%n"/>
        </layout>
    </appender>


    <!-- Root Logger -->
    <root>
        <priority value="debug" />
        <appender-ref ref="console" />
    </root>

</log4j:configuration>

It is not creating application.log.yyyymmdd instead it is only generating application.log . 它不是在创建application.log.yyyymmdd而是仅在生成application.log

Is there another way to only use log4j.xml to achieve this? 还有另一种方法只能使用 log4j.xml来实现吗?

The log file is rolled out every day, and the current day's log file without date. 日志文件每天推出,而当天的日志文件不带日期。 Suppose current day is 2015-05-06, and at midnight, once it pass 23:59:59, log4j will backup the application.log into application.log.2015-05-06 and the application.log file become logging for the new day, 2015-05-07, and son on 假设当前日期是2015-05-06,并且在午夜,一旦经过23:59:59,log4j将把application.log备份到application.log.2015-05-06 ,并且application.log文件将成为新的一天,2015年5月7日,儿子

You should be aware application.log is current day's log file, so it does not matter it has a data suffix or not. 您应该知道application.log是当天的日志文件,因此它是否有数据后缀都无关紧要。

The following is what I am using for now, it is rolled a new file every day, and kept the log files for 30 days. 以下是我目前使用的内容,它每天都会滚动一个新文件,并将日志文件保留30天。

<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>/data/log/logging.%d{yyyy-MM-dd}.log</fileNamePattern>
        <maxHistory>30</maxHistory>
</rollingPolicy>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM