Log4j vs. Logback 性能比较Inside

简介: 看过源码有段时间了,对logback中对判定是否记录一条日志记录的优化印象比较深刻,整理如下,希望对你有所帮助。   Java的logger框架有一个重要的层级(hierarchy)的概念,按约定,是通过小数点来区别父子关系的,也就是名字为“x.y”的logger是“x.y.z”logger的parent。 Hierachy的命名规则
看过源码有段时间了,对logback中对判定是否记录一条日志记录的优化印象比较深刻,整理如下,希望对你有所帮助。
 
Java的logger框架有一个重要的层级(hierarchy)的概念,按约定,是通过小数点来区别父子关系的,也就是名字为“x.y”的logger是“x.y.z”logger的parent。
Hierachy的命名规则:
Named Hierarchy
A logger is said to be an  ancestor of another logger if its name followed by a dot is a prefix of the  descendant logger name. A logger is said to be a  parent of a  child logger if there are no ancestors between itself and the descendant logger.
这样做的好处是我们可以灵活的指定不同logger的log level和Appender targets。
Logger
Name
Assigned
Level
Inherited
Level
Added
Appenders
Additivity
Flag
Output Targets Comment
root warn
warn
A1 not applicable A1 The root logger is anonymous but can be accessed with the Logger.getRootLogger() method.
There is no default appender attached to root.
x info
info
A-x true A1, A-x Appenders of "x" and root.
x.y none
info
A-xy false A1, A-x, A-xy Appenders in "x.y" , and its parents "x" and root. Log level is info which is inherited from "x"
x.y.z
debug
debug
A-xyz true A-xyz Appenders of "x.y.z". becuase its parent "x.y" addivitity is false, so no more appenders will be added
 
因为这个hierachy的关系,在做日志输出时,logger不仅要check自己是不是满足输出条件,还要遍历其所有的Additivity为非false的parent,看parents们能不能输出日志。
 
下图是一个Filter和Handler在Logger Hierarchy中运作的示意图,其运行逻辑是:

When a message is passed to a Logger, the message is passed through the Logger's Filter, if the Loggerhas a Filter set. The Filter can either accept or reject the message. If the message is accepted, the message is forwarded to the Handler's set on the Logger. If no Filter is set, the message is always accepted.

If a message is accepted by the Filter, the message is also forwarded to the Handler's of the parentLogger's. However, when a message is passed up the hierarchy, the message is not passed through theFilter's of the parent Logger's. The Filter's are only asked to accept the message when the message is passed directly to the Logger, not when the message comes from a child Logger.

 

更多详细请参考 http://tutorials.jenkov.com/java-logging/logger-hierarchy.html
 
log4j中是怎样处理这个hierachy的呢?
 
上面的实现有两个比较损耗性能的地方:
1)当日志不需要输出时,new LoggingEvent()纯属是做无用功。
2)当logger的日志自身不满足输出条件是,遍历parents也是在做无用功。
 
logback中是怎样做优化的呢?
 
logback 还有一个比较明显的优化点(贤亮补充)

在log4j中,当某个logger调用关联的appenders进行输出前会先通过synchronized加锁,如下代码所示:

但是在logback中,只会在某个appender要输出日志时,使用ReentrantLock来进行加锁,如下代码所示:

这两者的主要区别不在于ReentrantLock和synchronized,因为最新的JVM中,这两个性能不会相差太多,主要区别在于加锁粒度上,当大并发的情况下,logback会有明显的优势,这个原理和ConcurrentHashMap的分段锁类似。

对于其他的优化点,可以考虑自己做一些性能测试+阅读源码,体会会更深一些。
 
参考:
相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
相关文章
|
3月前
|
Java Spring
【Spring Boot】logback和log4j日志异常处理
【1月更文挑战第25天】【Spring Boot】logback和log4j日志异常处理
|
5月前
|
XML 监控 Java
JAVA日志技术 & Logback
为什么需要记录日志?我们不可能实时的24小时对系统进行人工监控,那么如果程序出现异常错误时要如何排查呢?并且系统在运行时做了哪些事情我们又从何得知呢?这个时候日志这个概念就出现了,日志的出现对系统监控和异常分析起着至关重要的作用。......
42 0
|
23天前
|
监控 Java 测试技术
日志框架到底是Logback 还是 Log4j2
日志框架到底是Logback 还是 Log4j2
17 0
QGS
|
3月前
|
Java 数据库连接 Apache
Springboot日志框架logback与log4j2
Springboot日志框架logback与log4j2
QGS
37 0
|
6月前
|
Java 数据库连接 API
快速了解常用日志技术(JCL、Slf4j、JUL、Log4j、Logback、Log4j2)
快速了解常用日志技术(JCL、Slf4j、JUL、Log4j、Logback、Log4j2)
41 0
|
7月前
|
XML 安全 Java
logback的使用和配置|logback比log4j的优点|logback是一个更好的log4j
logback的使用和配置|logback比log4j的优点|logback是一个更好的log4j
|
7月前
|
缓存 移动开发 Java
统一日志的处理Slf4j,log4j,logback
统一日志的处理Slf4j,log4j,logback
133 0
最通俗易懂的 JAVA slf4j,log4j,log4j2,logback 关系与区别以及完整集成案例
最通俗易懂的 JAVA slf4j,log4j,log4j2,logback 关系与区别以及完整集成案例
最通俗易懂的 JAVA slf4j,log4j,log4j2,logback 关系与区别以及完整集成案例
|
8月前
|
Java 数据库连接 API
Mybatis日志Log4j与Logback
Log4j是最早的Java日志框架之一,具有较长的历史;然而Log4j 1.x版本在性能和并发性方面存在一些限制,并且已经停止维护。 Logback是由Log4j创始人Ceki Gülcü开发的后继项目,旨在解决Log4j的问题,并提供更好的性能和可靠性,因此Logback可以被视为Log4j的升级版。 Logback是Log4j的升级版,具有更好的性能、效率和灵活性。如果你正在开始一个新项目或者计划升级现有项目的日志框架,Logback是一个较好的选择。
74 1
|
11月前
|
消息中间件 JavaScript 小程序
为什么阿里巴巴禁止工程师直接使用日志系统(Log4j、Logback)中的 API
为什么阿里巴巴禁止工程师直接使用日志系统(Log4j、Logback)中的 API