admin管理员组文章数量:1336645
I have the following log4j2.xml (with log4j2 version at 2.24 as of this writing)
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info" name="spring-boot-kafka-log">
<Appenders>
<Kafka name="Kafkalol" topic="topic-trying">
<JsonLayout></JsonLayout>
<Property name="bootstrap.servers">localhost:9093</Property>
</Kafka>
<Async name="Async">
<AppenderRef ref="Kafkalol"/>
</Async>
<Console name="stdout" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5p [%-7t] %F:%L - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="Kafkalol"/>
<AppenderRef ref="stdout"/>
</Root>
<Logger name=".apache.kafka" level="WARN" />
<!-- avoid recursive logging -->
</Loggers>
</Configuration>
As you can see, I am using the JSONLayout
construct.
This is working great, and I am able to see my logs as json format, something like this:
{
"instant": {
"epochSecond": 1731992687,
"nanoOfSecond": 353795274
},
"thread": "main",
"level": "ERROR",
"loggerName": "com.reloadly.log4jappender.HelloWorld",
"message": "error in finally 0",
"thrown": {
"commonElementCount": 0,
"localizedMessage": "/ by zero",
"message": "/ by zero",
"name": "java.lang.ArithmeticException",
"extendedStackTrace": [
{
"classLoaderName": "app",
"class": "com.reloadly.log4jappender.HelloWorld",
"method": "createLogs",
"file": "HelloWorld.java",
"line": 32,
"exact": true,
"location": "classes/",
"version": "?"
}
]
},
"endOfBatch": false,
"loggerFqcn": ".apache.logging.slf4j.Log4jLogger",
"threadId": 1,
"threadPriority": 5
}
I wish to add this json log, as it is, inside a custom json, and nest it inside. Something like this:
{
"mycustomfield": "somecustomvalue",
"thecustombusinessflow": "ourcoolpaymentservice",
"payload": {
"instant": {
"epochSecond": 1731992687,
"nanoOfSecond": 353795274
},
"thread": "main",
"level": "ERROR",
"loggerName": "com.reloadly.log4jappender.HelloWorld",
"message": "error in finally 0",
"thrown": {
"commonElementCount": 0,
"localizedMessage": "/ by zero",
"message": "/ by zero",
"name": "java.lang.ArithmeticException",
"extendedStackTrace": [
{
"classLoaderName": "app",
"class": "com.reloadly.log4jappender.HelloWorld",
"method": "createLogs",
"file": "HelloWorld.java",
"line": 32,
"exact": true,
"location": "classes/",
"version": "?"
}
]
},
"endOfBatch": false,
"loggerFqcn": ".apache.logging.slf4j.Log4jLogger",
"threadId": 1,
"threadPriority": 5
}
}
Just to avoid confusion, the content of the json from JSONLayout should be untouched, just nest it inside another custom json (in my case the field "payload", with some other custom fields.
And just to avoid confusion, it is not this, not adding custom fields inside the content of the json, but rather "outside"
not this:
{
"mycustomfield": "NOT THIS",
"thecustombusinessflow": "NO NO",
"instant": {
"epochSecond": 1731992687,
"nanoOfSecond": 353795274
},
"thread": "main",
"level": "ERROR",
"loggerName": "com.reloadly.log4jappender.HelloWorld",
"message": "error in finally 0",
"thrown": {
"commonElementCount": 0,
"localizedMessage": "/ by zero",
"message": "/ by zero",
"name": "java.lang.ArithmeticException",
"extendedStackTrace": [
{
"classLoaderName": "app",
"class": "com.reloadly.log4jappender.HelloWorld",
"method": "createLogs",
"file": "HelloWorld.java",
"line": 32,
"exact": true,
"location": "classes/",
"version": "?"
},
{
"classLoaderName": "app",
"class": "com.reloadly.log4jappender.Log4jAppenderApplication",
"method": "main",
"file": "Log4jAppenderApplication.java",
"line": 12,
"exact": true,
"location": "classes/",
"version": "?"
}
]
},
"endOfBatch": false,
"loggerFqcn": ".apache.logging.slf4j.Log4jLogger",
"threadId": 1,
"threadPriority": 5
}
I tried so far to manipulate the MDC, but it is giving the result of creating fields inside the initial json log.
May I ask how to nest the json from JSONLayout in a custom json?
本文标签: logginglog4j2 JSONLayouthow to nest the default json log inside a custom jsonStack Overflow
版权声明:本文标题:logging - log4j2 JSONLayout, how to nest the default json log inside a custom json - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742408120a2469238.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论