silver silver - 1 year ago 79
Java Question

Clickable classname and line number in console using log4j

below displays the location of the logging event using
. 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.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.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(] This is an INFO message.

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

Intended Output:

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

is clickable in the console.

Answer Source

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"/>

It works, because the Eclipse Console parses the pattern ( to be a link to 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 + ")");