如何将日期时间戳添加到log4j2日志文件中?

kxeu7u2r  于 2023-01-13  发布在  其他
关注(0)|答案(6)|浏览(393)

我想使用log4j2创建与日期相关的日志文件:

<RollingFile name="APP" fileName="application-%d{yyyy-MM-dd}.log" />

生成的日志文件名:application-%d{yyyy-MM-dd}.log,时间戳不替换,为什么?

k5hmc34c

k5hmc34c1#

要将文件名附加日期,请将%d替换为以下格式,我遇到了同样的问题,但通过这样做得到了:

<RollingFile name="APP" fileName="application-${date:yyyy-MM-dd}.log" />
bn31dyow

bn31dyow2#

模式不应该在属性“fileName”中给出,而必须在属性“filePattern”中指定模式,如下所示。

<RollingFile name="RollingFile" fileName="${log-path}/filename.log" 
filePattern="${log-path}/filename-%d{yyyy-MM-dd}-%i.log" >
...
...
</RollingFile>

“%i”是将在滚动中自动递增的计数器。
希望这对你有帮助。

yqyhoc1h

yqyhoc1h3#

试试这个:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">

    <Properties>
        <Property name="log-path">D:/logs/</Property>
    </Properties>

    <Appenders>

        <RollingFile name="DebuggerLogger" fileName="${log-path}CMSAutomation.${date:yyyy-MM-dd_hh-mm-ss}.log" filePattern="${log-path}/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="10 MB" />
            </Policies>
        </RollingFile>

    </Appenders>

    <Loggers>
        <Root level="ALL">
            <AppenderRef ref="DebuggerLogger"/>
        </Root>
    </Loggers>

</Configuration>
ktecyv1j

ktecyv1j4#

在appender中使用ASizeBasedTriggeringPolicy类。您甚至可以将当前秒添加到日志文件名中。%d{yyyy_MM_dd HH.mm.ss}

import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.rolling.*;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;

@Plugin(name = "ASizeBasedTriggeringPolicy",
        category = "Core",
        printObject = true
)
public class ASizeBasedTriggeringPolicy extends AbstractTriggeringPolicy {
    private SizeBasedTriggeringPolicy sizeBasedTriggeringPolicy;
    private RollingFileManager aManager;

    protected ASizeBasedTriggeringPolicy(String maxFileSize) {
        sizeBasedTriggeringPolicy = SizeBasedTriggeringPolicy.createPolicy(maxFileSize);

    }

    public void initialize(RollingFileManager aManager) {
        sizeBasedTriggeringPolicy.initialize(aManager);
        this.aManager = aManager;
    }

    public boolean isTriggeringEvent(LogEvent event) {
        if (sizeBasedTriggeringPolicy.isTriggeringEvent(event)) {
            aManager.getPatternProcessor().setPrevFileTime(System.currentTimeMillis());
            return true;
        } else {
            return false;
        }
    }

    @PluginFactory
    public static ASizeBasedTriggeringPolicy createPolicy(@PluginAttribute("size") String size) {
        return new ASizeBasedTriggeringPolicy(size);
    }
}

然后在日志附加器中使用ASizeBasedTriggeringPolicy

<RollingFile name="complete-log" fileName="${log-path}/complete-${date:yyyy_MM_dd HH.mm.ss} .log" 
                 filePattern="${log-path}/app-complete-%d{yyyy_MM_dd HH.mm.ss} - %i.log" >
    ...
    ... 
    <Policies>
         <ASizeBasedTriggeringPolicy size="200 kB"  />
    </Policies>
</RollingFile>
ma8fv8wu

ma8fv8wu5#

在yaml中检查定义为"${date:yyyy-MM-dd}"的属性filePattern,这有助于标记日期。如果您有兴趣将HOSTNAME环境变量标记为date,则:"${env:HOST}-${date:yyyy-MM-dd}"

Properties:
    Property:
      - name: log-path
        value: "logs"
      - name:  filePattern
        value: "${date:yyyy-MM-dd}"

  Appenders:

    Console:
      name: Console_Appender
      target: SYSTEM_OUT
      PatternLayout:
        pattern: "[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"

    File:
      name: File_Appender
      fileName: "${log-path}/filelog-${filePattern}.log"

      PatternLayout:
        pattern: "[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"

    RollingFile:
      - name: RollingFile_Appender
        fileName: "${log-path}/rollingfile-${filePattern}.log"
        filePattern: "logs/archive/rollingfile.log.%d{yyyy-MM-dd-hh-mm}.gz"
        PatternLayout:
          pattern: "[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
xqk2d5yq

xqk2d5yq6#

为了显示log4j2文件中的日期和时间戳,然后添加log4j2.property文件,例如:appender.file.fileName=${filename}/app-${date:yyyy-MM-dd-hh-mm-ss}.log

相关问题