logback配置详解4-实例配置

时间:2023-12-10 12:29:26

莫个银行项目中实际引用的logback实例,提供大家参考!!!!

[html] view
plain
copy
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- if debug set to true, context info always print
  3. otherwise the contex info will print when error occour -->
  4. <configuration scan="true" scanPeriod="60 seconds" debug="false">
  5. <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
  6. <layout class="ch.qos.logback.classic.PatternLayout">
  7. <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{40} - %msg%n</pattern>
  8. </layout>
  9. </appender>
  10. <!-- 所有日志文件-->
  11. <appender name="AllLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
  12. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  13. <fileNamePattern>log/abcchina/all/%d{yyyy-MM-dd}.log</fileNamePattern>
  14. <maxHistory>30</maxHistory>
  15. </rollingPolicy>
  16. <encoder>
  17. <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{40} - %msg%n</pattern>
  18. </encoder>
  19. </appender>
  20. <appender name="TaskLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
  21. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  22. <fileNamePattern>log/abcchina/task/%d{yyyy-MM-dd}.log</fileNamePattern>
  23. <maxHistory>30</maxHistory>
  24. </rollingPolicy>
  25. <encoder>
  26. <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{40} - %msg%n</pattern>
  27. </encoder>
  28. </appender>
  29. <appender name="SendLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
  30. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  31. <fileNamePattern>log/abcchina/send/%d{yyyy-MM-dd}.log</fileNamePattern>
  32. <maxHistory>30</maxHistory>
  33. </rollingPolicy>
  34. <encoder>
  35. <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{40} - %msg%n</pattern>
  36. </encoder>
  37. </appender>
  38. <!-- 为莫个包下的所有类指定Appender 这里也可以指定类名称例如:com.aa.bb.ClassName-->
  39. <logger name="com.aa.bb" >
  40. <appender-ref ref="TaskLog" />
  41. </logger>
  42. <logger name="com.aa.cc" >
  43. <appender-ref ref="SendLog" />
  44. </logger>
  45. <root level="INFO">
  46. <appender-ref ref="AllLog" />
  47. </root>
  48. </configuration>

补充:第二个例子
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration scan="true" scanPeriod="1 seconds">
	<!-- 控制台输出日志 -->
	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<pattern>%-5level %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread]  %logger{100}[line:%L] - %msg%n</pattern>
		</encoder>
	</appender>

	<!-- 文件输出日志 (文件大小策略进行文件输出,超过指定大小对文件备份)-->
	<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
		 <!-- 过滤掉 TRACE 和 DEBUG 级别的日志:过滤低于INFO级别的-->
	    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
	      <level>INFO</level>
	    </filter>
	    <!--
		<file>logs/waterapp.log</file>
		-->
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<FileNamePattern>logs/waterapp.%d{yyyy-MM-dd}.log</FileNamePattern>
			<maxHistory>30</maxHistory> <!-- 保存30天 -->
		</rollingPolicy>
		<!--5MB自动压缩-->
		<!--triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
          <maxFileSize>5MB</maxFileSize>
        </triggeringPolicy-->
		<encoder>
     		 <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{100}[line:%L] - %msg%n</pattern>
   		 </encoder>
	</appender>

	<!--	-->
	<logger name="bingo" level="debug"/>
	<logger name="org.apache" level="ERROR"/>
	<logger name="org.springframework" level="ERROR"/>
	<logger name="org.lightframework" level="ERROR"/>
	<logger name="ch.qos.logback" level="ERROR"/>

	<root level="debug">
		<appender-ref ref="STDOUT" />
	</root>
</configuration>