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

相關文章
相關標籤/搜索