springboot配置logback.xml(多环境配置)

一、配置dev,pro环境,通过spring.profiles.active属性切换日志配置

springboot配置logback.xml(多环境配置)_第1张图片

1.application.yml配置

# Spring配置
spring:
  profiles:
    active: dev
# 指定日志配置文件(默认)
logging:
  config: classpath:logback.xml

2.application-dev.yml配置

# 指定日志配置文件
logging:
  config: classpath:logback-dev.xml

3.logback.xml (test,pro环境linux)


<configuration>
    
    <property name="log.path" value="/home/hsc/logs/supervision" />
    
	<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>
	
	
	<logger name="com.hwasee" level="info" />

	<root level="info">
		<appender-ref ref="console" />
	root>
	
	
    <root level="info">
        <appender-ref ref="file_info" />
        <appender-ref ref="file_error" />
    root>
	
configuration> 

4.logback-dev.xml (dev环境windows)


<configuration>
    
    <springProperty scope="context" name="appName" source="spring.application.name" />
    <property name="log.path" value="./日志/${appName}" />
    
	<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>
	
	
	<logger name="com.hwasee" level="info" />

	<root level="info">
		<appender-ref ref="console" />
	root>
	
	
    <root level="info">
        <appender-ref ref="file_info" />
        <appender-ref ref="file_error" />
    root>

configuration> 

dev开发环境windows:(spring.application.name配置的即为health-hsc-supervision)
在这里插入图片描述

二、springProperty和springProfile

1.logback.xml (springProfile:测试时不生效可能是版本问题)

<springProfile name="dev">
    
springProfile>
 
<springProfile name="dev,test">
    
springProfile>
 
<springProfile name="!prod">
    
springProfile>

2.logback.xml (springProperty)

    
    <springProperty scope="context" name="appName" source="spring.application.name" />
    <property name="log.path" value="./日志/${appName}" />

在这里插入图片描述

你可能感兴趣的:(Java,springboot,spring,boot,spring,logback)