Java Logging: show the source line number of the caller (not the logging helper method)

Alternative answer.

It is possible to ask log4j to exclude the helper class by using the method

Category.log(String callerFQCN, Priority level, Object message, Throwable t)

and specifying the helper class as ‘callerFQCN’.

For example here is a class using a helper:

public class TheClass {
    public static void main(String...strings) {
        LoggingHelper.log("Message using full log method in logging helper.");
        LoggingHelper.logNotWorking("Message using class info method");

and the code of the helper:

public class LoggingHelper {
private static Logger LOG = Logger.getLogger(LoggingHelper.class);

public static void log(String message) {
    LOG.log(LoggingHelper.class.getCanonicalName(), Level.INFO, message, null);

public static void logNotWorking(String message) {;
} }

The first method will output your expected result.

Line(TheClass.main( Message using full log method in logging helper.
Line(LoggingHelper.logNotWorking( Message using class info method

When using this method, Log4j will work as usual, avoiding calculating the stack trace if it is not required.

Leave a Comment