logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Remko Popma (JIRA)" <j...@apache.org>
Subject [jira] [Comment Edited] (LOG4J2-599) Support lambda functions (or similar) for log message parameters
Date Fri, 07 Aug 2015 00:11:45 GMT

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

Remko Popma edited comment on LOG4J2-599 at 8/7/15 12:11 AM:
-------------------------------------------------------------

Could we avoid the whole api change thing by having a static method that I could static import
which had a signature something like

public static Object lazyParam(Callable param) { return param; }

Then it would be simple to even mix and match lazy and non-lazy params.

logger.trace("msg {} {}", lazyParam(() -> expensive operation()), nonLazy);



was (Author: bruce.brouwer):
Could we avoid the whole api change thing by having a static method that I
could static import which had a signature something like

public static Object lazyParam(Callable param) { return param; }

Then it would be simple to even mix and match lazy and non-lazy params.

logger.trace("msg {} {}", lazyParam(() -> expensive operation()), nonLazy);
On Aug 6, 2015 7:04 PM, "Remko Popma (JIRA)" <jira@apache.org> wrote:


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

Remko Popma commented on LOG4J2-599:
------------------------------------

That is the use case for this ticket: users on Java 8 will be able to avoid
the explicit level check and instead write
{code}
logger.trace("Some expensive operation returned {}", () ->
expensiveOperation());
{code}

resulting in more compact and cleaner code.

The only logging product that I am aware of that currently supports lambda
expressions for lazy logging is the JDK [Logger|
http://docs.oracle.com/javase/8/docs/api/java/util/logging/Logger.html#log-java.util.logging.Level-java.util.function.Supplier-].
But IMHO the JDK Logger is only doing a partial job: it only lazily
constructs the whole log message. There is no API for lazily constructing
message parameters.

I think keeping up to date with Java language developments makes this a
"strategic" feature that adds to the reasons why Log4j 2 is the logging
library of choice for our users.

equivalent: interfaces with a single empty-parameter message call), or more
simply, allow Runnables (or something similar) to be passed which will be
dynamically executed if the log message is enabled.
is a performance issue that is already solved quite well, the problem of
adding in information to the log message that makes other calculations
still needs to be wrapped in an if check.
interface, or try to emulate how Java 1.8 lambdas work via an interface
with a single method defined. The details here would still need to be
fleshed out, but I think this sort of feature could be rather handy
(especially in a Java 1.8+ environment, or in Groovy/Scala/etc.).



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


> Support lambda functions (or similar) for log message parameters
> ----------------------------------------------------------------
>
>                 Key: LOG4J2-599
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-599
>             Project: Log4j 2
>          Issue Type: Brainstorming
>          Components: Core
>            Reporter: Matt Sicker
>            Priority: Minor
>              Labels: Java8
>
> It would be nice if we could support 0-param lambda functions (or the equivalent: interfaces
with a single empty-parameter message call), or more simply, allow Runnables (or something
similar) to be passed which will be dynamically executed if the log message is enabled.
> The use case here is that although string construction of the log message is a performance
issue that is already solved quite well, the problem of adding in information to the log message
that makes other calculations still needs to be wrapped in an if check.
> I'm not sure if it'd be best to just use Runnable, or create a new interface, or try
to emulate how Java 1.8 lambdas work via an interface with a single method defined. The details
here would still need to be fleshed out, but I think this sort of feature could be rather
handy (especially in a Java 1.8+ environment, or in Groovy/Scala/etc.).



--
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


Mime
View raw message