[英]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
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>
Run!跑!
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
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>
Run!跑!
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.