偶然间使用spring tool suit看到这个日志框架;就去看了下与log4j的区别,发现比前者强大不少,抽空学习了下相关配置;
<!-- Logging with SLF4J & LogBack --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> <scope>runtime</scope> </dependency>
以上是maven配置的引用;
同时还要在项目的src/main/resource下配置一个logback.cml的默认识别文件;内容如下
<?xml version="1.0" encoding="UTF-8"?> <!-- configuration file for LogBack (slf4J implementation) See here for more details: http://gordondickens.com/wordpress/2013/03/27/sawing-through-the-java-loggers/ --> <configuration scan="true" scanPeriod="30 seconds"> <!-- 配置当前项目路径 --> <property name="contextPath" value="logs"/> <property name="logPath" value="${contextPath}"/> <!--Appendar详解: http://logback.qos.ch/manual/appenders.html#RollingFileAppender --> <!-- 日志输出文件配置 --> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- 当前Log文件名 --> <file>${logPath}/monitor.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 非当天的Log文件压缩备份为 archive/ldap-pwd.2014-08-10.zip --> <fileNamePattern>${logPath}/monitor.log.%d{yyyy-MM-dd}</fileNamePattern> <!-- 超过30天的备份文件会被删除 --> <maxHistory>30</maxHistory> </rollingPolicy> <layout class="ch.qos.logback.classic.PatternLayout"> <!-- 格式说明:http://logback.qos.ch/manual/layouts.html#ClassicPatternLayout --> <Pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %40logger{40} - %msg%n</Pattern> </layout> </appender> <!-- 日志控制台输出配置 --> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %logger{36} - %msg%n</pattern> </encoder> <!-- 过滤输出日志信息 通过修改level来查看输出日志情况--> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>debug</level> </filter> </appender> <!-- 下面配置一些第三方包的日志过滤级别,用于避免刷屏 --> <logger name="org.hibernate" level="WARN" /> <logger name="org.springframework" level="WARN" /> <logger name="com.opensymphony" level="WARN" /> <logger name="org.apache" level="WARN" /> <!-- show parameters for hibernate sql 专为 Hibernate 定制 --> <logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="TRACE" /> <logger name="org.hibernate.type.descriptor.sql.BasicExtractor" level="DEBUG" /> <logger name="org.hibernate.SQL" level="DEBUG" /> <logger name="org.hibernate.engine.QueryParameters" level="DEBUG" /> <logger name="org.hibernate.engine.query.HQLQueryPlan" level="DEBUG" /> <!-- Uncomment and add your logger here: <logger name="org.springframework.samples.service.service" level="debug"/> --> <!-- 配置日志总体输出级别 --> <root level="trace"> <appender-ref ref="FILE" /> <appender-ref ref="console"/> </root> </configuration>
相关测试代码很简单:
import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class LogBackTest { public static void main(String[] args){ Logger debugLogger = LoggerFactory.getLogger(LogBackTest.class); debugLogger.trace("log trace"); debugLogger.debug("log debuger"); debugLogger.info("log info"); debugLogger.warn("log warn"); debugLogger.error("log error"); } }
通过修改配置文件logback.xml的参数level来查看不同情况下的运行状况;
学习网站:
http://blog.csdn.net/haidage/article/details/6794509
http://forever8tf.iteye.com/blog/1295855
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-10 14:29:32