赞
踩
本文主要讲述logback的详细配置
首先在pom.xml中引入loback依赖的jar包,代码如下所示:
- <dependency>
- <groupId>ch.qos.logback</groupId>
- <artifactId>logback-classic</artifactId>
- <version>1.0.13</version>
- </dependency>
- <dependency>
- <groupId>ch.qos.logback</groupId>
- <artifactId>logback-core</artifactId>
- <version>1.0.13</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>jcl-over-slf4j</artifactId>
- <version>1.7.7</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>1.7.7</version>
- </dependency>
一份自动回滚日志的配置文件,如下所示:
- <?xml version="1.0" encoding="UTF-8"?>
- <configuration>
- <!--消息日志路径配置-->
- <property name="LOG_HOME" value="D://logs"/>
-
-
- <!-- 控制台输出 -->
- <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
- <!-- 日志输出编码 -->
- <Encoding>UTF-8</Encoding>
- <layout class="ch.qos.logback.classic.PatternLayout">
- <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
- <pattern>
- %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n
- </pattern>
- </layout>
- </appender>
-
-
- <!--*************************************记录监控日志*******************************************-->
- <appender name="monitorAppender"
- class="ch.qos.logback.core.rolling.RollingFileAppender">
- <Encoding>UTF-8</Encoding>
- <File>${LOG_HOME}/monitor.log</File>
- <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
- <!--日志文件输出的文件名 -->
- <FileNamePattern>${LOG_HOME}/file/monitor/monitor.log.%d{yyyy-MM-dd-HH}.log</FileNamePattern>
- <MaxHistory>30</MaxHistory>
- <TimeBasedFileNamingAndTriggeringPolicy
- class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
- <MaxFileSize>2048MB</MaxFileSize>
- </TimeBasedFileNamingAndTriggeringPolicy>
- </rollingPolicy>
- <layout class="ch.qos.logback.classic.PatternLayout">
- <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
- <pattern>
- %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n
- </pattern>
- </layout>
- </appender>
- <logger name="monitor" level="INFO">
- <appender-ref ref="monitorAppender"/>
- </logger>
-
-
- <!--*************************************记录bug日志*******************************************-->
- <appender name="bugAppender"
- class="ch.qos.logback.core.rolling.RollingFileAppender">
- <Encoding>UTF-8</Encoding>
- <File>${LOG_HOME}/bug.log</File>
- <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
- <!--日志文件输出的文件名 -->
- <FileNamePattern>${LOG_HOME}/file/bug/bug.log.%d{yyyy-MM-dd-HH}.log</FileNamePattern>
- <MaxHistory>100</MaxHistory>
- <TimeBasedFileNamingAndTriggeringPolicy
- class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
- <MaxFileSize>2048MB</MaxFileSize>
- </TimeBasedFileNamingAndTriggeringPolicy>
- </rollingPolicy>
- <layout class="ch.qos.logback.classic.PatternLayout">
- <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
- <pattern>
- %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n
- </pattern>
- </layout>
- </appender>
- <logger name="bug" level="INFO">
- <appender-ref ref="bugAppender"/>
- </logger>
-
-
-
- <!--*************************************记录错误日志*******************************************-->
- <appender name="errorAppender"
- class="ch.qos.logback.core.rolling.RollingFileAppender">
- <Encoding>UTF-8</Encoding>
- <File>${LOG_HOME}/error.log</File>
- <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
- <!--日志文件输出的文件名 -->
- <FileNamePattern>${LOG_HOME}/file/error/error.log.%d{yyyy-MM-dd-HH}.log</FileNamePattern>
- <MaxHistory>30</MaxHistory>
- <TimeBasedFileNamingAndTriggeringPolicy
- class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
- <MaxFileSize>2048MB</MaxFileSize>
- </TimeBasedFileNamingAndTriggeringPolicy>
- </rollingPolicy>
- <layout class="ch.qos.logback.classic.PatternLayout">
- <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
- <pattern>
- %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n
- </pattern>
- </layout>
- </appender>
- <logger name="error" level="INFO">
- <appender-ref ref="errorAppender"/>
- </logger>
-
-
- <!--*************************************记录消息到硬盘配置*******************************************-->
- <appender name="FILE"
- class="ch.qos.logback.core.rolling.RollingFileAppender">
- <Encoding>UTF-8</Encoding>
- <File>${LOG_HOME}/file.log</File>
- <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
- <!--日志文件输出的文件名 -->
- <FileNamePattern>${LOG_HOME}/file/file/file.log.%d{yyyy-MM-dd-HH}.log</FileNamePattern>
- <MaxHistory>30</MaxHistory>
- <TimeBasedFileNamingAndTriggeringPolicy
- class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
- <MaxFileSize>2048MB</MaxFileSize>
- </TimeBasedFileNamingAndTriggeringPolicy>
- </rollingPolicy>
- <layout class="ch.qos.logback.classic.PatternLayout">
- <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
- <pattern>
- %msg%n
- </pattern>
- </layout>
- </appender>
- <logger name="file" level="INFO">
- <appender-ref ref="FILE"/>
- </logger>
-
-
- <!-- 日志输出级别 -->
- <root level="INFO">
- <appender-ref ref="STDOUT"/>
- </root>
-
- </configuration>
以上配置文件,声明了四种类型的日志输入类型,并按照小时自动回滚日志。
如果想要查看详细的运行过程,请查看文章线程管理(二)获取和设置线程信息(含示例源码)中的源码,运行查看。
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。