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?
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742408121a4438264.html
评论列表(0条)