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的相關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 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, 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.
If none of the above succeeds, logback configures itself automatically using the BasicConfigurator
which will cause logging output to be directed to the console.
<?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的文件,別的名字不行的,官網上的例子都是不一樣的名字,我暈。