log4j2(四) - 日志位置是怎么获取到的?有什么影响?

x33g5p2x  于2021-12-25 转载在 其他  
字(2.3k)|赞(0)|评价(0)|浏览(421)

日志的位置信息包含哪些?

  • %C or $class:类名
  • %F or %file:文件名
  • %L or %line:打印日志的方法所在文件的行数
  • %M or %method:打印日志的方法名
  • %l or %location:包含以上的位置信息

打印日志位置有什么影响?

官方文档上强调了三遍,位置使用要慎用慎用。相比于不使用位置信息:

  • 对于同步日志来说,速度要慢3~5倍
  • 对于异步日志来说,速度要慢30~100

日志位置是怎么获取到的?

提示:includeLocation必须得设置为true

以AsyncLogger为例, 从处理日志事件开始看起:

  • AsyncLoggerConfig
protected void callAppenders(final LogEvent event) {
    	//填充属性
        populateLazilyInitializedFields(event);
        ///把日志事件入队
        if (!delegate.tryEnqueue(event, this)) {
            handleQueueFull(event);
        }
    }
   
   private void populateLazilyInitializedFields(final LogEvent event) {
   		//这里
        event.getSource();
        event.getThreadName();
    }
  • MutableLogEvent
public StackTraceElement getSource() {
        if (source != null) {
            return source;
        }
        //检查是否includeLocation为true
        if (loggerFqcn == null || !includeLocation) {
            return null;
        }
        //获取位置信息的步骤
        source = StackLocatorUtil.calcLocation(loggerFqcn);
        return source;
    }
  • StackLocatorUtil
public static StackTraceElement calcLocation(final String fqcnOfLogger) {
        return stackLocator.calcLocation(fqcnOfLogger);
    }
  • StackLocator
public StackTraceElement calcLocation(final String fqcnOfLogger) {
        if (fqcnOfLogger == null) {
            return null;
        }
        // LOG4J2-1029 new Throwable().getStackTrace is faster than Thread.currentThread().getStackTrace().
        //这个就是获取位置的真正方法了,通过创建一个异常来获取栈信息
        final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
        StackTraceElement last = null;
        for (int i = stackTrace.length - 1; i > 0; i--) {
            final String className = stackTrace[i].getClassName();
            if (fqcnOfLogger.equals(className)) {
                return last;
            }
            last = stackTrace[i];
        }
        return null;
    }
  • StackTraceElement: 我们想要的位置信息
public final class StackTraceElement implements java.io.Serializable {
    // Normally initialized by VM (public constructor added in 1.5)
    private String declaringClass;
    private String methodName;
    private String fileName;
    private int    lineNumber;
    //省略部分代码...
}
  • Throwable
public StackTraceElement[] getStackTrace() {
        return getOurStackTrace().clone();
    }

    private synchronized StackTraceElement[] getOurStackTrace() {
        //初始化栈信息
        if (stackTrace == UNASSIGNED_STACK ||
            (stackTrace == null && backtrace != null) /* Out of protocol state */) {
            int depth = getStackTraceDepth();
            stackTrace = new StackTraceElement[depth];
            //填充栈信息
            for (int i=0; i < depth; i++)
                stackTrace[i] = getStackTraceElement(i);
        } else if (stackTrace == null) {
            return UNASSIGNED_STACK;
        }
        return stackTrace;
    }
 	
 	//每一层的栈信息是通过本地方法来调用的
 	native StackTraceElement getStackTraceElement(int index);

小结

日志位置以前是通过Thread.currentThread().getStackTrace()获取的, 在2.5版本之后,采用new Throwable().getStackTrace()的方式来获取,性能上有了一定的提升的。改动见jira, 里边有bench mark。

相关文章