赞
踩
框架 | 版本号 |
logback | 1.3.0-alpha4 |
slf4j | 1.8.0-beta2 |
现在Logback框架最新版本为1.3.0-alpha4,而依赖的slf4j的版本也为最新的版本1.8.0-beta2.
Java开发jar包依赖管理现在主流都是用Maven进行依赖管理,那么开发使用Logback日志框架,只是引入下面依赖即可
1. 在Maven项目pom.xml配置文件properties中添加下如版本信息
- <slf4j.version>1.8.0-beta2</slf4j.version>
- <logback.version>1.3.0-alpha4</logback.version>
2. 在Maven项目pom.xml配置文件repositories中添加依赖
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>${slf4j.version}</version>
- </dependency>
- <dependency>
- <groupId>ch.qos.logback</groupId>
- <artifactId>logback-core</artifactId>
- <version>${logback.version}</version>
- </dependency>
- <dependency>
- <groupId>ch.qos.logback</groupId>
- <artifactId>logback-classic</artifactId>
- <version>${logback.version}</version>
- </dependency>
配置logout不仅仅需要依赖包,还需要在resources添加配置文件logback.xml,废话不多说,下面是比较完整的各级别的日志记录配置, 注意查看其中的注解
- <configuration>
-
- <!-- 日志文件存储在执行目录同级的logs目录下-->
- <property name="PROJECT_HOME" value="${user.dir}/../logs"/>
-
- <!-- 检查系统级别日志,输出到日志文件-->
- <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener"/>
-
- <!-- 控制条输出,info以下级别的日志 -->
- <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
- <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
- <level>INFO</level>
- </filter>
- <encoder>
- <pattern>%d{yyyy/MM/dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
- </pattern>
- </encoder>
- </appender>
-
- <!-- 按照每天生成日志文件,info以下级别的日志 -->
- <appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
- <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
- <level>INFO</level>
- </filter>
- <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
- <!--日志文件输出的文件名-->
- <FileNamePattern>${PROJECT_HOME}/info.%d{yyyy-MM-dd}.log</FileNamePattern>
- <!--日志文件保留天数-->
- <MaxHistory>30</MaxHistory>
- </rollingPolicy>
- <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
- <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
- <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{20} - %msg%n</pattern>
- </encoder>
- <!--日志文件最大的大小-->
- <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
- <MaxFileSize>200MB</MaxFileSize>
- </triggeringPolicy>
- </appender>
-
- <!--debug以下级别的日志,按照文件大小存储-->
- <appender name="DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender">
- <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
- <level>DEBUG</level>
- </filter>
- <file>${PROJECT_HOME}/debug.log</file>
- <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
- <fileNamePattern>${PROJECT_HOME}/debug.%i.log</fileNamePattern>
- <minIndex>1</minIndex>
- <maxIndex>100</maxIndex>
- </rollingPolicy>
- <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
- <maxFileSize>200MB</maxFileSize>
- </triggeringPolicy>
- <encoder>
- <pattern>%d{yyyy/MM/dd HH:mm:ss.SSS} %-4relative [%thread] %-5level %logger{30} - %msg%n
- </pattern>
- </encoder>
- </appender>
-
- <!--仅存储warn级别日志,按照文件大小存储-->
- <appender name="WARN" class="ch.qos.logback.core.rolling.RollingFileAppender">
- <filter class="ch.qos.logback.classic.filter.LevelFilter">
- <level>WARN</level>
- <onMatch>ACCEPT</onMatch>
- <onMismatch>DENY</onMismatch>
- </filter>
- <file>${PROJECT_HOME}/warn.log</file>
- <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
- <fileNamePattern>${PROJECT_HOME}/warn.%i.log</fileNamePattern>
- <minIndex>1</minIndex>
- <maxIndex>100</maxIndex>
- </rollingPolicy>
- <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
- <maxFileSize>50MB</maxFileSize>
- </triggeringPolicy>
- <encoder>
- <pattern>%d{yyyy/MM/dd HH:mm:ss.SSS} %-4relative [%thread] %-5level %logger{30} - %msg%n
- </pattern>
- </encoder>
- </appender>
-
- <!--仅存储error级别日志,按照文件大小存储-->
- <appender name="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>
- <file>${PROJECT_HOME}/error.log</file>
- <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
- <fileNamePattern>${PROJECT_HOME}/error.%i.log</fileNamePattern>
- <minIndex>1</minIndex>
- <maxIndex>100</maxIndex>
- </rollingPolicy>
- <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
- <maxFileSize>50MB</maxFileSize>
- </triggeringPolicy>
- <encoder>
- <pattern>%d{yyyy/MM/dd HH:mm:ss.SSS} %-4relative [%thread] %-5level %logger{30} - %msg%n
- </pattern>
- </encoder>
- </appender>
-
- <!-- 具体类debug日志存储到debug.log日志文件 -->
- <logger name="java.sql.Connection" additivity="false" level="DEBUG">
- <appender-ref ref="DEBUG"/>
- </logger>
- <logger name="java.sql.Statement" additivity="false" level="DEBUG">
- <appender-ref ref="DEBUG"/>
- </logger>
- <logger name="java.sql.PreparedStatement" additivity="false" level="DEBUG">
- <appender-ref ref="DEBUG"/>
- </logger>
- <logger name="org.apache.hive.jdbc.Utils" additivity="false" level="DEBUG">
- <appender-ref ref="DEBUG"/>
- </logger>
-
- <!-- 控制台系统异常日志输出到INFO、ERROR、DEBUG日志文件中-->
- <root level="ERROR">
- <appender-ref ref="STDOUT"/>
- <appender-ref ref="INFO"/>
- <appender-ref ref="ERROR"/>
- <appender-ref ref="DEBUG"/>
- </root>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。