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

Nick Williams commented on LOG4J2-599:
--------------------------------------

I've been thinking about this a lot. We wouldn't even have to change the API, 
per se, just the documentation for the API. Essentially, for all ***(String, 
Object...) methods, we say, "Any parameters that are strings are used as-is. 
Any parameters that implement {{java.util.function.Supplier}} or 
{{java.util.concurrent.Concurrent}} will be invoked and the return value will 
replace the parameter. Otherwise, {{toString}} will be called on the parameter."

Then, our Java 8 users can simply do this:

{code:java}logger.info("Message with a complex parameter {}.", (Supplier)() -> 
{ /* code that returns parameter value */ });{code}

> 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
>
> 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.2#6252)

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

Reply via email to