4

I am using log4j2 with following dependency ::

   <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.0-rc1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.0-rc1</version>
    </dependency>

I am using following configuration ::

    <?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">

    <Properties>
        <Property name="LOGGER_HOME">/logs</Property>
    </Properties>

    <Appenders>

        <RollingFile name="application" fileName="${LOGGER_HOME}/application.log"
            filePattern="${LOGGER_HOME}/application.%d{yyyy-MM-dd}_%i.log">

            <PatternLayout pattern="%d{ISO8601}{GMT} %-5p %C{2} (%F:%L) - %m%n" />
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="1 GB" />
            </Policies>

        </RollingFile>

        <RollingFile name="framework" fileName="${LOGGER_HOME}/em-logs/framework.log"
            filePattern="${LOGGER_HOME}/framework.%d{yyyy-MM-dd}_%i.log">

            <PatternLayout pattern="%d{ISO8601}{GMT} %-5p %C{2} (%F:%L) - %m%n" />
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="1 GB" />
            </Policies>
        </RollingFile>

        <Console name="out" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{ISO8601}{GMT} %-5p %C{2} (%F:%L) - %m%n" />
        </Console>

        <Async name="asyncApplication">
            <AppenderRef ref="application" />
        </Async>

        <Async name="asyncFramework">
            <AppenderRef ref="framework" />
        </Async>


    </Appenders>


    <Loggers>

        <Logger name="com.memorynotfound.logging" level="debug"
            includeLocation="true">
            <AppenderRef ref="asyncApplication" />
        </Logger>

    <Root level="debug" includeLocation="true">
            <AppenderRef ref="asyncApplication"></AppenderRef>
        </Root>

        <Logger name="org.axonframework" level="info" additivity="false"
            includeLocation="true">
            <AppenderRef ref="asyncFramework" />
        </Logger>

        <Root level="error" includeLocation="true">
            <AppenderRef ref="out" />
        </Root>

    </Loggers>


</Configuration>

However i am getting logs on the console in the following format

2015-08-20 14:29:41,613 DEBUG logging.LoggerExample (LoggerExample.java:11) - This will be printed on debug

And in the Rolling file i get following pattern where the line number is missing ::

2015-08-20 14:29:41,613 DEBUG ? () - This will be printed on debug

I have gone nuts as nothing seem to work for printing the line numbers i also followed the official log4j2 link Log4j2 Migration but still the result is same as above. If any one has any solution please let me know .

Anand Kadhi
  • 1,790
  • 4
  • 27
  • 40

3 Answers3

3

Here i found the solution :: Log4j2 AsyncLogger with rolling file appender not showing file line number

Then i changed my appender reference to directly point to RollingFile name instead of <Async name> , it is now correctly showing the line number . Not sure why this happens, I will find and soon post the reason.

So changed the following ::

<Logger name="com.memorynotfound.logging" level="debug"
            includeLocation="true">
            <AppenderRef ref="asyncApplication" />
        </Logger>

to

<Logger name="com.memorynotfound.logging" level="debug"
        includeLocation="true">
        <AppenderRef ref="application" />
    </Logger>
Community
  • 1
  • 1
Anand Kadhi
  • 1,790
  • 4
  • 27
  • 40
3

For the consideration of performance, async logger closed the includeLocation by default. If you want to print the line num and class name, you need add <Async name="asyncFramework" includeLocation="true">

FYI https://logging.apache.org/log4j/2.x/manual/layouts.html#LocationInformation

shawn
  • 31
  • 3
3

The document describe the reason that not printing line number . It say you can override the default behaviour in your logger or asynchronous appender configuration by specifying includeLocation="true". But i did not solve the problem that not printing line number after i did it. Maybe because i didn`t understand what it meant, i made the wrong place.

My solution is add %L in Log4j2 PatternLayout pattern. My Log4j2.xml is following:

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="WARN" monitorInterval="30">
        <Properties>
            <Property name="LOG_HOME">./logs</Property>
        </Properties>

        <Console name="Console" target="SYSTEM_OUT">
            <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY" />
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36}:%L - %msg%n" />
        </Console>

        <RollingFile name="RollingFileInfo" fileName="${LOG_HOME}/pinche_quartz.log"
                     filePattern="${LOG_HOME}/test.log-%d{yyyy-MM-dd}.log">
            <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY" />
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36}:%L - %msg%n" />
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
            </Policies>
        </RollingFile>

        <RollingFile name="RollingFileError" fileName="${LOG_HOME}/error/pinche_quartz-error.log"
                     filePattern="${LOG_HOME}/error/pinche_quartz-error.log-%d{yyyy-MM-dd}.log">
            <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY" />
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36}:%L - %msg%n" />
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
            </Policies>
        </RollingFile>
    </Appenders>

    <Loggers>
        <root level="all">
            <appender-ref ref="Console"/>
            <appender-ref ref="RollingFileInfo"/>
            <appender-ref ref="RollingFileError"/>
        </root>
    </Loggers>

</Configuration>

I hope to help you.

Log4j2 document https://logging.apache.org/log4j/2.x/manual/layouts.html#LocationInformation

HK boy
  • 1,398
  • 11
  • 17
  • 25
sunshine
  • 31
  • 2