Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clickable classname and line number in console using log4j

The log4j.properties below displays the location of the logging event using %l. The output is a bit too long with the fully qualified classname and method name.

# root logger option
log4j.rootLogger=INFO, stdout, file

# redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%p: %d{yyyy-MM-dd HH:mm:ss} %l] %m%n

# redirect log messages to a log file, support file rolling
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=${catalina.home}/logs/mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p: %d{yyyy-MM-dd HH:mm:ss} %l] %m%n

Actual Output:

enter image description here

[INFO: 2015-08-30 14:24:07 com.mypackage.MyAction.execute(MyAction.java:64)] This is an INFO message.

What is the correct ConversionPattern in the properties file so it only takes the clickable portion of the location?

Intended Output:

[INFO: 2015-08-30 14:24:07 MyAction.java:64] This is an INFO message.

Where:

MyAction.java:64 is clickable in the console.

like image 971
silver Avatar asked Jan 07 '23 16:01

silver


1 Answers

If you mean the Console output in Eclipse, you can get a klickable link.

An appender like this should work:

<appender name="console" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-5p %m (%F:%L) in %t%n"/>
    </layout>
</appender>

It works, because the Eclipse Console parses the pattern (FileName.java:lineNumber) to be a link to FileName.java at line lineNumber.

An easy test (creates a link to line that jumps to line 6) is:

public class TestLink
{
    public static void main(String[] args)
    {
        System.out.println("(" + new TestLink().getClass().getSimpleName()
            + ".java:" + 6 + ")");
    }
}
like image 172
Burkhard Avatar answered Jan 10 '23 06:01

Burkhard