简体   繁体   English

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

[英]log4j2 using CsvParameterLayout, how to add timestamp in the cells?

I am using log4j2's CsvParameterLayout to log.我正在使用 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());

Is there any simple and direct way to get Timestamp ?有没有简单直接的方法来获取Timestamp

You can use KeyValuePair option in JsonLayOut .您可以在JsonLayOut中使用KeyValuePair选项。

Please choice and use one among of 3 cases.请选择并使用 3 种情况中的一种。

CASE 1. Using KeyValuePair案例 1. 使用KeyValuePair

  1. add JsonLayout in your configuration file.在配置文件中添加JsonLayout I used log4j2.xml .我用log4j2.xml The key is <KeyValuePair key="timestamp" value="$${date:yyyy-MM-dd'T'HH:mm:ss.SSSZ}" />关键是<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. Run!跑!

  2. Check the output.检查 output。 The key is timestamp .关键是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"
}

Reference: Property Substitution - date参考: 属性替换 - 日期

CASE 2. Using JsonLayOut案例 2. 使用JsonLayOut

  1. add JsonLayout in your configuration file.在配置文件中添加JsonLayout I used log4j2.xml .我用log4j2.xml The key is <JsonLayout includeTimeMillis="true">关键是<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. Run!跑!

  2. Check the output.检查 output。 The key is timeMillis .关键是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)

CASE 3. Using System Property Sorry, I couldn't make working code.案例 3. 使用System Property抱歉,我无法编写工作代码。 I don't know how could I adopt it in code.我不知道如何在代码中采用它。

Reference: System Properties - log4j2.simplelogShowdatetime参考: 系统属性 - log4j2.simplelogShowdatetime

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM