Spring MVC整合logback日志框架实战

1.引入依赖,本项目maven构建,普通项目导入想要的jar包即可

版本

<properties>    <slf4j-api.version>1.7.7</slf4j-api.version>    <logback.version>1.1.3</logback.version>
</properties>

依赖包

<dependency>    <groupId>ch.qos.logback</groupId>    <artifactId>logback-core</artifactId>    <version>${logback.version}</version></dependency><dependency>    <groupId>org.logback-extensions</groupId>    <artifactId>logback-ext-spring</artifactId>    <version>0.1.2</version></dependency><dependency>    <groupId>ch.qos.logback</groupId>    <artifactId>logback-classic</artifactId>    <version>${logback.version}</version></dependency><dependency>    <groupId>org.slf4j</groupId>    <artifactId>slf4j-api</artifactId>    <version>${slf4j-api.version}</version></dependency>

2.配置logback.xml

<?xml version="1.0" encoding="UTF-8"?>

<configuration scan="true" scanPeriod="30 seconds">

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"><!-- encoders are  by default assigned the type             ch.qos.logback.classic.encoder.PatternLayoutEncoder --><encoder>            <pattern>%d{yyyy-MM-dd HH:mm:ss} [%level] - %m%n</pattern>

<!-- 常用的Pattern变量,大家可打开该pattern进行输出观察 -->            <!--              <pattern>                  %d{yyyy-MM-dd HH:mm:ss} [%level] - %msg%n                  Logger: %logger                  Class: %class                  File: %file                  Caller: %caller                  Line: %line                  Message: %m                  Method: %M                  Relative: %relative                  Thread: %thread                  Exception: %ex                  xException: %xEx                  nopException: %nopex                  rException: %rEx                  Marker: %marker                  %n

              </pattern>               --></encoder>    </appender>

<!-- 按日期区分的滚动日志 --><appender name="ERROR-OUT" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 日志文件完整路径 -->        <file>/data/log/ballq-forecast/ballq-forecast.error.log</file>

        <encoder>            <pattern>%d{yyyy-MM-dd HH:mm:ss} [%class:%line] - %m%n</pattern>        </encoder>

        <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"><!-- daily rollover --><fileNamePattern>ballq-forecast.error.%d{yyyy-MM-dd}.log.zip</fileNamePattern>

<!-- keep 30 days‘ worth of history --><maxHistory>30</maxHistory>        </rollingPolicy>    </appender>

<!-- 按文件大小区分的滚动日志 --><appender name="INFO-OUT" class="ch.qos.logback.core.rolling.RollingFileAppender">        <file>/data/log/ballq-forecast/ballq-forecast.info.log</file>

        <encoder>            <pattern>%d{yyyy-MM-dd HH:mm:ss} [%class:%line] - %m%n</pattern>        </encoder>

        <filter class="ch.qos.logback.classic.filter.LevelFilter">            <level>INFO</level>            <onMatch>ACCEPT</onMatch>            <onMismatch>DENY</onMismatch>        </filter>

        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">            <fileNamePattern>ballq-forecast.info.%d{yyyy-MM-dd}.%i.log</fileNamePattern>            <minIndex>1</minIndex>            <maxIndex>3</maxIndex>        </rollingPolicy>

        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">            <maxFileSize>50MB</maxFileSize>        </triggeringPolicy>

    </appender>

<!-- 按日期和大小区分的滚动日志 --><appender name="DEBUG-OUT" class="ch.qos.logback.core.rolling.RollingFileAppender">        <file>/data/log/ballq-forecast/ballq-forecast.debug.log</file>

        <encoder>            <pattern>%d{yyyy-MM-dd HH:mm:ss} [%class:%line] - %m%n</pattern>        </encoder>

        <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"><!-- rollover daily --><fileNamePattern>ballq-forecast.debug.%d{yyyy-MM-dd}.%i.log</fileNamePattern>

            <timeBasedFileNamingAndTriggeringPolicyclass="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"><!-- or whenever the file size reaches 100MB --><maxFileSize>50MB</maxFileSize>            </timeBasedFileNamingAndTriggeringPolicy>

        </rollingPolicy>

    </appender>

<!-- 级别阀值过滤 --><appender name="SUM-OUT" class="ch.qos.logback.core.rolling.RollingFileAppender">        <file>/data/log/ballq-forecast/ballq-forecast.sum.log</file>

        <encoder>            <pattern>%d{yyyy-MM-dd HH:mm:ss} [%class:%line] - %m%n</pattern>        </encoder>

