码迷,mamicode.com
首页 > 其他好文 > 详细

The logback manual #03# Configuration

时间:2018-11-20 00:04:09      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:htm   dep   def   sda   eve   sources   service   控制台   main   

(粗糙选译。。)

Joran是logback所依赖的配置框架。

Configuration in logback

观察表明,在一个应用程序中,日志代码大概占4%左右。

所以即便是一个几千行。。几万行的程序也有成百上千的日志语句,鉴于它们的数量,我们需要工具来管理这些日志语句。logback既可以通过编程方式配置,也可以通过脚本配置(XML或Groovy 格式)。顺便说一下,现有的log4j用户可以使用PropertiesTranslator 应用程序将log4j.properties转换为logback.xml

让我们从讨论logback尝试配置自身的初始化步骤开始:

  1. Logback尝试在classpath中查找一个名为logback-test.xml的文件。
  2. 如果没找到,logback将尝试在classpath中检查有没有logback.groovy
  3. 如果还是没有找到,logback继续在classpath中找logback.xml
  4. 如果依然没找到,则使用service-provider loading facility(JDK 1.6中引入的)通过查找类路径中的文件META-INF\services\ch.qos.logback.classic.spi.Configurator来解析com.qos.logback.classic.spi.Configurator接口的实现。它的内容应该指定所需Configurator实现的完全限定类名。
  5. 如果以上方法都不成功,logback将使用BasicConfigurator自动配置自己,这将导致日志输出定向到控制台。

Automatically configuring logback

技术分享图片
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.sample.logback</groupId>
    <artifactId>test-logback</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- -source 1.5 中不支持 try-with-resources-->
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.2.3</version>
        </dependency>
        <!--Failed to load class "org.slf4j.impl.StaticLoggerBinder".-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.21</version>
        </dependency>
    </dependencies>
</project>
pom.xml

BasicConfigurator是logback默认的一个最小可用的配置。试了下文档里的例子程序,在main函数里跑,debug信息莫名奇妙输不出来(只输出info的),但在junit里跑却又可以。。

Automatic configuration with logback-test.xml or logback.xml

和没有xml文件时的默认配置等价的最小可用xml配置:

<configuration>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="debug">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

Automatic printing of status messages in case of warning or errors

启用“观察logback内部状态模式”(如果logback出现内部错误则不启用也会自动输出到控制台):

        // assume SLF4J is bound to logback in the current environment
        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
        // print logback‘s internal status
        StatusPrinter.print(lc);

Status data

启用“观察logback内部状态模式”通常对诊断logback问题大有帮助。因此,强烈建议将其视为第一求助办法。(一般情况都是推荐启用的。)xml启用“观察logback内部状态模式”:

<configuration debug="true"> 

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> 
    <!-- encoders are  by default assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="debug">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

等价写法:

<configuration>
  <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />  

  ... the rest of the configuration file  
</configuration>

Automatically reloading configuration file upon modification

30秒(默认单位是毫秒)扫描一次,如果文件改变就自动重新配置:

<configuration scan="true" scanPeriod="30 seconds" > 
  ...
</configuration> 

在编辑xml的时候很容易出错,所以感觉不是很好用。

Enabling packaging data in stack traces

Viewing status messages

通过网页来查看logback的内部状态信息。

Listening to status messages

"logback.statusListenerClass" system property

Stopping logback-classic

Configuration file syntax

The logback manual #03# Configuration

标签:htm   dep   def   sda   eve   sources   service   控制台   main   

原文地址:https://www.cnblogs.com/xkxf/p/9978318.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!