masiboo
masiboo

Reputation: 4719

Log4j 2 logging output message with link to the file number

Previously I used log4j version 1 where I had following log4j.xml configuration file:-

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
    <param name="threshold" value="info" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level Line: %L  - %msg%n" />
    </layout>
</appender>

<category name="com.ma.dev" additivity="false">
    <priority value="INFO" />  <!-- <priority value="${myinfo}" /> --> 
    <appender-ref ref="STDOUT" />
    <appender-ref ref="LOGFILE" />
</category>


<root>
    <priority value="WARN" />
    <appender-ref ref="UNMAPFILE" />
</root>

</log4j:configuration>

This config generate following log output where I have link to the logging line:-

enter image description here

I tried trying to get similar log output for log4j version 2 with following config file:-

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Properties>
        <Property name="priorityLevel">info</Property>
    </Properties>

    <Appenders>
        <Console name="myConsole" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%thread] %-5level Line: %L  - %msg%n"/>
        </Console>
    </Appenders>

    <Loggers>
        <Root level="${priorityLevel}">
            <AppenderRef ref="myConsole"/>
        </Root>
    </Loggers>
</Configuration> 

I got the output as:-

enter image description here

No link. How can I have link in Log4j version 2?

Upvotes: 0

Views: 626

Answers (2)

Justin Lam
Justin Lam

Reputation: 21

The link pattern in IntelliJ console seems to be "class(file:line)".

You can try to set pattern to:

%d{yyyy-MM-dd HH:mm:ss} %highlight{%-5p} %c{1}:%C(%F:%L) - %m%n

Upvotes: 2

DylanRodgers98
DylanRodgers98

Reputation: 11

In Log4j1, %l meant “location”, so where you have %level you get the logger location + evel.

In Log4j2, location has changed to %c which outputs the name of the logger (which is the class name, if you’re instantiating your loggers correctly with LoggerFactory.getLogger(Foo.class) where foo is your class’ name.

%level prints the logging level, so you will need to change this to %c in your PatternLayout configuration to get the same output as you are when using Log4j1:

<PatternLayout pattern="%d{HH:mm:ss.SSS} [%thread] %c %-5level Line: %L  - %msg%n"/>

Check out the Layout page from the log4j2 documentation and scroll down to PatternLayout for more https://logging.apache.org/log4j/2.x/manual/layouts.html

Upvotes: 1

Related Questions