<!-- deny all events with a level below INFO, that is TRACE and DEBUG --><filter class="ch.qos.logback.classic.filter.ThresholdFilter">            <level>INFO</level>        </filter>

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><!-- rollover daily --><fileNamePattern>ballq-forecast.sum-%d{yyyy-MM-dd}.%i.log</fileNamePattern>

            <timeBasedFileNamingAndTriggeringPolicyclass="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"><!-- or whenever the file size reaches 100MB --><maxFileSize>50MB</maxFileSize>            </timeBasedFileNamingAndTriggeringPolicy>

        </rollingPolicy>

    </appender>

    <root level="debug">        <appender-ref ref="STDOUT" />        <appender-ref ref="ERROR-OUT" />        <appender-ref ref="INFO-OUT" />        <appender-ref ref="DEBUG-OUT" />        <appender-ref ref="SUM-OUT" />    </root></configuration>

3.配置web.xml

<context-param>    <param-name>logbackConfigLocation</param-name>    <param-value>classpath:logback.xml</param-value></context-param><listener>    <listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class></listener>

参考地址:http://www.cnblogs.com/mailingfeng/p/3499436.html

来自为知笔记(Wiz)

时间: 2024-10-04 22:38:49

Spring MVC整合logback日志框架实战的相关文章

Spring Boot 整合 Logback 日志框架

常用处理 Java 的日志组件有 slf4j.log4j.logback.common-logging 等.logback 基于 log4j 基础上大量改良,不能单独使用,推荐配合日志框架 slf4j 来使用. logback 当前分成三个模块:logback-core.logback-classic 和 logback-access,logback-core 是其它两个模块的基础模块 logback 的核心对象:Logger(日志记录器).Appender(指定日志输出的目的地,目的地可以是控

spring mvc整合mybaitis和log4j

在上一篇博客中,我介绍了在mac os上用idea搭建spring mvc的maven工程,但是一个完整的项目肯定需要数据库和日志管理,下面我就介绍下spring mvc整合mybatis和log4j 一.先把需要用到的jar包配置到pom.xml上(主要是mybaitis和mysql驱动包) <!-- mybatis/spring包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId

Spring mvc整合mybatis基于mysql数据库实现用户增删改查及其分页显示的完整入门实例【转】

Spring mvc整合mybatis例子, 基于mysql数据库实现对用户的增.删.改.查,及分页显示的完整例子. 查询显示用户 添加用户 更新用户 官方验证: 项目截图 必须修改applicationContext.xml中mysql的配置为本地的,否则启动失败. 另外jar包多一个ehcache.jar无关紧要,删除即可. 1. 使用阿里巴巴Druid连接池(高效.功能强大.可扩展性好的数据库连接池.监控数据库访问性能.支持Common-Logging.Log4j和JdkLog,监控数据库

SpringBoot整合+logback日志配置

本次演示的代码结构如下,基于maven,整合SpringBoot.Spring.Mybaits的SSM框架.同时测试logback日志框架的使用及配置. 1.创建maven工程,修改pom.xml文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="h

MyBatis+Spring+Spring MVC整合开发

MyBatis+Spring+Spring MVC整合开发课程观看地址:http://www.xuetuwuyou.com/course/65课程出自学途无忧网:http://www.xuetuwuyou.com 课程介绍一.课程用到的软件:1.jdk 1.82.eclispe luna3.tomcat 84.MySQL 5.1 +5.navicat 9 + 二.课程涉及到的技术点1.MyBatis基础,主要介绍2.MyBatis高级,3.SpringMVC基础4.SpringMVC高级5.My

MyBatis+Spring+Spring MVC整合开发视频教程

课程观看地址:http://www.xuetuwuyou.com/course/65 课程出自学途无忧网:http://www.xuetuwuyou.com 课程介绍 一.课程用到的软件: 1.jdk 1.8 2.eclispe luna 3.tomcat 8 4.MySQL 5.1 + 5.navicat 9 + 二.课程涉及到的技术点 1.MyBatis基础,主要介绍 2.MyBatis高级, 3.SpringMVC基础 4.SpringMVC高级 5.MyBatis+SpringMVC整合

logback日志框架的简单使用

最近在自己搭建项目的时候,遇到logback选的问题,现在记录如下 在框架中使用logback日志框架,关于logback和log4j日志框架的选择,这里就不多说了网上百度一大堆,总之一句话 logback要比log4j效率高,性能好,配置灵活. 1.首先在maven中增加依赖 <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId&g

Spring与Struts2整合VS Spring与Spring MVC整合

Spring与Struts2整合,struts.xml在src目录下 1.在web.xml配置监听器 web.xml <!-- 配置Spring的用于初始化ApplicationContext的监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <contex

spring MVC 整合mongodb

Spring Mongodb 目录 1 SPRING整合MONGODB 1 1.1 环境准备 1 1.2 包依赖 1 1.3 配置 2 2 案列 5 2.1 SPRING MVC整合MONGODB代码案例 5 1 Spring整合Mongodb 1.1 环境准备 1. mongodb官网 http://www.mongodb.org/,下载mongodb安装包和mongodb的java驱动包. mongodb安装包(下载地址http://www.mongodb.org/downloads).Mo