log4j2谨慎使用位置信息

原文引用:

If one of the layouts is configured with a location-related attribute like HTML locationInfo, or one of the patterns %C or %class, %F or %file, %l or %location, %L or %line, %M or %method, Log4j will take a snapshot of the stack, and walk the stack trace to find the location information.

This is an expensive operation: 1.3 - 5 times slower for synchronous loggers. Synchronous loggers wait as long as possible before they take this stack snapshot. If no location is required, the snapshot will never be taken.

However, asynchronous loggers need to make this decision before passing the log message to another thread; the location information will be lost after that point. The performance impact of taking a stack trace snapshot is even higher for asynchronous loggers: logging with location is 30-100 times slower than without location. For this reason, asynchronous loggers and asynchronous appenders do not include location information by default.

You can override the default behaviour in your logger or asynchronous appender configuration by specifying includeLocation="true".

翻译如下:

如果其中一个布局配置了与位置相关的属性(例如 HTML locationInfo),或者配置了模式%C 或$ class%F 或%file%l 或%location%L 或%line%M 或%方法之一,则 Log4j 会获取堆栈的快照,并遍历堆栈跟踪以查找位置信息。

这是一项昂贵的操作:对于同步 Logger,速度要慢 1.3-5 倍。同步 Logger 在获取此堆栈快照之前会 await 尽可能长的时间。如果不需要位置,将永远不会拍摄快照。

但是,异步 Logger 需要在将日志消息传递到另一个线程之前做出决定。之后,位置信息将丢失。对于异步 Logger 而言,获取堆栈跟踪快照的performance impact甚至更高:有位置的记录比没有位置的记录慢 30-100 倍。因此,默认情况下,异步 Logger 和异步附加器不包含位置信息。

您可以通过指定 includeLocation =“ true”覆盖 Logger 或异步附加程序配置中的默认行为。

不要自动获取日志位置信息

常见日志器中 如 log4j/logback 提供的自动获取类、方法、文件名、文件行是非常耗性能的,生产环境中不要使用,特别是日志级别低的或大量触发写日志的(很多拿ERROR写跟踪日志)更不能使用,排查方式 pattern 中出现如下变量的均是这类情况:
%C or %class, %F or %file, %l or %location, %L or %line, %M or %method

详细参见:
https://logging.apache.org/log4j/2.x/manual/layouts.html#LocationInformation
http://logback.qos.ch/manual/layouts.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值