springboot日志配置logback-spring.xml

springboot日志配置logback-spring.xml


日志配置文件logback-spring.xml

放在项目src/main/resources目录下




<configuration scan="false">

    
    <springProfile name="dev">

        
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                
                <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
            encoder>
        appender>

        
        <root level="INFO">
            <appender-ref ref="STDOUT"/>
        root>
    springProfile>

    
    <springProfile name="test">
        
        <property name="LOG_HOME" value="./log"/>

        
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                
                <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
            encoder>
        appender>

        
        <appender name="INFO_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${LOG_HOME}/info.logfile>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                
                <FileNamePattern>${LOG_HOME}/info-%d{yyyy-MM-dd}.logFileNamePattern>
                
                <MaxHistory>30MaxHistory>
            rollingPolicy>
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                
                <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
                
                <charset>UTF-8charset>
            encoder>
            
            <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
                <MaxFileSize>10MBMaxFileSize>
            triggeringPolicy>
        appender>

        
        <appender name="ERROR_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${LOG_HOME}/error.logfile>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                
                <FileNamePattern>${LOG_HOME}/error-%d{yyyy-MM-dd}.logFileNamePattern>
                
                <MaxHistory>30MaxHistory>
            rollingPolicy>
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                
                <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
                
                <charset>UTF-8charset>
            encoder>
            
            <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
                <MaxFileSize>10MBMaxFileSize>
            triggeringPolicy>
            <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                <level>ERRORlevel>
            filter>
        appender>

        
        <root level="INFO">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="INFO_APPENDER"/>
            <appender-ref ref="ERROR_APPENDER"/>
        root>
    springProfile>

    
    <springProfile name="prod">
        
        <property name="LOG_HOME" value="./log"/>

        
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                
                <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
            encoder>
        appender>

        
        <appender name="INFO_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${LOG_HOME}/info.logfile>
            <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
				
				<FileNamePattern>${LOG_HOME}/info.%d{yyyy-MM-dd}.%i.logFileNamePattern>
				<MaxHistory>30MaxHistory>
				
				<totalSizeCap>1GBtotalSizeCap>
				<MaxFileSize>10MBMaxFileSize>
			rollingPolicy>
			<encoder>
				<charset>UTF-8charset>
				
				<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
				pattern>
			encoder>
        appender>

        
        <appender name="ERROR_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${LOG_HOME}/error.logfile>
            <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
				
				<FileNamePattern>${LOG_HOME}/error.%d{yyyy-MM-dd}.%i.logFileNamePattern>
				<MaxHistory>30MaxHistory>
				
				<totalSizeCap>1GBtotalSizeCap>
				<MaxFileSize>10MBMaxFileSize>
			rollingPolicy>
			<encoder>
				<charset>UTF-8charset>
				
				<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
				pattern>
			encoder>
            <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                <level>ERRORlevel>
            filter>
        appender>

        
        <root level="INFO">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="INFO_APPENDER"/>
            <appender-ref ref="ERROR_APPENDER"/>
        root>
    springProfile>
configuration>

在配置文件中切换不同环境

application.properties文件中配置

spring.profiles.active = prod

你可能感兴趣的:(springboot,springboot)