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

前端 未结 8 1373
太阳男子
太阳男子 2020-12-08 03:50

The numerous (sigh...) logging frameworks for Java all do a nice job of showing the line number of the source file name for the method that created the log message:

8条回答
  •  广开言路
    2020-12-08 04:36

    Comes out that there is a very simple solution, just add FQCN (The wrapper class' fully qualified class name) to your logger helper:

    public class MyLogger extends Logger {
    
    private static final String FQCN = MyLogger.class.getName() + ".";
    
    protected MyLogger(String name) {
        super(name);
    }
    
    public void info(final Object msg) {
        super.log(FQCN, Level.INFO, msg, null);
    }
    
    //etc...
    

    In Your working class you just do:

    public class MyClass {
    
    private static final Logger LOG = MyLogger.getLogger();   
    
    private void test()
    {
        LOG.info("test");
    }
    
    }
    

提交回复
热议问题