[英]log4j2 using CsvParameterLayout, how to add timestamp in the cells?
我正在使用 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
?
您可以在JsonLayOut
中使用KeyValuePair
选项。
请选择并使用 3 种情况中的一种。
案例 1. 使用KeyValuePair
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>
跑!
检查 output。 关键是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
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>
跑!
检查 output。 关键是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
抱歉,我无法编写工作代码。 我不知道如何在代码中采用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.