【问题标题】:log4j2 using CsvParameterLayout, how to add timestamp in the cells?log4j2 使用 CsvParameterLayout,如何在单元格中添加时间戳?
【发布时间】:2020-05-04 12:42:33
【问题描述】:

我正在使用 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的方法?

【问题讨论】:

  • 您好,您的问题的答案是yes,获取时间戳的方式有很多种,例如System.currentTimeMillis();new Date().getTime();、Instant.now()等,这是否回答了您的问题?
  • 谢谢,我本来想问,有没有办法,比如patternlayout,直接引用logger本身的时间戳。如果没有,我会使用你推荐的方法

标签: java timestamp log4j2


【解决方案1】:

您可以在JsonLayOut 中使用KeyValuePair 选项。

请选择并使用 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"
}

参考:Property Substitution - date

案例 2. 使用JsonLayOut

  1. 在您的配置文件中添加JsonLayout。我用log4j2.xml。 关键是&lt;JsonLayout includeTimeMillis="true"&gt;
<?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 抱歉,我无法编写工作代码。我不知道如何在代码中采用它。

参考:System Properties - log4j2.simplelogShowdatetime

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-04
    • 1970-01-01
    相关资源
    最近更新 更多