基本結構
目前(1.7),logback有3個模塊logback-core, logback-classic 和 logback-access.
logback-core是另外兩個模塊的基礎。logback-classic扩展了核心模塊,经典模块對log4j進行類显著改进並自身實現類slf4j-api。称为访问的第三个模块,集成了Servlet容器以提供HTTP访问日志功能。
Logger
确保所有的日誌記錄最终会有继承层次,root logger总是有一个指定的級別。默认情况下,这个级别是debug。任何logger都會繼承root logger。
Example 1
Logger name |
Assigned level |
Effective level |
root
DEBUG
DEBUG
X
none
DEBUG
X.Y
none
DEBUG
X.Y.Z
none
DEBUG
Example 2
Logger name |
Assigned level |
Effective level |
root
ERROR
ERROR
X
INFO
INFO
X.Y
DEBUG
DEBUG
X.Y.Z
WARN
WARN
Example 3
Logger name |
Assigned level |
Effective level |
root
DEBUG
DEBUG
X
INFO
INFO
X.Y
none
INFO
X.Y.Z
ERROR
ERROR
Example 4
Logger name |
Assigned level |
Effective level |
root
DEBUG
DEBUG
X
INFO
INFO
X.Y
none
INFO
X.Y.Z
none
INFO
級別順序
TRACE < DEBUG < INFO < WARN < ERROR
level of
request
p
effective level
q
TRACE |
DEBUG |
INFO |
WARN |
ERROR |
OFF |
TRACE |
YES
NO
NO
NO
NO
NO
DEBUG |
YES
YES
NO
NO
NO
NO
INFO |
YES
YES
YES
NO
NO
NO
WARN |
YES
YES
YES
YES
NO
NO
ERROR |
YES
YES
YES
YES
YES
NO
Appenders 和 Layouts
addAppender方法添加一个appender到给定的logger。
Logger Name |
Attached Appenders |
Additivity Flag |
Output Targets |
Comment |
root
A1
not applicable
A1
Since the root logger stands at the top of the logger hierarchy, the additivity flag does not apply to it.
x
A-x1, A-x2
true
A1, A-x1, A-x2
Appenders of "x" and of root.
x.y
none
true
A1, A-x1, A-x2
Appenders of "x" and of root.
x.y.z
A-xyz1
true
A1, A-x1, A-x2, A-xyz1
Appenders of "x.y.z", "x" and of root.
security
A-sec
false
A-sec
No appender accumulation since the additivity flag is set to
false
. Only appender A-sec will be used.
security.access
none
true
A-sec
Only appenders of "security" because the additivity flag in "security" is set to
false
.
通常,用户不仅希望定制输出目的地,而且希望定制输出格式。
For example, the PatternLayout with the conversion pattern "%-4relative [%thread] %-5level %logger{32} - %msg%n" will output something akin to:
例如,PatternLayout爲"%-4relative [%thread] %-5level %logger{32} - %msg%n"時,將會輸出類似信息:
176 [main] DEBUG manual.architecture.HelloWorld2 - Hello world.
第一个字段是程序運行以来经过的毫秒数。第二个是發起日志请求的线程。第三个字段是日志请求的级别。第四個与日誌请求相關的logger的名字。“-”后是請求的文本消息。
參數化
logger.debug("The new entry is "+entry+".");
logger.debug("The new entry is {}.", entry);
logger.debug("The new entry is {}. It replaces {}.", entry, oldEntry);
後面兩個比第一個更高效。
內部調用
这是一个UML序列图,以info()爲例来显示其內部調用情況。
性能
暫缺