Logback使用

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


  • logback概述

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

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

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

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

api

注意:架构

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

因此咱们这里也就将这几个包放一块儿研究好了。如今咱们开始:app

  • logback使用

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

<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,如今咱们暂时不添加任何项目配置文件,直接写一些业务代码来输出日志看下效果。ui

下面是咱们写的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以后,作一个全部的日志对比和对比,就结束日志相关了,也耽误很多时间了。
相关文章
相关标签/搜索