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

Stefan Küttner edited comment on LOG4J2-1570 at 1/13/19 8:15 PM:
-----------------------------------------------------------------

If one starts using functional syntax this issue becomes more serious as more 
and more log entries are not attributable to the logging class/line number, 
making logs less useful for diagnostics.

Another Example - with only one method - that will produce a weird outcome 
telling you the log entry's source was {{Optional.ifPresent}}!:

{code:java}
package com.exxeta.log4j.test;

import java.util.Optional;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.appender.ConsoleAppender;
import org.apache.logging.log4j.core.config.Configurator;
import 
org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder;
import 
org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory;
import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration;

public class Log4jThreadAnalyzerErrorDemo
{
    private static final Logger LOG;

    static
    {
        configureRootLogger();
        LOG = LogManager.getRootLogger();
    }

    public static void main(String[] args)
    {
        // This produces "INFO main - (Optional.java:159) - Optional.ifPresent: 
Hello World!"
        Optional.of("Hello World!").ifPresent(LOG::info);
    }

    private static void configureRootLogger()
    {
        ConfigurationBuilder<BuiltConfiguration> builder = 
ConfigurationBuilderFactory
                .newConfigurationBuilder();
        AppenderComponentBuilder console = builder.newAppender("Stdout", 
"CONSOLE").addAttribute("target",
                ConsoleAppender.Target.SYSTEM_OUT);
        console.add(builder.newLayout("PatternLayout").addAttribute("pattern",
                "%n%d{dd.MM.yyyy HH:mm:ss} %-5p %t - (%F:%L) - %C{1}.%M: 
%m%n"));
        builder.add(console);
        
builder.add(builder.newRootLogger(Level.INFO).add(builder.newAppenderRef("Stdout")));
        Configurator.initialize(builder.build());
    }
}
{code}


was (Author: stefan.kuettner):
If one starts using functional syntax this issue becomes more serious as more 
and more log entries are not attributable to the logging class/line number, 
making logs less useful for diagnostics.

> Logging with a lambda expression with a method call that also logs causes 
> logs within method call to reference line num and method name as parent method
> --------------------------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: LOG4J2-1570
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-1570
>             Project: Log4j 2
>          Issue Type: Bug
>          Components: API
>    Affects Versions: 2.6.2
>            Reporter: Joel Berta
>            Priority: Minor
>              Labels: easyfix, newbie
>
> While logging a message using lambda expressions. If i call a method within 
> the log message (i.e logger.info(() -> "foo" +bar()) which also logs 
> messages, those logs within the called method will reference the line number 
> and method name of the method which invoked it.
> Running the following code demonstrates this issue:
> {code:title=TestingLogging.java|borderStyle=solid}
>  private static final Logger LOGGER = 
> LogManager.getLogger(TestingLogging.class);
>     public static String foo(){
>         LOGGER.info("bar");
>         return "foo";
>     }
>     public static void main(String[] args){
>         System.out.println("correct method reference: ");
>         LOGGER.info(foo());
>         System.out.println("\nincorrect method reference: ");
>         // causes logging in foo to reference
>         // this line number and method (main)
>         LOGGER.info(() -> foo());
>     }
> {code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to