[
https://issues.apache.org/jira/browse/LOG4J2-2749?focusedWorklogId=438629&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-438629
]
ASF GitHub Bot logged work on LOG4J2-2749:
------------------------------------------
Author: ASF GitHub Bot
Created on: 29/May/20 09:13
Start Date: 29/May/20 09:13
Worklog Time Spent: 10m
Work Description: gagoman opened a new pull request #362:
URL: https://github.com/apache/logging-log4j2/pull/362
### Description
`Log4jJsonObjectMapper` [has `JsonInclude.Include.NON_EMPTY`
enabled](https://github.com/apache/logging-log4j2/blob/log4j-2.13.3/log4j-core/src/main/java/org/apache/logging/log4j/core/jackson/Log4jJsonObjectMapper.java)
but it is not working at the moment - keys with empty values are still present
in output. It is caused by [bug in
`jackson-databind`](https://github.com/FasterXML/jackson-databind/pull/2615)
which I have fixed. Fix was released as part of `jackson-databind:2.11.x`.
### Why LOG4J2-2749?
I feel this fix will address user request.
### Issue itself
#### `log4j2.xml`
```
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<JsonLayout compact="true" properties="true">
<KeyValuePair key="empty" value="${ctx:empty:-}"/>
</JsonLayout>
</Console>
</Appenders>
<Loggers>
<Root level="DEBUG">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
```
#### `Main.java`
```
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Main {
private static final Logger logger = LogManager.getLogger();
public static void main(String[] args) {
logger.info("info");
}
}
```
#### Output
```
{"instant":{"epochSecond":1590743485,"nanoOfSecond":93000000},"thread":"main","level":"INFO","loggerName":"org.apache.logging.log4j.core.Main","message":"info","endOfBatch":false,"loggerFqcn":"org.apache.logging.log4j.spi.AbstractLogger","contextMap":{},"threadId":1,"threadPriority":5,"empty":""}
```
### Notes
* I have chosen `release-2.x` while I have actually developed a patch on
`2.13.3` tag. I can rebase to any other brach, just let me know which one
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 438629)
Remaining Estimate: 0h
Time Spent: 10m
> JsonLayout KeyValuePair should not log json key which has empty or blank value
> ------------------------------------------------------------------------------
>
> Key: LOG4J2-2749
> URL: https://issues.apache.org/jira/browse/LOG4J2-2749
> Project: Log4j 2
> Issue Type: New Feature
> Components: Layouts
> Affects Versions: 2.12.1
> Reporter: Mahesh Gujale
> Priority: Minor
> Labels: features
> Time Spent: 10m
> Remaining Estimate: 0h
>
> JsonLayout configured with following
> {quote}<Console name="APP_STAT" target="SYSTEM_OUT">
> <JSONLayout objectMessageAsJsonObject="true" compact="true"
> eventEol="true">
> <KeyValuePair key="timestamp"
> value="$${date:yyyy-MM-dd'T'HH:mm:ss.SSSZ}" />
> <KeyValuePair key="type" value="$${ctx:type:-}" />
> </JSONLayout>
> </Console>
> {quote}
> Which logs as below
> {quote}{
> "thread":"http-nio-8080-exec-1",
> "level":"INFO",
> "loggerName":"com.xyz.logging.interceptor.LogApiInterceptor",
> "message":"App Stat",
> "endOfBatch":true,
> "loggerFqcn":"org.apache.logging.log4j.spi.AbstractLogger",
> "instant":{
> "epochSecond":1577422969,
> "nanoOfSecond":22000000
> },
> "threadId":26,
> "threadPriority":5,
> "timestamp":"2019-12-27T10:32:49.022+0530",
> "type":"",
> }
> {quote}
> If we do not put 'type' in MDC or ThreadContext then json contains blank or
> empty key 'type'. If the key type is not put MDC or if its value is empty
> string/blank then JsonLayout should not log the key type in json.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)