Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RollingFile Appender Log4j2 not printing Line number

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 .

like image 436
Anand Kadhi Avatar asked Feb 10 '23 06:02

Anand Kadhi


2 Answers

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>
like image 124
Anand Kadhi Avatar answered Feb 11 '23 19:02

Anand Kadhi


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

like image 39
shawn Avatar answered Feb 11 '23 20:02

shawn