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

时间:2021-09-24 23:02:44

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来实现这个目的吗?

1 个解决方案

#1


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-05-07,和儿子

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>

#1


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-05-07,和儿子

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>