【springboot】SpringBoot配置logback.xml 多环境

前提

logback日志文件要实现springboot多环境配置,不然每次都需要修改logback.xml里面的配置文件,所以很麻烦。


操作步骤

  1. resource文件的内容结构如下:
    【springboot】SpringBoot配置logback.xml 多环境_第1张图片

  2. 配置application.yml

此步骤是实现控制多环境日志文件的关键。

spring:
  profiles:
    active: dev

logging:
  config: classpath:logback-${spring.profiles.active}.xml

【springboot】SpringBoot配置logback.xml 多环境_第2张图片

  1. 配置lockback-dev.xml
    这个地方就可以实现自己的多环境日志配置了,只列举了一个。

<configuration>
    

    <property name="log.path" value="/Users/he/ruoyi/logs" />
    
	<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n" />

	
	<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<pattern>${log.pattern}pattern>
		encoder>
	appender>
	
	
	<appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
	    <file>${log.path}/sys-info.logfile>
        
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
			<fileNamePattern>${log.path}/sys-info.%d{yyyy-MM-dd}.logfileNamePattern>
			
			<maxHistory>60maxHistory>
		rollingPolicy>
		<encoder>
			<pattern>${log.pattern}pattern>
		encoder>
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
            
            <level>INFOlevel>
            
            <onMatch>ACCEPTonMatch>
            
            <onMismatch>DENYonMismatch>
        filter>
	appender>
	
	<appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
	    <file>${log.path}/sys-error.logfile>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${log.path}/sys-error.%d{yyyy-MM-dd}.logfileNamePattern>
			
			<maxHistory>60maxHistory>
        rollingPolicy>
        <encoder>
            <pattern>${log.pattern}pattern>
        encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            
            <level>ERRORlevel>
			
            <onMatch>ACCEPTonMatch>
			
            <onMismatch>DENYonMismatch>
        filter>
    appender>
	
	
    <appender name="sys-user" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${log.path}/sys-user.logfile>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${log.path}/sys-user.%d{yyyy-MM-dd}.logfileNamePattern>
            
            <maxHistory>60maxHistory>
        rollingPolicy>
        <encoder>
            <pattern>${log.pattern}pattern>
        encoder>
    appender>
	
	
	<logger name="com.ruoyi" level="info" />
	
	<logger name="org.springframework" level="warn" />

	<root level="info">
		<appender-ref ref="console" />
	root>
	
	
    <root level="info">
        <appender-ref ref="file_info" />
        <appender-ref ref="file_error" />
    root>
	
	
    <logger name="sys-user" level="info">
        <appender-ref ref="sys-user"/>
    logger>
configuration> 

总结

要实现多环境的配置,主要是依赖于springboot的application.yml文件去实现。

你可能感兴趣的:(springboot,springboot,logback,多环境)