乐趣区

logback官方文档阅读笔记一

logback 官方文档

本文关于官方文档第一章:introduction


Note that the above example does not reference any logback classes. In most cases, as far as logging is concerned, your classes will only need to import SLF4J classes. Thus, the vast majority, if not all, of your classes will use the SLF4J API and will be oblivious to the existence of logback.

这一段言下之意即 logback 是 slf4j 日志系统规约的实现。所以在软件中使用的均为 slf4j 的接口,即编写代码时遵循日志规约而不考虑日志规约的具体实现,从而将日志的实现与软件解耦,将关注点回归到软件本身。


这一段告知了如何要求日志系统输出内部状态,从而便于调试日志系统相关问题。

比较搞人的事情是,一开始提到的 StatusManager 实际上并没被用上。代码中用 LoggerFactory.getILoggerFactory(); 得到的实际是 ILoggerFactory,从命名和本段代码来看,这应该是一个接口,而后LoggerContext 应该是这个接口的一个实现类。

推测 LoggerFactory 是一个单例模式的类,其内有一个 ILoggerFactory 接口的成员变量,随着单例的 LoggerFactory 生成被赋值,跟踪负责 LoggerFactory 和它生产的 Logger 状态。


An Appender is a class that can be seen as an output destination. Appenders exist for many different destinations including the console, files, Syslog, TCP Sockets, JMS and many more. Users can also easily create their own Appenders as appropriate for their specific situation.

介绍了类 Appender, 从这段文字来看,不妨将Appender 翻译理解为输出器。类 Appender 可以为控制台,文件等多种输出目标服务,用户也可以自定义自己的 Appender 以适配特定的场景。

具体如何配置这里没有讲,自然是期待往后看会有了。


Here is a list of the three required steps in order to enable logging in your application.

  1. Configure the logback environment. You can do so in several more or less sophisticated ways. More on this later.
  2. In every class where you wish to perform logging, retrieve a Logger instance by invoking the org.slf4j.LoggerFactory class’ getLogger() method, passing the current class name or the class itself as a parameter.
  3. Use this logger instance by invoking its printing methods, namely the debug(), info(), warn() and error() methods. This will produce logging output on the configured appenders.

讲了使用 logback 的基础步骤。后两者其实是不太会变化的,学习 logback 的重点就在于第一步的配置。


退出移动版