log4j2使用CsvParameterLayout,如何在单元格中添加时间戳?

问题描述 投票:0回答:1

我使用的是log4j2的 CsvParameterLayout 来记录。

logger.info("unzipPhase logging", "Timestamp", "Level",  "RenamedPath","Instruction","Status", "Message", "Layer#", "Thread#");
logger.info("unzipPhase logging...", "????", "info",  newName.getAbsolutePath(),"unzip","success", "Message...", "Layer#",Thread.currentThread().getName());

有没有什么简单直接的方法来获得 Timestamp?

java timestamp log4j2
1个回答
0
投票

你可以使用 KeyValuePair 选项中 JsonLayOut.

请从3种情况中选择并使用一种。

案例1. KeyValuePair

  1. 增加 JsonLayout 的配置文件中。我使用了 log4j2.xml.关键是 <KeyValuePair key="timestamp" value="$${date:yyyy-MM-dd'T'HH:mm:ss.SSSZ}" />
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
     <Appenders>

        <Routing name="RoutingAppender">
            <Routes pattern="${ctx:logFileName}">
                <Route>
                    <RollingFile name="Rolling-${ctx:logFileName}"
                                 fileName="./logs/${ctx:logFileName}.log"
                                 filePattern="./logs/${ctx:logFileName}.%i.log.gz">
                        <JsonLayout>
                            <KeyValuePair key="timestamp" value="$${date:yyyy-MM-dd'T'HH:mm:ss.SSSZ}" />
                        </JsonLayout>
                        <SizeBasedTriggeringPolicy size="512" />
                    </RollingFile>
                </Route>
            </Routes>
        </Routing>
    </Appenders>

    <Loggers>
        <Root level="all">
            <AppenderRef ref="RoutingAppender" />
        </Root>
    </Loggers>

</Configuration>
  1. 跑!

  2. 检查输出,关键是 timestamp.

{
  "instant" : {
    "epochSecond" : 1588683107,
    "nanoOfSecond" : 556000000
  },
  "thread" : "main",
  "level" : "INFO",
  "loggerName" : "com.study.Stackoverflow",
  "message" : "log printed! - testFile",
  "endOfBatch" : false,
  "loggerFqcn" : "org.apache.logging.log4j.spi.AbstractLogger",
  "threadId" : 1,
  "threadPriority" : 5,
  "timestamp" : "2020-05-05T21:51:47.556+0900"
}

参考资料 : 财产置换 - 日期

案例2.使用 JsonLayOut

  1. 增加 JsonLayout 的配置文件中。我使用了 log4j2.xml.关键是 <JsonLayout includeTimeMillis="true">
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
     <Appenders>

        <Routing name="RoutingAppender">
            <Routes pattern="${ctx:logFileName}">
                <Route>
                    <RollingFile name="Rolling-${ctx:logFileName}"
                                 fileName="./logs/${ctx:logFileName}.log"
                                 filePattern="./logs/${ctx:logFileName}.%i.log.gz">
                        <JsonLayout includeTimeMillis="true">
                        </JsonLayout>
                        <SizeBasedTriggeringPolicy size="512" />
                    </RollingFile>
                </Route>
            </Routes>
        </Routing>
    </Appenders>

    <Loggers>
        <Root level="all">
            <AppenderRef ref="RoutingAppender" />
        </Root>
    </Loggers>

</Configuration>
  1. 跑!

  2. 检查输出,关键是 timeMillis.

{
  "timeMillis" : 1588688067619,
  "thread" : "main",
  "level" : "INFO",
  "loggerName" : "com.edu.test.Stackoverflow",
  "message" : "log printed! - testFile",
  "endOfBatch" : false,
  "loggerFqcn" : "org.apache.logging.log4j.spi.AbstractLogger",
  "threadId" : 1,
  "threadPriority" : 5,
  "test" : "11111"
}

Reference : [JSONLayout - includeTimeMillis](https://logging.apache.org/log4j/2.x/manual/layouts.html#JSONLayout)

案例3. System Property对不起,我无法编写工作代码。我不知道怎样才能在代码中采用它。

参考。系统属性 - log4j2.simplelogShowdatetime

© www.soinside.com 2019 - 2024. All rights reserved.