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

Stefan Leonhartsberger commented on LOG4J2-1028:
------------------------------------------------

I'm copying the promised JUnit Test in here. Let me know if I should add it 
somewhere else. 
The test contains 2 test methods - One should fail using the default Log4j2 
implementation and one pass with a custom implementation.

{code:title=Log4j2LogLineTest.java}
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.junit.Assert;
import org.junit.Test;

/**
 * Simple Log4J2 Log Line Test
 */
public class Log4j2LogLineTest {

    @Test
    public void logLineNumberTestLog4j2() {
        A a = new A();
        int lineNumber = a.doLog();
        Assert.assertTrue("LineNumber should be 62 but was: " + lineNumber, 
lineNumber == 62);
    }

    @Test
    public void logLineNumberTestCustom() {
        A a = new A();
        int lineNumber = a.doLogCustom();
        Assert.assertTrue("LineNumber should be 65 but was: " + lineNumber, 
lineNumber == 65);
    }

    private class A {
        B b = new B();

        public int doLog() {
            return b.doLog();
        }

        public int doLogCustom() {
            return b.doLogCustom();
        }
    }

    private class B {
        C c = new C();

        public int doLog() {
            return c.doLog();   //Line No: 42
        }

        public int doLogCustom() {
            return c.doLogCustom();
        }
    }

    private class C {
        Logger logger = LogManager.getLogger(C.class);
        public int doLog() {
            return this.doLogPrivate(true); //Line No: 53
        }

        public int doLogCustom() {
            return this.doLogPrivate(false);
        }

        private int doLogPrivate(final boolean bLog4jLocationCalculation) {
            if (bLog4jLocationCalculation) {
                StackTraceElement element = 
Log4jLogEvent.calcLocation(logger.getName()); //Line No: 62
                return element.getLineNumber();
            } else {
                StackTraceElement element = 
Log4j2LogLineTest.calcLocation(logger.getName()); //Line No: 65
                return element.getLineNumber();
            }
        }
    }

    /**
     * Custom implementation of log line calculation.
     */
    public static StackTraceElement calcLocation(final String fqcnOfLogger) {
        if (fqcnOfLogger == null) {
            return null;
        }
        final StackTraceElement[] stackTrace = 
Thread.currentThread().getStackTrace();
        for (int i = 0; i < stackTrace.length; i++) {
            final String className = stackTrace[i].getClassName();
            if (fqcnOfLogger.equals(className)) {
                return stackTrace[i];
            }
        }
        return null;
    }
}

{code}

> Incorrect Line Number in location information
> ---------------------------------------------
>
>                 Key: LOG4J2-1028
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-1028
>             Project: Log4j 2
>          Issue Type: Bug
>          Components: Core
>    Affects Versions: 2.3
>         Environment: Java 1.8.0_45 64-bit
> Maven 3.3.1
> Windows 7
> basically any...
>            Reporter: Stefan Leonhartsberger
>
> When enabling location information in Log4j2
> The line numbers of the log records are incorrect 
> as they are only taking into account the first time in the stacktrace where 
> the 
> class in question (FQCN) was called
> This however may not be accurate as the class may call methods within itself  
> adding to the stacktrace - these are not considered when gathering location 
> information within Log4jLogEvent.
> An example stacktrace:
> {code}
> stackTrace = {java.lang.StackTraceElement[18]@4612} 
>  0 = {java.lang.StackTraceElement@4617} 
> "java.lang.Thread.getStackTrace(Thread.java:1552)"
>  1 = {java.lang.StackTraceElement@4618} 
> "org.apache.logging.log4j.core.impl.Log4jLogEvent.calcLocation(Log4jLogEvent.java:386)"
>  2 = {java.lang.StackTraceElement@4619} 
> "org.apache.logging.log4j.core.async.AsyncLogger.location(AsyncLogger.java:293)"
>  3 = {java.lang.StackTraceElement@4620} 
> "org.apache.logging.log4j.core.async.AsyncLogger.logMessage(AsyncLogger.java:252)"
>  4 = {java.lang.StackTraceElement@4621} 
> "org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:738)"
>  5 = {java.lang.StackTraceElement@4622} 
> "org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:708)"
>  6 = {java.lang.StackTraceElement@4623} 
> "com.test.logging.factory.Log4j2Factory$Log4J2CatsLogger.log(Log4j2Factory.java:106)"
>  7 = {java.lang.StackTraceElement@4624} 
> "com.test.agent.configuration.Configuration.setPttId(Configuration.java:1079)"
>  8 = {java.lang.StackTraceElement@4625} 
> "com.test.agent.configuration.Configuration.loadDefaultConfiguration(Configuration.java:323)"
>  9 = {java.lang.StackTraceElement@4614} 
> "com.test.agent.configuration.Configuration.<init>(Configuration.java:302)"
>  10 = {java.lang.StackTraceElement@4613} 
> "com.test.agent.configuration.ConfigurationParser.createConfig(ConfigurationParser.java:51)"
>  11 = {java.lang.StackTraceElement@4626} 
> "com.test.agent.AgentMain.initConfig(AgentMain.java:231)"
>  12 = {java.lang.StackTraceElement@4627} 
> "com.test.agent.AgentMain.init(AgentMain.java:204)"
>  13 = {java.lang.StackTraceElement@4628} 
> "com.c4i.test.agent.AgentBase.init(AgentBase.java:456)"
>  14 = {java.lang.StackTraceElement@4629} 
> "com.test.agent.AgentBase.createAgent(AgentBase.java:194)"
>  15 = {java.lang.StackTraceElement@4630} 
> "com.test.agent.AgentBase.agentmain(AgentBase.java:141)"
>  16 = {java.lang.StackTraceElement@4631} 
> "com.test.agent.AgentMain.main(AgentMain.java:109)"
>  17 = {java.lang.StackTraceElement@4632} 
> "com.test.agent.AgentMain.main(AgentMain.java:86)"
> {code} 
> Expected Line Number: 1079
> Received Line Number: 302
> Reason is that the stack is iterated from the wrong direction - i=17. 
> (starting with i=0 would result in the correct behaviour.



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

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to