---
title: "logback status messages "
author: "dikar"
date: "2010-04-22T13:39:25Z"
tags: []
categories: ["编程语言"]
slug: "logback-status-messages"
draft: false
lastmod: "2010-05-14T16:20:20Z"
id: 650346
url: "https://dikar.iteye.com/blog/650346"
---

项目中使用了slf4j + logback 在测试环境是正常的，结果在生产环境就不能按照预想打出log。

生产环境debug是不可能的，再说也不好debug。查了下logback的官方，发现有个功能很强大，可以打印出logback的状态信息，例如初始化信息，可以帮助你诊断。

 

以下信息摘自：http://logback.qos.ch/manual/configuration.html

 

#### [Automatic printing of status messages in case of warning or errors](#automaticStatusPrinting)

If warning or errors occur during the parsing of the configuration file, logback will automatically print its internal status messages on the console.

If warnings or errors occur during the parsing of the configuration file, logback will automatically print status data on the console. 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 the addition of two lines of code for printing internal status data.

*Example: Print logback's internal status information [(logback-examples/src/main/java/chapters/configuration/MyApp2.java)](/admin/blogs/xref/chapters/configuration/MyApp2.html)*

 

```
  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); 
    ... 
  }
```

 

If everything goes well, you should see the following output on the console

```
17:44:58,578 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback-test.xml]
17:44:58,671 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
17:44:58,671 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
17:44:58,687 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
17:44:58,812 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Popping appender named [STDOUT] from the object stack
17:44:58,812 |-INFO in ch.qos.logback.classic.joran.action.LevelAction - root level set to DEBUG
17:44:58,812 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[root]

17:44:58.828 [main] INFO  chapters.configuration.MyApp2 - Entering application.
17:44:58.828 [main] DEBUG chapters.configuration.Foo - Did it again!
17:44:58.828 [main] INFO  chapters.configuration.MyApp2 - Exiting application.
```

At the end of this output, you can recognize the lines that were printed in the previous example. You should also notice the logback's internal messages, a.k.a. `Status` objects, which allow convenient access to logback's internal state.