[
https://issues.apache.org/jira/browse/LOG4J2-1913?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16016584#comment-16016584
]
Gary Gregory commented on LOG4J2-1913:
--------------------------------------
Hi Corneliu,
The logIfEnabled() logic MUST have a Message. This allows filters to use the
Message from {{org.apache.logging.log4j.spi.ExtendedLogger.isEnabled(Level,
Marker, Message, Throwable)}}.
The flip side would be to check if the logger is enabled twice, which has its
own cost. Once in exit(final String fqcn, final R result) and another in
logIfEnabled().
Feel free to provide a patch. Make sure a full build passes (mvn clean install)
that things are not worse ;-)
Gary
> traceExit allocates memory for the input parameter even if it doesn't print it
> ------------------------------------------------------------------------------
>
> Key: LOG4J2-1913
> URL: https://issues.apache.org/jira/browse/LOG4J2-1913
> Project: Log4j 2
> Issue Type: Improvement
> Components: API
> Affects Versions: 2.8.2
> Reporter: Corneliu C
> Labels: performance
>
> When calling traceExit(object instance) the underlying code allocates a
> Message for the input object instance although the log level is not TRACE and
> there won't be any printing in the end.
> With trivial objects is not a problem but using large lists or XML objects
> results in large memory allocation for nothing which goes to GC and
> performance impact.
> Execution flow is like this:
> {code}
> @Override
> public <R> R traceExit(final R result) {
> return exit(FQCN, null, result);
> }
> {code}
> next _exit_ calls:
> {code}
> protected <R> R exit(final String fqcn, final R result) {
> logIfEnabled(fqcn, Level.TRACE, EXIT_MARKER, exitMsg(null, result),
> null);
> return result;
> }
> {code}
> which calls _exitMsg_ with the input parameter _result_:
> {code}
> protected Message exitMsg(final String format, final Object result) {
> if (result == null) {
> if (format == null) {
> return messageFactory.newMessage("Exit");
> }
> return messageFactory.newMessage("Exit: " + format);
> }
> if (format == null) {
> return messageFactory.newMessage("Exit with(" + result + ')');
> }
> return messageFactory.newMessage("Exit: " + format, result);
> }
> {code}
> As it can be seen the _exitMsg()_ method is called and only later the
> _logIfEnabled()_ is executed.
> I assume a log level validation can be applied upfront so as _exitMsg_ is not
> called for nothing.
> *Note:* this is not the case for other flavors of _traceExit_ which check the
> log level upfront.
> Thank you
--
This message was sent by Atlassian JIRA
(v6.3.15#6346)