使用 logback + slf4j 进行日志记录

此处主要介绍maven web工程下如何使用 logback + slf4j  进行日志记录。

logback主要包含三个组成部分:Loggers(日志记录器)、Appenders(输出目的在)、Layouts(日志输出格式)

 

slf4j :如jdbc一样,定义了一套接口,是一个日志门面,可实现多个日志系统间快速切换(通过修改配置文件)

logback : 和log4j是同一作者,是log4j的升级版,效果可想而知.

logback 主要分为三个模块,分别是:

  logback-core:提供基础功能,是其他两个模块的基础
  logback-classic : log4j的升级,实现了self4j api
  logback-access:用于与sevlet容器进行集成、提供网络访问日志的功能

 

logback初始化时,默认会去classpath下依次加载如下配置文件(logback.groovy、logback-test.xml、logback.xml),当找不到配置文件时logback将为rootLogger 添加一个 ConsoleAppender ,用于将日志输出到控制台。对于logback的初始化,官网有如下描述

复制代码
Logback tries to find a file called logback.groovy in the classpath.

If no such file is found, logback tries to find a file called logback-test.xml in the classpath.

If no such file is found, it checks for the file logback.xml in the classpath..

If no such file is found, service-provider loading facility (introduced in JDK 1.6) is used to resolve the implementation of com.qos.logback.classic.spi.Configurator interface by looking up the file META-INF\services\ch.qos.logback.classic.spi.Configurator in the class path. Its contents should specify the fully qualified class name of the desired Configurator implementation.

If none of the above succeeds, logback configures itself automatically using the BasicConfigurator which will cause logging output to be directed to the console.
复制代码

 

 

编程时可是要如下代码查看logback内部运行情况:

LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
StatusPrinter.print(lc);

对于web项目,可配置sevlet,然后通过网页查看logback内部运行状态,如下:

复制代码
<!-- 通过浏览器以html形式查看logback内部状态 :http://host/yourWebapp/lbClassicStatus -->
    <servlet>
        <servlet-name>ViewStatusMessages</servlet-name>
        <servlet-class>ch.qos.logback.classic.ViewStatusMessagesServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>ViewStatusMessages</servlet-name>
        <url-pattern>/lbClassicStatus</url-pattern>
    </servlet-mapping>
复制代码

说了一大堆废话,现在开始进入主题:

首先,在pom文件引入相关依赖,如下:

复制代码
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
     <version>1.7.5</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.0.13</version>
</dependency>
复制代码

 

接着,编写logback.xml(位于classpath目录下,此处为src/main/resources目录)

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

 

最后,就可以在程序中使用logback了,例如:

复制代码
package com.yinz.aaa;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Test {

    public static void main(String[] args) {
        Logger logger = LoggerFactory.getLogger(Test.class);
        logger.debug("test........");
    }
}
复制代码

如上所述,我们使用的slf4j中的api,如此依赖,要想在多个日志系统间切换,只需要提供相应的配置文件就可以了,而不需要修改编码部分。

 

最后有几点需要注意:

1、logger 的日志级别若没显示定义,则继承最近的祖先logger(该logger需显示定义level,直到rootLogger)的日志级别。

  1.1,、logger的父子关系,由logger的名称决定,例如有三个logger,分别为:java.lang.util 、 java.lang  、java

  则,java是java.lang的父logger, 是java.lang.util的祖先logger. 而同时java.lang是java.lang.util的父logger

2、logger的appender默认具有累加性(默认日志输出到当前logger的appender和所有祖先logger的appender中),可通过配置 “additivity”属性修改默认行为

 

logback与spring整合还在研究中。。。。。。

posted @ 2019-01-02 15:38  甜菜波波  阅读(855)  评论(0编辑  收藏  举报