You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Log4j 2.9.1 XML配置中的静态日志文件名改为带时间戳?

Solution for Dynamic Timestamped Filename in Log4j 2.9.1

To create a dynamic filename with a timestamp for your dubg_Logs appender, the basic <File> appender won't work—it only supports static filenames. Instead, you'll need to use Log4j 2's RollingFileAppender, which is designed for time-based or size-based log rolling and can include dynamic timestamps in filenames.

Here's your updated XML configuration with the modified dubg_Logs appender:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS}{GMT+3} [%t] %-5level %logger{36} - %msg%n" />
    </Console>
    <File name="MyFile" fileName="CRM_Update_Logs.log" immediateFlush="true" append="true">
      <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS}{GMT+3} %c{2} [%t] %-5level %logger{36} - %msg%n"/>
    </File>
    <!-- Modified RollingFileAppender for dynamic timestamped filename -->
    <RollingFile name="dubg_Logs" 
                 fileName="CRM_Update_dubg_Logs_%d{yyyy-MM-dd}.log" 
                 filePattern="CRM_Update_dubg_Logs_%d{yyyy-MM-dd}-%i.log.gz"
                 immediateFlush="true" 
                 append="true">
      <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS}{GMT+3} %c{2} [%t] %-5level %logger{36} - %msg%n"/>
      <!-- Time-based rolling policy: rolls over once per day at midnight -->
      <Policies>
        <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
        <!-- Optional: add size-based rolling if needed -->
        <!-- <SizeBasedTriggeringPolicy size="10MB"/> -->
      </Policies>
      <!-- Optional: keep up to 30 days of archived logs -->
      <DefaultRolloverStrategy max="30"/>
    </RollingFile>
  </Appenders>
  <Loggers>
    <Root level="debug">
      <AppenderRef ref="Console"/>
      <AppenderRef ref="MyFile"/>
      <AppenderRef ref="dubg_Logs"/>
    </Root>
    <Logger level="ALL" additivity="false" name="dubg">
      <AppenderRef ref="dubg_Logs"/>
    </Logger>
  </Loggers>
</Configuration>

Key Changes Explained:

  • Replaced <File> with <RollingFile>: This appender supports dynamic filename patterns and automatic log rolling.
  • Dynamic fileName: The %d{yyyy-MM-dd} placeholder inserts the current date into the active log filename (e.g., CRM_Update_dubg_Logs_2024-05-20.log).
  • filePattern: Defines the format for archived logs—here, old logs are compressed into .gz files with an index (%i) if multiple rolls happen in one day (handy if you enable size-based rolling).
  • Time-Based Triggering: The <TimeBasedTriggeringPolicy> ensures a new log file is created every day at midnight (modulate="true" aligns rollover time with midnight even if your app starts mid-day).
  • Optional Extras: Uncomment the size-based policy to roll logs when they hit a specific size, or adjust DefaultRolloverStrategy to control how many archived logs are retained.

This setup works seamlessly with Log4j 2.9.1 and will automatically generate timestamped log files on a daily basis.

内容的提问来源于stack exchange,提问作者MUNEER

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:18:49