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
时间: 2024-10-04 22:38:49