springboot基于logback的多环境日志配置主要有两种方式:
1.在同一个logback文件中,通过springProfile标签来配置
-
<?xml version="1.0" encoding="UTF-8"?>
-
<configuration scan="true" scanPeriod="60 seconds" debug="false">
-
<contextName>logback</contextName>
-
<!--输出到控制台-->
-
<appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
-
<encoder>
-
<pattern>%d{HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
-
</encoder>
-
</appender>
-
-
<!--按天生成日志-->
-
<appender name="logFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
-
<Prudent>true</Prudent>
-
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
-
<FileNamePattern>
-
applog/%d{yyyy-MM-dd}/%d{yyyy-MM-dd}.log
-
</FileNamePattern>
-
</rollingPolicy>
-
<layout class="ch.qos.logback.classic.PatternLayout">
-
<Pattern>
-
%d{yyyy-MM-dd HH:mm:ss} -%msg%n
-
</Pattern>
-
</layout>
-
</appender>
-
-
<!-- 测试环境+开发环境. 多个使用逗号隔开. -->
-
<springProfile name="test,dev">
-
<!-- 开发环境打印sql日志 -->
-
<logger name="com.source.dao" level="DEBUG" additivity="false">
-
<appender-ref ref="consoleLog"/>
-
</logger>
-
<root level="info">
-
<appender-ref ref="consoleLog" />
-
</root>
-
</springProfile>
-
-
<!-- 生产环境. -->
-
<springProfile name="prod">
-
<root level="info">
-
<appender-ref ref="logFile" />
-
</root>
-
</springProfile>
-
-
<!-- 如果项目启动没有指定profile,就按默认的日志配置 -->
-
<!-- logger是基础在root之上的对特殊包的配置,additivity表示是否向上级传递 -->
-
<logger name="com.source.dao" level="DEBUG" additivity="false">
-
<appender-ref ref="consoleLog"/>
-
</logger>
-
-
<!--指定基础的日志输出等级-->
-
<root level="info">
-
<appender-ref ref="consoleLog" />
-
</root>
-
-
-
</configuration>
2.通过创建多个针对不同环境的logback-${spring.profiles.active}.xml文件,然后再application.properties里面指定加载路径logging.config=classpath:logback-${spring.profiles.active}.xml
配置结果:
1.开发环境
-
<?xml version="1.0" encoding="UTF-8"?>
-
<configuration scan="true" scanPeriod="60 seconds" debug="false">
-
<contextName>logback</contextName>
-
<!--输出到控制台-->
-
<appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
-
<encoder>
-
<pattern>%d{HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
-
</encoder>
-
</appender>
-
-
<!--按天生成日志-->
-
<appender name="logFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
-
<Prudent>true</Prudent>
-
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
-
<FileNamePattern>
-
applog/%d{yyyy-MM-dd}/%d{yyyy-MM-dd}.log
-
</FileNamePattern>
-
</rollingPolicy>
-
<layout class="ch.qos.logback.classic.PatternLayout">
-
<Pattern>
-
%d{yyyy-MM-dd HH:mm:ss} -%msg%n
-
</Pattern>
-
</layout>
-
</appender>
-
-
<!-- 如果项目启动没有指定profile,就按默认的日志配置 -->
-
<!-- logger是基础在root之上的对特殊包的配置,additivity表示是否向上级传递 -->
-
<logger name="com.source.dao" level="DEBUG" additivity="false">
-
<appender-ref ref="consoleLog"/>
-
</logger>
-
-
<!--指定基础的日志输出等级-->
-
<root level="info">
-
<appender-ref ref="consoleLog" />
-
</root>
-
-
-
</configuration>
2.生成环境
-
<?xml version="1.0" encoding="UTF-8"?>
-
<!-- 分级别异步文件日志输出配置 -->
-
<configuration>
-
<!-- 日志级别 -->
-
<property name="logLevel" value="INFO"></property>
-
<!-- 日志地址 -->
-
<property name="logPath" value="./logs"></property>
-
<!-- 最大保存时间 -->
-
<property name="maxHistory" value="30"/>
-
<!-- 异步缓冲队列的深度,该值会影响性能.默认值为256 -->
-
<property name="queueSize" value="512"></property>
-
-
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
-
<encoder>
-
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
-
</encoder>
-
</appender>
-
-
<appender name="FILE_DEBUG"
-
class="ch.qos.logback.core.rolling.RollingFileAppender">
-
<filter class="ch.qos.logback.classic.filter.LevelFilter">
-
<level>DEBUG</level>
-
<onMatch>ACCEPT</onMatch>
-
<onMismatch>DENY</onMismatch>
-
</filter>
-
<file>${logPath}/cms_debug.log</file>
-
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
-
<fileNamePattern>${logPath}/cms_debug.log.%d{yyyy-MM-dd}.zip
-
</fileNamePattern>
-
<maxHistory>${maxHistory}</maxHistory>
-
</rollingPolicy>
-
<encoder>
-
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
-
</encoder>
-
</appender>
-
<appender name="FILE_INFO"
-
class="ch.qos.logback.core.rolling.RollingFileAppender">
-
<filter class="ch.qos.logback.classic.filter.LevelFilter">
-
<level>INFO</level>
-
<onMatch>ACCEPT</onMatch>
-
<onMismatch>DENY</onMismatch>
-
</filter>
-
<file>${logPath}/cms_info.log</file>
-
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
-
<fileNamePattern>${logPath}/cms_info.log.%d{yyyy-MM-dd}.zip
-
</fileNamePattern>
-
<maxHistory>${maxHistory}</maxHistory>
-
</rollingPolicy>
-
<encoder>
-
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
-
</encoder>
-
</appender>
-
-
<appender name="FILE_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>${logPath}/cms_warn.log</file>
-
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
-
<fileNamePattern>${logPath}/cms_warn.log.%d{yyyy-MM-dd}.zip
-
</fileNamePattern>
-
<maxHistory>${maxHistory}</maxHistory>
-
</rollingPolicy>
-
<encoder>
-
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
-
</encoder>
-
</appender>
-
<appender name="FILE_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>${logPath}/cms_error.log</file>
-
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
-
<fileNamePattern>${logPath}/cms_error.log.%d{yyyy-MM-dd}.zip
-
</fileNamePattern>
-
<maxHistory>${maxHistory}</maxHistory>
-
</rollingPolicy>
-
<encoder>
-
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
-
</encoder>
-
</appender>
-
-
<appender name="ASYNC_LOG_DEBUG" class="ch.qos.logback.classic.AsyncAppender">
-
<!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 -->
-
<discardingThreshold>0</discardingThreshold>
-
<!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
-
<queueSize>${queueSize}</queueSize>
-
<appender-ref ref="FILE_DEBUG"/>
-
</appender>
-
<appender name="ASYNC_LOG_INFO" class="ch.qos.logback.classic.AsyncAppender">
-
<!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 -->
-
<discardingThreshold>0</discardingThreshold>
-
<!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
-
<queueSize>${queueSize}</queueSize>
-
<appender-ref ref="FILE_INFO"/>
-
</appender>
-
<appender name="ASYNC_LOG_WARN" class="ch.qos.logback.classic.AsyncAppender">
-
<!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 -->
-
<discardingThreshold>0</discardingThreshold>
-
<!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
-
<queueSize>${queueSize}</queueSize>
-
<appender-ref ref="FILE_WARN"/>
-
</appender>
-
<appender name="ASYNC_LOG_ERROR" class="ch.qos.logback.classic.AsyncAppender">
-
<!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 -->
-
<discardingThreshold>0</discardingThreshold>
-
<!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
-
<queueSize>${queueSize}</queueSize>
-
<appender-ref ref="FILE_ERROR"/>
-
</appender>
-
<root level="${logLevel}">
-
<!-- appender referenced after it is defined -->
-
<appender-ref ref="STDOUT"/>
-
<appender-ref ref="ASYNC_LOG_DEBUG"/>
-
<appender-ref ref="ASYNC_LOG_INFO"/>
-
<appender-ref ref="ASYNC_LOG_WARN"/>
-
<appender-ref ref="ASYNC_LOG_ERROR"/>
-
</root>
-
</configuration>
logback异步记录日志的原理,也是使用一个缓冲队列,当缓冲数量到一定阀值时,才把日志写到文件里。
阅读(1150) | 评论(0) | 转发(0) |