Logback使用

OK,现在让我们开始logback吧。关于logback的使用,网上的资料良莠不齐,权威一点的材料比如书我是没有找到。所以只好去官网上看文档,本人英语一般,所以只能一边翻译,一边研究。这里来整理一份相对比较详细的博客。


  • logback概述

LogBack是由log4j的创始人开发的一个日志组件,用于替代log4j。LogBack的架构设计足够通用,可适用于不同的环境,目前LogBack分为三个模:lobback-core,logback-classic和logback-access。

core模块是其它两个模块的基础,classic是core的扩展,是log4j巨大改进的版本。LogBack-classic本身实现了SL4J的API,因此可以很容易的在logback与其它日志系统之间转换,例如log4j、JDK1.4中的java.util.logging(JUL)。第三个模块access,它集成了

Servlet容器,提供了通过HTTP访问日志的功能。

LogBack的日志级别有TRACE < DEBUG < INFO <  WARN < ERROR。和log4j类似,日志级别依次升高,高级别的日志等级将屏蔽低级别的日志等级。

注意:

在项目中使用logback,我们需要一些依赖包,关于logback的初始化,要加载slf4j的包。其实我在这里只想单纯的研究下logback,但是官网上给的例子也都是用slf4j来初始化的,实际编码中这2者一般都是放在一起的。

所以我们这里也就将这几个包放一起研究好了。现在我们开始:

  • logback使用

首先项目中添加logback的相关jar包。pom文件如下:

<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.linkinpark.commons</groupId>
	<artifactId>linkin-log-test</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>linkin-log-test</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<!-- slf4j依赖 -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.12</version>
		</dependency>
		<!-- logback依赖 -->
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>1.1.2</version>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-core</artifactId>
			<version>1.1.2</version>
		</dependency>
		<!-- junit依赖 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>


现在来看下项目的目录结构:



OK,现在我们暂时不添加任何项目配置文件,直接写一些业务代码来输出日志看下效果。

下面是我们写的Java测试代码:

package org.linkinpark.commons.logbackLogging;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LoggingBack
{

	private static Logger logger = LoggerFactory.getLogger(LoggingBack.class);

	@Test
	public void test()
	{
		logger.debug("debug()。。。");
		logger.info("info()。。。");
		logger.error("error()。。。");
	}

}
执行上面的测试,junit绿条,然后控制台输出如下:

15:41:47.516 [main] DEBUG o.l.c.logbackLogging.LoggingBack - debug()。。。
15:41:47.519 [main] INFO  o.l.c.logbackLogging.LoggingBack - info()。。。
15:41:47.520 [main] ERROR o.l.c.logbackLogging.LoggingBack - error()。。。


logback加载配置文件顺序:


=========================================================================================================================================================================================
下面贴出logback加载文件的顺序:
  1. Logback tries to find a file called logback.groovy in the classpath.

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

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

  4. If no such file is found, and the executing JVM has the ServiceLoader (JDK 6 and above) the ServiceLoader will be used to resolve an implementation of com.qos.logback.classic.spi.Configurator. The first implementation found will be used. See ServiceLoader documentation for more details.

  5. 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.xml文件到项目中,这里我先举一个使用配置文件的例子。具体的详细的我会在后面专门一篇博客中整理。

其他代码不动,我们现在添加到logback.xml到我们的项目中,然后看下控制台输出。配置文件代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<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="info">
		<appender-ref ref="STDOUT" />
	</root>
</configuration>

运行我们的测试,控制台输出如下:
19:57:27.361 [main] INFO  o.l.c.logbackLogging.LoggingBack - info()。。。
19:57:27.363 [main] ERROR o.l.c.logbackLogging.LoggingBack - error()。。。
上面的测试代码输出3行不同级别的日志,现在配置文件生效了info级别以上的日志输出,说明我们的配置生效了。值得注意的是:这里的日志配置文件必须是logback.xml的文件,别的名字不行的,官网上的例子都是不同的名字,我晕。


OK,这篇就先到这里,下一篇我整理一下关于logback配置文件的详解logback就整理完了。关于用法和源码就不做多的研究了,整理完logback和slf4j之后,做一个所有的日志对比和对比,就结束日志相关了,也耽误不少时间了。


原文地址:https://www.cnblogs.com/LinkinPark/p/5232832.html