[ 
https://issues.apache.org/jira/browse/LOG4J2-3080?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17356467#comment-17356467
 ] 

Volkan Yazici commented on LOG4J2-3080:
---------------------------------------

Hrm... The problem is more intricate then I was anticipating.

{{org.apache.logging.log4j.Logger.info(String)}}, logging via the new Log4j 2 
API, wraps the content {{String}} into a {{SimpleMessage}} instance, which is 
treated by {{MessageResolver}} (of {{JsonTemplateLayout}}) in combination with 
{{fallbackKey}}. Whereas, 
{{org.apache.log4j.LogManager.getLogger(Main.class).info(String)}}, logging via 
the legacy Log4j 1 API, wraps the content {{String}} into an {{ObjectMessage}} 
and consequently {{fallbackKey}} gets deliberately discarded by 
{{MessageResolver}}. I guess the suspect here is 
{{org.apache.log4j.Category#maybeLog(String fqcn, Level level, Object message, 
Throwable throwable)}} method:

{code:java}
private void maybeLog(final String fqcn, final org.apache.logging.log4j.Level 
level,
        final Object message, final Throwable throwable) {
    if (logger.isEnabled(level)) {
        @SuppressWarnings("unchecked")
        Message msg = message instanceof Map ? new MapMessage((Map) message) : 
new ObjectMessage(message);
        if (logger instanceof ExtendedLogger) {
            ((ExtendedLogger) logger).logMessage(fqcn, level, null, msg, 
throwable);
        } else {
            logger.log(level, msg, throwable);
        }
    }
}
{code}

I think when {{message}} is of type {{String}}, {{msg}} should be a 
{{SimpleMessage}}. I will discuss this further in the {{dev}} mailing list and 
address the issue accordingly.

> Messages logged inconsistently when using JsonTemplateLayout.
> -------------------------------------------------------------
>
>                 Key: LOG4J2-3080
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-3080
>             Project: Log4j 2
>          Issue Type: Bug
>          Components: JsonTemplateLayout
>    Affects Versions: 2.14.1
>            Reporter: Schubert Fernandes
>            Assignee: Volkan Yazici
>            Priority: Major
>             Fix For: 2.15.0
>
>         Attachments: log4j.json.event.layout.zip
>
>
> When using the JsonTemplateLayout and configuring the message with a 
> fallbackKey...
> {code:json}
>   "message": {
>     "$resolver": "message",
>     "fallbackKey": "asString"
>   }
> {code}
> the fallbackKey is not consistently used.
> When logging via _org.apache.log4j.Category.info(Object)_ the message string 
> is logged directly without using the property defined in _fallbackKey_, e.g. 
> {panel}
> {"timestamp":"2021-04-21T14:25:27.625+01:00","loggerName":"explore.log4j.json.template.layout.Main","level":"INFO","{color:red}message":"Message
>  logged using org.apache.log4j.Category.info(Object)"}{color}
> {panel}
> but when logging using _org.apache.logging.log4j.Logger.info(String)_ the 
> message is correctly logged in the _asString_ property as required, i.e.
> {panel}
> {"timestamp":"2021-04-21T14:25:27.628+01:00","loggerName":"explore.log4j.json.template.layout.Main","level":"INFO","{color:green}message":{*"asString"*:"Message
>  logged using org.apache.logging.log4j.Logger.info(String)"}}{color}
> {panel}
> Sample Maven project attached to reproduce.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to