logback日志配置

1、首先配置pom.xml 确定日志及相关依赖(用slf4j+logback代替jcl+log4j) 

<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.5</version>
		</dependency>
		<!-- 将现有的jakarta commons logging的调用转换成lsf4j的调用。 -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jcl-over-slf4j</artifactId>
			<version>1.7.5</version>
		</dependency>
		<!-- 将现有的log4j的调用转换成lsf4j的调用。 -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>log4j-over-slf4j</artifactId>
			<version>1.7.12</version>
		</dependency>
		<!-- Hack:确保commons-logging的jar包不被引入,否则将和jcl-over-slf4j冲突 -->
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.1.3</version>
			<scope>provided</scope>
		</dependency>
		<!-- slf4j的实现:logback,用来取代log4j。 -->
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>1.0.13</version>
			<scope>runtime</scope>
		</dependency>

2、在classpath路径中新建logback.xml文件,文件内容如下

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOG_FILE" value="D:\\log\\dal.log" />
<property name="LOG_PATTERN" value="D:\\log\\pattern\\event.%d{yyyy-MM-dd}\L%d{dd}-%i.log" />
<property name="ENCODE" value="UTF-8" />
<property name="LOG_LEVEL" value="DEBUG"></property> 
	<!-- trace日志添加到控制台 -->
   <appender name="TRACESTDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!--   Layout 负责把事件转换成字符串,格式化的日志信息的输出。 -->
        <!-- 输出格式 时间 进程 输出级别 输出信息 换行符 -->
         <target>System.out</target>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>TRACE</level>
             <onMatch>ACCEPT</onMatch>   
      		<onMismatch>DENY</onMismatch>   
        </filter>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern><![CDATA[
			%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n
            ]]></pattern>
        </layout>
    </appender>
    <!-- debug日志添加到控制台 -->
    <appender name="DEBUGSTDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!--   Layout 负责把事件转换成字符串,格式化的日志信息的输出。 -->
        <!-- 输出格式 时间 进程 输出级别 输出信息 换行符 -->
         <target>System.out</target>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>DEBUG</level>
             <onMatch>ACCEPT</onMatch>   
      		<onMismatch>DENY</onMismatch>   
        </filter>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern><![CDATA[
			%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n
            ]]></pattern>
        </layout>
    </appender>
    <!-- info日志添加到控制台 -->
    <appender name="INFOSTDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!--   Layout 负责把事件转换成字符串,格式化的日志信息的输出。 -->
        <!-- 输出格式 时间 进程 输出级别 输出信息 换行符 -->
         <target>System.out</target>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
        	<level>INFO</level>
        	 <onMatch>ACCEPT</onMatch>   
      		<onMismatch>DENY</onMismatch>
        </filter>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern><![CDATA[
			%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n
            ]]></pattern>
        </layout>
    </appender>
	<!-- warn及error信息添加到控制台 -->
    <appender name="STDERR" class="ch.qos.logback.core.ConsoleAppender">
        <target>System.err</target>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>WARN</level>
        </filter>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern><![CDATA[
            %d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n
            ]]></pattern>
        </layout>
    </appender>
    
	<!-- 滚动记录文件,先将日志记录到指定文件,每天生成一个文件 30天周期归档  -->
 	<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${LOG_FILE}</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${LOG_PATTERN}</fileNamePattern>
			<maxHistory>30</maxHistory>
			<TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
		    	<MaxFileSize>5MB</MaxFileSize>
		    </TimeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>
		<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
			<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
		</encoder>
	</appender>
	
	
	
    <logger name="org.apache">
        <level value="INFO" />
    </logger>

    <logger name="org.springframework">
        <level value="INFO" />
    </logger>

    <root>
    	<!--设定全局日志级别-->
        <level value="${LOG_LEVEL}" />
         <appender-ref ref="TRACESTDOUT" />
         <appender-ref ref="DEBUGSTDOUT" /> 
         <appender-ref ref="INFOSTDOUT" />
        <appender-ref ref="STDERR" />
        <appender-ref ref="FILE" />
    </root>
</configuration>

3、启动项目,如出现以下信息,表示logback日志启动成功

你可能感兴趣的:(logback日志配置)