英文:
log4j2 using CsvParameterLayout, how to add timestamp in the cells?
问题
我正在使用log4j2的`CsvParameterLayout`进行日志记录。
logger.info("解压阶段日志", "时间戳", "级别", "重命名路径", "指令", "状态", "消息", "层号", "线程号");
logger.info("解压阶段日志...", "????", "信息", newName.getAbsolutePath(), "解压", "成功", "消息...", "层号", Thread.currentThread().getName());
是否有一种简单直接的方法来获取`时间戳`?
<details>
<summary>英文:</summary>
I am using log4j2's `CsvParameterLayout` to log.
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`?
</details>
# 答案1
**得分**: 0
你可以在`JsonLayout`中使用`KeyValuePair`选项。
请选择并使用以下三种情况之一。
**案例 1. 使用`KeyValuePair`**
1. 在你的配置文件中添加`JsonLayout`。我使用了`log4j2.xml`作为示例。
键是```<KeyValuePair key="timestamp" value="$${date:yyyy-MM-dd'T'HH:mm:ss.SSSZ}" />```
```xml
<?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>
-
运行程序!
-
检查输出。
键是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"
}
参考:属性替换 - date
案例 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>
-
运行程序!
-
检查输出。
键是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"
}
参考:JSONLayout - includeTimeMillis
案例 3. 使用系统属性
抱歉,我无法提供可工作的代码。我不清楚如何在代码中应用它。
参考:系统属性 - log4j2.simplelogShowdatetime
英文:
You can use KeyValuePair
option in JsonLayOut
.
Please choice and use one among of 3 cases.
CASE 1. Using KeyValuePair
- add
JsonLayout
in your configuration file. I usedlog4j2.xml
.
The key is<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.
The key istimestamp
.
{
"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
- add
JsonLayout
in your configuration file. I usedlog4j2.xml
.
The key is<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.
The key istimeMillis
.
{
"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. I don't know how could I adopt it in code.
Reference : System Properties - log4j2.simplelogShowdatetime
专注分享java语言的经验与见解,让所有开发者获益!
评论