logback常用配置

1实现.控制台输出

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
	<target>System.out</target>
	<encoder>
		<Pattern>
		   %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36}-%msg%n
		</Pattern>
		<charset>utf-8</charset>
	</encoder>
	<filter class="ch.qos.logback.classic.filter.LevelFilter">
		<level>error</level>
		<onMatch>DENY</onMatch>
		<onMismatch>ACCEPT</onMismatch>
	</filter>
</appender>

<appender name="STDOUT_ERROR" class="ch.qos.logback.core.ConsoleAppender">
	<target>System.err</target>
	<encoder>
		<Pattern>
			%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} -%msg%n
		</Pattern>
		<charset>utf-8</charset>
	</encoder>
	<filter class="ch.qos.logback.classic.filter.LevelFilter">
		<level>error</level>
		<onMatch>ACCEPT</onMatch>
		<onMismatch>DENY</onMismatch>
	</filter>
</appender>


2实现:将所有的日志消息一直追加到指定的文件中

<appender name="FILE_APPEND" class="ch.qos.logback.core.FileAppender">
	<file>F:/log/test/logback.log</file>
	<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
		<Pattern>
			%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} -%msg%n
		</Pattern>
		<charset>utf-8</charset>
	</encoder>
	<append>true</append>
</appender>
3实现:每天产生一个日志文件且只保存最近10天的日志

<appender name="FILE_LIMITED_ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">   
   	<file>F:/log/test/today.log</file>
   	 <encoder>   
      <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] %logger{36} - %msg%n</pattern>   
      <charset>utf-8</charset>
    </encoder>   
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">   
      <fileNamePattern>F:/log/test/%d{yyyy-MM-dd}.log</fileNamePattern>
      <!-- 10 day -->
      <maxHistory>10</maxHistory>    
    </rollingPolicy>
</appender>


 4实现.一天产生一个日志,如果当天日志超过10M将进行再分割

<appender name="FILE_DAILY_ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
	<file>F:/log/test/today.log</file>
	<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
		<Pattern>
		    %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
		</Pattern>
		<charset>utf-8</charset>
	</encoder>
	<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
		<!-- rollover daily -->
		<fileNamePattern>F:/log/test/%d.%i.log</fileNamePattern>
		<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
			<maxFileSize>10MB</maxFileSize>
		</timeBasedFileNamingAndTriggeringPolicy>
	</rollingPolicy>
</appender>


5实现:所有的日志消息追加到一个日志文件中,如果该文件超过100M,就将其打包成zip.(归档个数不限)
 注意:*fileNamePattern不能使用%d,
          *如果归档文件名为backup%i.zip,那么日志文件的后缀名(log)将丢失

 <appender name="FILE_ARCHIVE" class="ch.qos.logback.core.rolling.RollingFileAppender">   
    <file>F:/log/test/logback.log</file>   
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">   
      <fileNamePattern>F:/log/test/backup%i.log.zip</fileNamePattern>   
    </rollingPolicy>   
   
    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">   
      <maxFileSize>100MB</maxFileSize>   
    </triggeringPolicy>   
    <encoder>   
      <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %level %logger{36} - %msg%n</pattern>   
    </encoder>   
  </appender>


6实现: 【在5实的基础上添加】只保存最近的3个归档文件(.zip)

 <appender name="FILE_LIMITED_ARCHIVE" class="ch.qos.logback.core.rolling.RollingFileAppender">   
    <file>F:/log/test/logback.log</file>   
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">   
      <fileNamePattern>F:/log/test/backup%i.log.zip</fileNamePattern>   
      <minIndex>1</minIndex>   
      <maxIndex>3</maxIndex> 
    </rollingPolicy>   
   
    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">   
      <maxFileSize>100MB</maxFileSize>   
    </triggeringPolicy>   
    <encoder>   
      <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %level %logger{36} - %msg%n</pattern>   
    </encoder>   
  </appender> 






你可能感兴趣的:(logback常用配置)