带有时间戳的动态日志文件名log4j xml文件配置

时间:2018-05-11 11:03:53

标签: java xml configuration log4j configuration-files

我有静态日志文件名“CRM_Update_dubg_Logs.log”的xml配置文件,我想用时间戳创建这个名称,java log4j 2.9.1

<?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>
    <File name="dubg_Logs" fileName="CRM_Update_dubg_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>
</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>

1 个答案:

答案 0 :(得分:0)

在xml中编辑文件名

fileName="CRM_Update_Logs.log"    

fileName="${sys:logFilename}_CRM_Update_Logs.log"

并将其添加到您的Java代码

    Date date = new Date();      
    String LogDate= new SimpleDateFormat("yyyyMMdd").format(date);
    System.setProperty("logFilename", LogDate);