Chinaunix首页 | 论坛 | 博客
  • 博客访问: 742318
  • 博文数量: 225
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2722
  • 用 户 组: 普通用户
  • 注册时间: 2013-02-03 17:32
文章分类

全部博文(225)

文章存档

2019年(7)

2018年(16)

2017年(1)

2016年(26)

2015年(41)

2014年(15)

2013年(119)

我的朋友

分类: Java

2018-04-19 15:56:41

springboot基于logback的多环境日志配置主要有两种方式:
1.在同一个logback文件中,通过springProfile标签来配置

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration scan="true" scanPeriod="60 seconds" debug="false">
  3.     <contextName>logback</contextName>
  4.     <!--输出到控制台-->
  5.     <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
  6.         <encoder>
  7.             <pattern>%d{HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
  8.         </encoder>
  9.     </appender>

  10.     <!--按天生成日志-->
  11.     <appender name="logFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
  12.         <Prudent>true</Prudent>
  13.         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  14.             <FileNamePattern>
  15.                 applog/%d{yyyy-MM-dd}/%d{yyyy-MM-dd}.log
  16.             </FileNamePattern>
  17.         </rollingPolicy>
  18.         <layout class="ch.qos.logback.classic.PatternLayout">
  19.             <Pattern>
  20.                 %d{yyyy-MM-dd HH:mm:ss} -%msg%n
  21.             </Pattern>
  22.         </layout>
  23.     </appender>
  24.     
  25.      <!-- 测试环境+开发环境. 多个使用逗号隔开. -->
  26.     <springProfile name="test,dev">
  27.         <!-- 开发环境打印sql日志 -->
  28.         <logger name="com.source.dao" level="DEBUG" additivity="false">
  29.             <appender-ref ref="consoleLog"/>
  30.         </logger>
  31.         <root level="info">
  32.             <appender-ref ref="consoleLog" />
  33.            </root>
  34.     </springProfile>

  35.     <!-- 生产环境. -->
  36.     <springProfile name="prod">
  37.         <root level="info">
  38.             <appender-ref ref="logFile" />
  39.            </root>
  40.     </springProfile>
  41.     
  42.     <!-- 如果项目启动没有指定profile,就按默认的日志配置 -->
  43.     <!-- logger是基础在root之上的对特殊包的配置,additivity表示是否向上级传递 -->
  44.     <logger name="com.source.dao" level="DEBUG" additivity="false">
  45.         <appender-ref ref="consoleLog"/>
  46.     </logger>
  47.     
  48.      <!--指定基础的日志输出等级-->
  49.     <root level="info">
  50.         <appender-ref ref="consoleLog" />
  51.     </root>
  52.    

  53. </configuration>
2.通过创建多个针对不同环境的logback-${spring.profiles.active}.xml文件,然后再application.properties里面指定加载路径logging.config=classpath:logback-${spring.profiles.active}.xml
配置结果:
1.开发环境

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration scan="true" scanPeriod="60 seconds" debug="false">
  3.     <contextName>logback</contextName>
  4.     <!--输出到控制台-->
  5.     <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
  6.         <encoder>
  7.             <pattern>%d{HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
  8.         </encoder>
  9.     </appender>

  10.     <!--按天生成日志-->
  11.     <appender name="logFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
  12.         <Prudent>true</Prudent>
  13.         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  14.             <FileNamePattern>
  15.                 applog/%d{yyyy-MM-dd}/%d{yyyy-MM-dd}.log
  16.             </FileNamePattern>
  17.         </rollingPolicy>
  18.         <layout class="ch.qos.logback.classic.PatternLayout">
  19.             <Pattern>
  20.                 %d{yyyy-MM-dd HH:mm:ss} -%msg%n
  21.             </Pattern>
  22.         </layout>
  23.     </appender>
  24.     
  25.     <!-- 如果项目启动没有指定profile,就按默认的日志配置 -->
  26.     <!-- logger是基础在root之上的对特殊包的配置,additivity表示是否向上级传递 -->
  27.     <logger name="com.source.dao" level="DEBUG" additivity="false">
  28.         <appender-ref ref="consoleLog"/>
  29.     </logger>
  30.     
  31.      <!--指定基础的日志输出等级-->
  32.     <root level="info">
  33.         <appender-ref ref="consoleLog" />
  34.     </root>
  35.    

  36. </configuration>
2.生成环境

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- 分级别异步文件日志输出配置 -->
  3. <configuration>
  4.     <!-- 日志级别 -->
  5.     <property name="logLevel" value="INFO"></property>
  6.     <!-- 日志地址 -->
  7.     <property name="logPath" value="./logs"></property>
  8.     <!-- 最大保存时间 -->
  9.     <property name="maxHistory" value="30"/>
  10.     <!-- 异步缓冲队列的深度,该值会影响性能.默认值为256 -->
  11.     <property name="queueSize" value="512"></property>

  12.     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
  13.         <encoder>
  14.             <pattern>%d{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
  15.         </encoder>
  16.     </appender>

  17.     <appender name="FILE_DEBUG"
  18.               class="ch.qos.logback.core.rolling.RollingFileAppender">
  19.         <filter class="ch.qos.logback.classic.filter.LevelFilter">
  20.             <level>DEBUG</level>
  21.             <onMatch>ACCEPT</onMatch>
  22.             <onMismatch>DENY</onMismatch>
  23.         </filter>
  24.         <file>${logPath}/cms_debug.log</file>
  25.         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  26.             <fileNamePattern>${logPath}/cms_debug.log.%d{yyyy-MM-dd}.zip
  27.             </fileNamePattern>
  28.             <maxHistory>${maxHistory}</maxHistory>
  29.         </rollingPolicy>
  30.         <encoder>
  31.             <pattern>%d{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
  32.         </encoder>
  33.     </appender>
  34.     <appender name="FILE_INFO"
  35.               class="ch.qos.logback.core.rolling.RollingFileAppender">
  36.         <filter class="ch.qos.logback.classic.filter.LevelFilter">
  37.             <level>INFO</level>
  38.             <onMatch>ACCEPT</onMatch>
  39.             <onMismatch>DENY</onMismatch>
  40.         </filter>
  41.         <file>${logPath}/cms_info.log</file>
  42.         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  43.             <fileNamePattern>${logPath}/cms_info.log.%d{yyyy-MM-dd}.zip
  44.             </fileNamePattern>
  45.             <maxHistory>${maxHistory}</maxHistory>
  46.         </rollingPolicy>
  47.         <encoder>
  48.             <pattern>%d{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
  49.         </encoder>
  50.     </appender>

  51.     <appender name="FILE_WARN"
  52.               class="ch.qos.logback.core.rolling.RollingFileAppender">
  53.         <filter class="ch.qos.logback.classic.filter.LevelFilter">
  54.             <level>WARN</level>
  55.             <onMatch>ACCEPT</onMatch>
  56.             <onMismatch>DENY</onMismatch>
  57.         </filter>
  58.         <file>${logPath}/cms_warn.log</file>
  59.         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  60.             <fileNamePattern>${logPath}/cms_warn.log.%d{yyyy-MM-dd}.zip
  61.             </fileNamePattern>
  62.             <maxHistory>${maxHistory}</maxHistory>
  63.         </rollingPolicy>
  64.         <encoder>
  65.             <pattern>%d{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
  66.         </encoder>
  67.     </appender>
  68.     <appender name="FILE_ERROR"
  69.               class="ch.qos.logback.core.rolling.RollingFileAppender">
  70.         <filter class="ch.qos.logback.classic.filter.LevelFilter">
  71.             <level>ERROR</level>
  72.             <onMatch>ACCEPT</onMatch>
  73.             <onMismatch>DENY</onMismatch>
  74.         </filter>
  75.         <file>${logPath}/cms_error.log</file>
  76.         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  77.             <fileNamePattern>${logPath}/cms_error.log.%d{yyyy-MM-dd}.zip
  78.             </fileNamePattern>
  79.             <maxHistory>${maxHistory}</maxHistory>
  80.         </rollingPolicy>
  81.         <encoder>
  82.             <pattern>%d{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
  83.         </encoder>
  84.     </appender>

  85.     <appender name="ASYNC_LOG_DEBUG" class="ch.qos.logback.classic.AsyncAppender">
  86.         <!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 -->
  87.         <discardingThreshold>0</discardingThreshold>
  88.         <!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
  89.         <queueSize>${queueSize}</queueSize>
  90.         <appender-ref ref="FILE_DEBUG"/>
  91.     </appender>
  92.     <appender name="ASYNC_LOG_INFO" class="ch.qos.logback.classic.AsyncAppender">
  93.         <!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 -->
  94.         <discardingThreshold>0</discardingThreshold>
  95.         <!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
  96.         <queueSize>${queueSize}</queueSize>
  97.         <appender-ref ref="FILE_INFO"/>
  98.     </appender>
  99.     <appender name="ASYNC_LOG_WARN" class="ch.qos.logback.classic.AsyncAppender">
  100.         <!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 -->
  101.         <discardingThreshold>0</discardingThreshold>
  102.         <!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
  103.         <queueSize>${queueSize}</queueSize>
  104.         <appender-ref ref="FILE_WARN"/>
  105.     </appender>
  106.     <appender name="ASYNC_LOG_ERROR" class="ch.qos.logback.classic.AsyncAppender">
  107.         <!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 -->
  108.         <discardingThreshold>0</discardingThreshold>
  109.         <!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
  110.         <queueSize>${queueSize}</queueSize>
  111.         <appender-ref ref="FILE_ERROR"/>
  112.     </appender>
  113.     <root level="${logLevel}">
  114.         <!-- appender referenced after it is defined -->
  115.         <appender-ref ref="STDOUT"/>
  116.         <appender-ref ref="ASYNC_LOG_DEBUG"/>
  117.         <appender-ref ref="ASYNC_LOG_INFO"/>
  118.         <appender-ref ref="ASYNC_LOG_WARN"/>
  119.         <appender-ref ref="ASYNC_LOG_ERROR"/>
  120.     </root>
  121. </configuration>
logback异步记录日志的原理,也是使用一个缓冲队列,当缓冲数量到一定阀值时,才把日志写到文件里。
阅读(1150) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~