logback官方文档阅读笔记(一)


前言


logback官方文档html

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

下一篇笔记关于官方文档第二章:笔记(二)app

正文


logback的角色定位


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的接口,即编写代码时遵循日志规约而不考虑日志规约的具体实现,从而将日志的实现与软件解耦,将关注点回归到软件自己。less


要求logback输出内部准备过程当中的状态信息


2.jpg

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

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

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


Appender的简单介绍——输出器


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以适配特定的场景。spa

具体如何配置这里没有讲,天然是期待日后看会有了。翻译


logback使用的套路模板


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

  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的重点就在于第一步的配置。

相关文章
相关标签/搜索