You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by "Maple Wang (JIRA)" <ji...@apache.org> on 2016/08/11 03:48:21 UTC

[jira] [Updated] (LOG4J2-1510) why MutableLogEvent don't keep the ReusableObjectMessage

     [ https://issues.apache.org/jira/browse/LOG4J2-1510?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Maple Wang updated LOG4J2-1510:
-------------------------------
    Description: 
After upgrading Log4j from 2.5 to 2.6.2, I found that log event catched by my appender is changed from Log4jLogEvent to MutableLogEvent, it's fine except that I can't get the ObjectMessage from MutableLogEvent. If I sent object message though log4j API like log.info(object);, I assume object can be obtained in ReusableObjectMessage, but since MutableLogEvent look like don't keep it as below:

public void setMessage(final Message msg) {
        if (msg instanceof ReusableMessage) {
            final ReusableMessage reusable = (ReusableMessage) msg;
            reusable.formatTo(getMessageTextForWriting());
            if (parameters != null) {
                parameters = reusable.swapParameters(parameters);
                parameterCount = reusable.getParameterCount();
            }
        } else {
            // if the Message instance is reused, there is no point in freezing its message here
            if (!Constants.FORMAT_MESSAGES_IN_BACKGROUND && msg != null) { // LOG4J2-898: user may choose
                msg.getFormattedMessage(); // LOG4J2-763: ask message to freeze parameters
            }
            this.message = msg;
        }
}

So ,  when I call the getMessage from MutableLogEvent like below, I only can get the instance of MutableLogEvent itself.

public Message getMessage() {
        if (message == null) {
            return (messageText == null) ? EMPTY : this;
        }
        return message;
    }

If I use the ReusableMessageFactory and ReusableLogEventFactory, then when I send an object though API, it seems impossible to get it from MutableLogEvent.

I know I can use other MessageFactory to bypass it, but why MutableLogEvent don't keep the ReusableObjectMessage, is that on purpose? Is there other way to get the ReusableObjectMessage from MutableLogEvent?

Best reagrds.

  was:
After upgrading Log4j from 2.5 to 2.6.2, I found that log event catched by my appender is changed from Log4jLogEvent to MutableLogEvent, it's fine except that I can't get the ObjectMessage from MutableLogEvent. If I sent object message thought log4j API like log.info(object);, I assume object can be obtained in ReusableObjectMessage, but since MutableLogEvent look like don't keep it as below:

public void setMessage(final Message msg) {
        if (msg instanceof ReusableMessage) {
            final ReusableMessage reusable = (ReusableMessage) msg;
            reusable.formatTo(getMessageTextForWriting());
            if (parameters != null) {
                parameters = reusable.swapParameters(parameters);
                parameterCount = reusable.getParameterCount();
            }
        } else {
            // if the Message instance is reused, there is no point in freezing its message here
            if (!Constants.FORMAT_MESSAGES_IN_BACKGROUND && msg != null) { // LOG4J2-898: user may choose
                msg.getFormattedMessage(); // LOG4J2-763: ask message to freeze parameters
            }
            this.message = msg;
        }
}

So ,  when I call the getMessage from MutableLogEvent like below, I only can get the instance of MutableLogEvent itself.

public Message getMessage() {
        if (message == null) {
            return (messageText == null) ? EMPTY : this;
        }
        return message;
    }

I know I can use other MessageFactory to bypass it, but why MutableLogEvent don't keep the ReusableObjectMessage, is that on purpose? Is there other way to get the ReusableObjectMessage from MutableLogEvent?

Best reagrds.


> why MutableLogEvent don't keep the ReusableObjectMessage
> --------------------------------------------------------
>
>                 Key: LOG4J2-1510
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-1510
>             Project: Log4j 2
>          Issue Type: Question
>          Components: Core
>    Affects Versions: 2.6.2
>            Reporter: Maple Wang
>            Priority: Blocker
>
> After upgrading Log4j from 2.5 to 2.6.2, I found that log event catched by my appender is changed from Log4jLogEvent to MutableLogEvent, it's fine except that I can't get the ObjectMessage from MutableLogEvent. If I sent object message though log4j API like log.info(object);, I assume object can be obtained in ReusableObjectMessage, but since MutableLogEvent look like don't keep it as below:
> public void setMessage(final Message msg) {
>         if (msg instanceof ReusableMessage) {
>             final ReusableMessage reusable = (ReusableMessage) msg;
>             reusable.formatTo(getMessageTextForWriting());
>             if (parameters != null) {
>                 parameters = reusable.swapParameters(parameters);
>                 parameterCount = reusable.getParameterCount();
>             }
>         } else {
>             // if the Message instance is reused, there is no point in freezing its message here
>             if (!Constants.FORMAT_MESSAGES_IN_BACKGROUND && msg != null) { // LOG4J2-898: user may choose
>                 msg.getFormattedMessage(); // LOG4J2-763: ask message to freeze parameters
>             }
>             this.message = msg;
>         }
> }
> So ,  when I call the getMessage from MutableLogEvent like below, I only can get the instance of MutableLogEvent itself.
> public Message getMessage() {
>         if (message == null) {
>             return (messageText == null) ? EMPTY : this;
>         }
>         return message;
>     }
> If I use the ReusableMessageFactory and ReusableLogEventFactory, then when I send an object though API, it seems impossible to get it from MutableLogEvent.
> I know I can use other MessageFactory to bypass it, but why MutableLogEvent don't keep the ReusableObjectMessage, is that on purpose? Is there other way to get the ReusableObjectMessage from MutableLogEvent?
> Best reagrds.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org