logback官方文档阅读笔记:调试logback

前言

主要涉及如何让logback输出自己被初始化被使用背后内部过程里产生的信息。

正文

使用StatusPrinter#print方法

chapter 1

Logback can report information about its internal state using a built-in status system. Important events occurring during logback's lifetime can be accessed through a component called StatusManager. For the time being, let us instruct logback to print its internal state by invoking the static print() method of the StatusPrinter class.

这一段告知了如何使用`StatusPrinter#print方法达成日志系统输出内部变动过程信息的目的。

之后用了一个真实的例子的代码和输出。

package chapters.introduction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.util.StatusPrinter;
public class HelloWorld2 {
  public static void main(String[] args) {
    Logger logger = LoggerFactory.getLogger("chapters.introduction.HelloWorld2");
    logger.debug("Hello world.");
    // print internal state
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    StatusPrinter.print(lc);
  }
}
12:49:22.203 [main] DEBUG chapters.introduction.HelloWorld2 - Hello world.
12:49:22,076 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
12:49:22,078 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
12:49:22,093 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.xml]
12:49:22,093 |-INFO in ch.qos.logback.classic.LoggerContext[default] -  Setting up default configuration.

举的例子讲了logback读取配置的过程,可以看到在经历读取logback.groovy,logback-test.xml,logback.xml失败后logback选择使用自己的默认配置。

输出logback内部过程的信息:就连不是错误和警告的普通信息也要看

chapter 3

If warnings or errors occur during the parsing of the configuration file, logback will automatically print its internal status data on the console. Note that to avoid duplication, automatic status printing is disabled if the user explicitly registers a status listener (defined below).

默认地,如果logback在读取并解析配置文件时出现了错误或警告,才会输出信息到输出台。

而为了避免重复输入,如果使用了状态监听器(就比如下文的那个StatusPrinter),那么这一步的输出将不会有。

那么当除了错误和警告,还希望了解logback读取解析配置文件配置自身过程中的更多详细信息怎么办?继续看:

In the absence of warnings or errors, if you still wish to inspect logback's internal status, then you can instruct logback to print status data by invoking the print() of the StatusPrinter class. The MyApp2 application shown below is identical to MyApp1 except for the addition of two lines of code for printing internal status data.

public static void main(String[] args) {
// assume SLF4J is bound to logback in the current environment
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
// print logback's internal status
StatusPrinter.print(lc);
...
}

Instead of invoking StatusPrinter programmatically from your code, you can instruct the configuration file to dump status data, even in the absence of errors. To achieve this, you need to set the debug attribute of the configuration element, i.e. the top-most element in the configuration file, as shown below. Please note that this debug attribute relates only to the status data. It does not affect logback's configuration otherwise, in particular with respect to logger levels. (If you are asking, no, the root logger will not be set to DEBUG.)

 
 


%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n





Setting debug="true" within the element will output status information, assuming that:

the configuration file is found
the configuration file is well-formed XML.
If any of these two conditions is not fulfilled, Joran cannot interpret the debug attribute since the configuration file cannot be read. If the configuration file is found but is malformed, then logback will detect the error condition and automatically print its internal status on the console. However, if the configuration file cannot be found, logback will not automatically print its status data, since this is not necessarily an error condition. Programmatically invoking StatusPrinter.print() as shown in the MyApp2 application above ensures that status information is printed in every case.

之后还有几段文字与此有关,但我认为意义不大,不再复制。

以上就是想要更详细地了解logback读取并解析配置文件配置自己过程中的信息能使用的方法。

之后一些原文没有在复制粘贴过来,它们又讲了在logback的配置文件糟糕的情况下,该如何做以能继续输出信息。感觉这一块在实践中的使用并不多,因此本文并不继续深究。

StatusManager:内部状态数据统合器

chapter 3

Logback collects its internal status data in a StatusManager object, accessible via the LoggerContext.

Given a StatusManager you can access all the status data associated with a logback context.

这里才正式介绍StatusManager属实有些晚了,前面第一小节的内部实现,我猜应该就是靠这个类实现的。再往前一个小节是配置logback.xml配置文件来实现的,不过可以推测具体代码仍然是依靠这个类。

结合JavaWeb技术以网页查看logback内部配置过程中的信息

chapter 3

Logback-classic ships with a servlet called ViewStatusMessagesServlet. This servlet prints the contents of the StatusManager associated with the current LoggerContext as an HTML table.

To add this servlet to your web-application, add the following lines to its WEB-INF/web.xml file.


ViewStatusMessages
ch.qos.logback.classic.ViewStatusMessagesServlet



ViewStatusMessages
/lbClassicStatus

The ViewStatusMessages servlet will be viewable at the URL.

通过将ViewStatusMessagesServlet注册,可以直接访问该组件从而以HTML的形式查看logback内部状态信息。

使用OnConsoleStatusListener

chapter 3

对应于标题为"Listening to status messages“的整个小节。

You may also attach a StatusListener to a StatusManager so that you can take immediate action in response to status messages, especially to messages occurring after logback configuration. Registering a status listener is a convenient way to supervise logback's internal state without human intervention.

Logback ships with a StatusListener implementation called OnConsoleStatusListener which, as its name indicates, prints all newincoming status messages on the console.

本小节又一个将logback的内部状态信息对外输出的功能的实现方法。

这个功能可以说是反复提及了,在本篇笔记中”输出logback内部过程的信息“,”结合JavaWeb技术以网页查看logback内部配置过程中的信息“也是对官方文档中关于此功能笔记。在笔记(一)中”要求logback输出内部准备过程中的状态信息“节也是官方文档关于此功能的描述。

相比以往,这次新颖在OnConsoleStatusListener类上。

   LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); 
   StatusManager statusManager = lc.getStatusManager();
   OnConsoleStatusListener onConsoleListener = new OnConsoleStatusListener();
   statusManager.add(onConsoleListener);

你可能感兴趣的:(slf4j,logback)