`
Donald_Draper
  • 浏览: 984727 次
社区版块
存档分类
最新评论

slf4j + Logback使用

阅读更多
使用logback轻松管理日志: http://blog.csdn.net/mydeman/article/details/6716925
Logback日志使用详解:http://www.cnblogs.com/davidwang456/p/4448011.html
logback的介绍-配置:http://sunney2010.iteye.com/blog/1404723
slf4j +  Logback详解:http://donald-draper.iteye.com/admin/blogs/2332433
需要的jar
slf4j-api.jar
logback-core.jar
logback-classic.jar
log4j-over-slf4j.jar
jcl104-over-slf4j.jar
janino.jar
测试主类:
package org;

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

public class testLogback {
	private static Logger log = LoggerFactory.getLogger(testSFL4J.class); 
	   public static void main(String[] args) {
		   log.debug("=======test root=========");
		   log.info("========test daily level info=========");
		   log.warn("========test warn=========");
		   log.error("========test error=========");
  }
} 

如果添加Log4j的相关jar包,则控制台输出为:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/E:/lib/slf4j-log4j12-1.7.9.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/E:/lib/logback-classic-0.9.27.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
[DEBUG] 2016-10-23 15:14:08,549 org.testLogback =======test root=========
[INFO ] 2016-10-23 15:14:08,549 org.testLogback ========test daily level info=========
[WARN ] 2016-10-23 15:14:08,549 org.testLogback ========test warn=========
[ERROR] 2016-10-23 15:14:08,549 org.testLogback ========test error=========
从上面信息可以看出,当有多个StaticLoggerBinder实现时,默认为Log4jLoggerFactory,
去除Log4j的相关jar包,控制输出:
15:24:53,431 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
15:24:53,432 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
15:24:53,432 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/F:/Myeclipse/Log/bin/logback.xml]
15:24:53,477 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
15:24:53,494 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
15:24:53,497 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [stdout]
15:24:53,506 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
15:24:53,542 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
15:24:53,543 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [file—debug]
15:24:53,555 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - No compression will be used
15:24:53,555 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use the pattern F:/logback/debug.%d{yyyy-MM-dd}.log for the active file
15:24:53,558 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyy-MM-dd' from file name pattern 'F:/logback/debug.%d{yyyy-MM-dd}.log'.
15:24:53,558 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over at midnight.
15:24:53,560 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Sun Oct 23 15:24:53 CST 2016
15:24:53,560 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
15:24:53,561 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[file—debug] - Active log file name: F:/logback/debug.2016-10-23.log
15:24:53,561 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[file—debug] - File property is set to [null]
15:24:53,564 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
15:24:53,564 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [file—error]
15:24:53,565 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - No compression will be used
15:24:53,565 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use the pattern F:/logback/error.%d{yyyy-MM-dd}.log for the active file
15:24:53,566 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyy-MM-dd' from file name pattern 'F:/logback/error.%d{yyyy-MM-dd}.log'.
15:24:53,566 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over at midnight.
15:24:53,566 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Sun Oct 23 15:24:53 CST 2016
15:24:53,566 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
15:24:53,566 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[file—error] - Active log file name: F:/logback/error.2016-10-23.log
15:24:53,566 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[file—error] - File property is set to [null]
15:24:53,566 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
15:24:53,566 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [file-str]
15:24:53,568 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.boolex.JaninoEventEvaluator] for [evaluator] property
15:24:53,910 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - No compression will be used
15:24:53,910 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use the pattern F:/logback/contains.%d{yyyy-MM-dd}.log for the active file
15:24:53,910 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyy-MM-dd' from file name pattern 'F:/logback/contains.%d{yyyy-MM-dd}.log'.
15:24:53,910 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over at midnight.
15:24:53,910 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Sun Oct 23 15:24:53 CST 2016
15:24:53,910 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
15:24:53,911 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[file-str] - Active log file name: F:/logback/contains.2016-10-23.log
15:24:53,911 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[file-str] - File property is set to [null]
15:24:53,912 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.classic.db.DBAppender]
15:24:53,963 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [db]
15:24:53,968 |-INFO in ch.qos.logback.classic.joran.action.LevelAction - java.sql.Connection level set to DEBUG
15:24:53,968 |-INFO in ch.qos.logback.classic.joran.action.LevelAction - java.sql.Statement level set to DEBUG
15:24:53,968 |-INFO in ch.qos.logback.classic.joran.action.LevelAction - com.ibatis level set to DEBUG
15:24:53,968 |-INFO in ch.qos.logback.classic.joran.action.LevelAction - com.ibatis.common.jdbc.SimpleDataSource level set to DEBUG
15:24:53,968 |-INFO in ch.qos.logback.classic.joran.action.LevelAction - com.ibatis.common.jdbc.ScriptRunner level set to DEBUG
15:24:53,968 |-INFO in ch.qos.logback.classic.joran.action.LevelAction - com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate level set to DEBUG
15:24:53,968 |-INFO in ch.qos.logback.classic.joran.action.LevelAction - com.danga.MemCached level set to INFO
15:24:53,968 |-INFO in ch.qos.logback.classic.joran.action.LevelAction - org.springframework.test level set to DEBUG
15:24:53,968 |-INFO in ch.qos.logback.classic.joran.action.LevelAction - org.apache.struts2 level set to DEBUG
15:24:53,968 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
15:24:53,968 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [stdout] to Logger[ROOT]
15:24:53,970 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [file—debug] to Logger[ROOT]
15:24:53,970 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [file—error] to Logger[ROOT]
15:24:53,970 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [file-str] to Logger[ROOT]
15:24:53,970 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [db] to Logger[ROOT]

2016-10-23 15:24:53,976 [main] DEBUG org.testLogback - =======test root=========
2016-10-23 15:24:53,979 [main] INFO  org.testLogback - ========test daily level info=========
2016-10-23 15:24:53,979 [main] WARN  org.testLogback - ========test warn=========
2016-10-23 15:24:53,979 [main] ERROR org.testLogback - ========test error=========

去F盘logback文件夹下,可以看到相关日志文件
contains.2016-10-23.log
debug.2016-10-23.log
2016-10-23 15:24:53,976 [main] DEBUG org.testLogback - =======test root=========
error.2016-10-23.log
2016-10-23 15:24:53,979 [main] ERROR org.testLogback - ========test error=========
我的logback.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
 <configuration>
     <!-- 控制台输出 -->
     <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
         <encoder>
             <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
         </encoder>
     </appender>
 
     <!-- 时间滚动输出 level为 DEBUG 日志 -->
     <appender name="file—debug"
         class="ch.qos.logback.core.rolling.RollingFileAppender">
         <filter class="ch.qos.logback.classic.filter.LevelFilter">
             <level>DEBUG</level>
             <onMatch>ACCEPT</onMatch>
             <onMismatch>DENY </onMismatch>
         </filter>
         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
             <FileNamePattern>F:/logback/debug.%d{yyyy-MM-dd}.log</FileNamePattern>
             <MaxHistory>30</MaxHistory>
         </rollingPolicy>
         <encoder>
             <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
             <charset>UTF-8</charset>
         </encoder>
     </appender>
 
     <!-- 时间滚动输出 level为 ERROR 日志 -->
     <appender name="file—error"
         class="ch.qos.logback.core.rolling.RollingFileAppender">
         <filter class="ch.qos.logback.classic.filter.LevelFilter">
             <level>ERROR</level>
             <onMatch>ACCEPT</onMatch>
             <onMismatch>DENY </onMismatch>
         </filter>
         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
             <FileNamePattern>F:/logback/error.%d{yyyy-MM-dd}.log</FileNamePattern>
             <MaxHistory>30</MaxHistory>
         </rollingPolicy>
         <encoder>
             <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
             <charset>UTF-8</charset>
         </encoder>
     </appender>
 
     <!-- 特定过滤含有某字符串的日志 -->
     <appender name="file-str"
         class="ch.qos.logback.core.rolling.RollingFileAppender">
         <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
             <evaluator>
                 <expression>message.contains("str")</expression>
             </evaluator>
             <onMatch>ACCEPT</onMatch>
             <onMismatch>DENY</onMismatch>
         </filter>
         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
             <FileNamePattern>F:/logback/contains.%d{yyyy-MM-dd}.log
             </FileNamePattern>
             <MaxHistory>30</MaxHistory>
         </rollingPolicy>
         <encoder>
             <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
         </encoder>
     </appender>
 
     <!-- 数据库输出 -->
     <appender name="db" class="ch.qos.logback.classic.db.DBAppender">
         <connectionSource
             class="ch.qos.logback.core.db.DriverManagerConnectionSource">
             <driverClass>com.mysql.jdbc.Driver</driverClass>
             <url>jdbc:mysql://localhost:3306/test</url>
             <user>root</user>
             <password>123456</password>
         </connectionSource>
     </appender>
 
     <logger name="java.sql.Connection">
         <level value="DEBUG" />
     </logger>
     <logger name="java.sql.Statement">
         <level value="DEBUG" />
     </logger>
     <logger name="com.ibatis">
         <level value="DEBUG" />
     </logger>
     <logger name="com.ibatis.common.jdbc.SimpleDataSource">
         <level value="DEBUG" />
     </logger>
     <logger name="com.ibatis.common.jdbc.ScriptRunner">
         <level value="DEBUG" />
     </logger>
     <logger name="com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate">
         <level value="DEBUG" />
     </logger>
     <logger name="com.danga.MemCached">
         <level value="INFO" />
     </logger>
     <logger name="org.springframework.test">
         <level value="DEBUG" />
     </logger>
     <logger name="org.apache.struts2">
         <level value="DEBUG" />
     </logger>
     <root level="DEBUG">
         <appender-ref ref="stdout" />
         <appender-ref ref="file—debug" />
         <appender-ref ref="file—error" />
         <appender-ref ref="file-str" />
         <appender-ref ref="db" />
     </root>
</configuration>

总结:
从上面的测试可以看出logback日志,可以根据日志的类别去,输出到不同的文件,不用log4j那样要输出大于等于Appender日志等级的日志。



分享到:
评论

相关推荐

    调试日志之slf4j+logback

    在IT行业中,日志记录是系统调试、故障排查和性能分析的重要工具。本文将深入探讨“调试日志之slf4j+...文档《复件 日志系统.docx》可能包含了更多关于如何配置和使用SLF4J+Logback的详细信息,建议进一步查阅学习。

    日志 log4j-slf4j+logback1.3.0

    总的来说,"log4j-slf4j+logback1.3.0"的组合提供了一个强大、灵活的日志解决方案,允许开发者使用统一的API进行日志记录,同时利用Logback的高性能和丰富特性。通过正确配置这些组件,开发者可以轻松管理和优化Java...

    Tomcat + SLF4J + Logback 2017年10月最新包(Tomcat7~9)

    https://github.com/tomcat-slf4j-logback/tomcat-slf4j-logback上的jar与源码,网络不好,下载不易,特此分享。包括: logback-1.2.3.zip Tomcat7.0.82\tomcat-juli-7.0.82-slf4j-1.7.25-logback-1.2.3.zip Tomcat...

    slf4j+logback使用

    本篇将详细介绍SLF4J和Logback的使用,以及它们在实际开发中的重要性。 首先,SLF4J的主要目标是为各种日志框架提供一个简单的抽象,这样应用程序就可以在部署时动态地绑定到任何兼容的实现。SLF4J提供了类似于`...

    slf4j+logback日志系统介绍

    但对于公共类库或大规模应用来说,使用SLF4J + Logback是非常值得推荐的。 总之,SLF4J + Logback为Java开发者提供了一套强大、灵活且高效的日志解决方案。无论是对于初学者还是经验丰富的开发者,了解并掌握这套...

    slf4j+logback

    SLF4J(Simple Logging Facade for Java)和Logback是Java日志处理中的两个重要组件,...通过对SLF4J接口的使用和Logback的细致配置,开发者可以更好地监控和分析应用程序的行为,同时保持代码的可移植性和可维护性。

    slf4j+logback 于log4j+commons-logging大PK

    今天我们将聚焦两个流行的日志框架——SLF4J(Simple Logging Facade for Java)和Logback,以及它们与Log4j和Commons-Logging的对比。 SLF4J是一个接口层的日志框架,它提供一个统一的API,允许用户在运行时插入...

    slf4j+logback的jar和配置文件

    在使用SLF4J和Logback时,你需要引入相应的JAR文件。这些JAR文件通常包括SLF4J API(如`slf4j-api-*.jar`)、Logback核心库(如`logback-core-*.jar`)、Logback经典库(如`logback-classic-*.jar`)以及任何其他...

    ssm +shiro+slf4j+logback 学习记录

    SSM(Spring、SpringMVC、MyBatis)与Apache Shiro、SLF4J以及Logback是Java Web开发中的重要框架和工具,它们在构建安全、高效且可维护的Web应用方面起着关键作用。这里我们将深入探讨这些技术的核心概念、功能以及...

    slf4j + logback配置实例

    本文将通过一个配置实例,详细介绍如何在项目中集成和使用SLF4J与Logback。 首先,我们需要在项目的类路径下添加SLF4J和Logback的相关依赖。通常,这包括`slf4j-api.jar`(SLF4J API)、`logback-classic.jar`...

    采用slf4j+logback输出日志

    本文将详细介绍如何使用SLF4J与Logback来输出应用程序的日志,并提供必要的配置和依赖知识。 1. **SLF4J介绍** SLF4J提供了一种统一的日志接口,使得开发者可以在不改变代码的情况下切换不同的日志实现,如Log4j、...

    slf4j+logback+springmvc+maven小例子

    SLF4J允许开发者在部署时插入所需的日志实现,比如logback、log4j等,这使得项目的可移植性和灵活性大大增强。 Logback是另一个关键组件,它是由log4j的创始人Ceki Gülcü创建的。Logback是目前广泛使用的日志框架...

    slf4j+logback快速上手教程

    为了使用SLF4J结合Logback进行日志记录,你需要以下JAR文件: - `slf4j-api-1.4.3.jar`:SLF4J API,必不可少。 - `logback-core.jar`:Logback的核心包。 - `logback-classic.jar`:Logback的经典模块,包含SLF4J的...

    Slf4j+logback实现logback测试

    综上所述,Slf4j与Logback的结合使用可以为Java应用程序提供强大的日志管理能力。通过合理的配置和测试,我们可以确保日志系统既满足调试需求,又不拖累应用性能。在实际开发中,理解并熟练运用这些工具是非常重要的...

    slf4j+logback 日志系统使用

    SLF4J(Simple Logging Facade for Java)和Logback是Java开发中广泛使用的日志处理框架。SLF4J提供了一个抽象层,允许开发者在运行时选择合适的日志实现,而Logback则是SLF4J的一个具体实现,它以其高效、灵活和...

    详解Spring Boot 使用slf4j+logback记录日志配置

    使用 SLF4J 和 Logback 进行日志记录配置的主要目的是: 1. 为 Spring 的开发提供了一个更快速更广泛的入门方法。 2. 使用默认方式实现快速开发。 3. 提供项目所需的一些非功能特性,如嵌入式服务器、安全、心跳...

    tomcat9-slf4j-logback:Tomcat 9.0.x的SLF4J和Logback集成

    Tomcat SLF4J Logback是tomcat的替代产品,它允许所有内部日志记录全部使用我们喜欢的slf4j / logback库。 注意 从logback 1.1.7开始,不再需要在server.xml中包含${catalina.home}来进行logback访问。 我们还重新...

    springMvc+mybaties+slf4j

    【SLF4J(Simple Logging Facade for Java)】是一个日志门面,为各种日志框架如log4j、Logback提供一个统一的接口。SLF4J允许开发者在部署时选择合适的日志实现,只需更换绑定的日志实现库即可。这使得项目具有更好...

    slf4j-1.6.1+logback-0.9.24.rar

    janino-2.3.17.jar jcl-over-slf4j-1.6.1.jar log4j-1.2.8.jar logback-access-0.9.24.jar logback-classic-0.9.24.jar logback-core-0.9.24.jar ... slf4j-1.6.1+logback-0.9.24.rar slf4j-api-1.6.1.jar

    SLF4J&Logback.zip

    On Java 8 中用到的日志包,SLF4J+Logback。

Global site tag (gtag.js) - Google Analytics