SpringBoot——》配置logback日志文件

推荐链接:
    总结——》【Java】
    总结——》【Mysql】
    总结——》【Redis】
    总结——》【Kafka】
    总结——》【Spring】
    总结——》【SpringBoot】
    总结——》【MyBatis、MyBatis-Plus】
    总结——》【Linux】
    总结——》【MongoDB】
    总结——》【Elasticsearch】

SpringBoot——》配置logback日志文件

  • 一、application.yml
  • 二、logback-spring.xml

一、application.yml

logging:
  # logback.xml配置文件的位置
  config: classpath:logback-spring.xml

二、logback-spring.xml

SpringBoot——》配置logback日志文件_第1张图片


DOCTYPE xml>




<configuration scan="true" scanPeriod="10 seconds">

  <property name="CONTEXT_NAME" value="facade-portal" />
  <property name="LOG_PATH" value="logs" />
  <property name="MAX_FILE_SIZE" value="100MB" />
  <property name="MAX_HISTORY" value="30" />
  
  
  

  <contextName>${CONTEXT_NAME}contextName>

  
  
  <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
      <level>INFOlevel>
    filter>
    <encoder>
      
      <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %replace(%caller{1}){'\t|Caller.{1}0|\r\n', ''} -- %msg%nPattern>
      
      <charset>UTF-8charset>
    encoder>
  appender>

  
  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    
    <File>${LOG_PATH}/${CONTEXT_NAME}.logFile>
    
    <encoder>
      
      <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{50}.%M\(%line\) -- %msg%npattern>
      <charset>UTF-8charset> 
    encoder>
    
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      
      <fileNamePattern>${LOG_PATH}/${CONTEXT_NAME}-%d{yyyy-MM-dd}-%i.logfileNamePattern>
      
      <maxHistory>${MAX_HISTORY}maxHistory>
      <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
        
        <maxFileSize>${MAX_FILE_SIZE}maxFileSize>
      timeBasedFileNamingAndTriggeringPolicy>
    rollingPolicy>
  appender>

  
  <appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    
    <File>${LOG_PATH}/${CONTEXT_NAME}-error.logFile>
    <encoder>
      <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{50}.%M\(%line\) -- %msg%npattern>
            <charset>UTF-8charset> 
        encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_PATH}/${CONTEXT_NAME}_error-%d{yyyy-MM-dd}.logfileNamePattern>
            <maxHistory>${MAX_HISTORY}maxHistory>
        rollingPolicy>
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            
            <level>ERRORlevel>
            
            <onMatch>ACCEPTonMatch>
            
            <onMismatch>DENYonMismatch>
          
        filter>
    appender>

    
    <appender name="ASYNC_FILE" class="ch.qos.logback.classic.AsyncAppender">
        
        <queueSize>512queueSize>
        
        <appender-ref ref="FILE" />
    appender>
    <appender name="ASYNC_ERROR_FILE" class="ch.qos.logback.classic.AsyncAppender">
        
        <queueSize>512queueSize>
        
        <appender-ref ref="ERROR_FILE" />
    appender>
    
    
    
    
  
    
    


    
    <springProfile name="dev"> 
        <logger name="com.test.dao" level="debug"/> 
        <root level="DEBUG"> 
            <appender-ref ref="CONSOLE" /> 
            <appender-ref ref="ASYNC_FILE" />
            <appender-ref ref="ASYNC_ERROR_FILE" /> 
        root> 
    springProfile>

    <springProfile name="prod"> 
        <root level="INFO"> 
            <appender-ref ref="ASYNC_FILE" /> 
            <appender-ref ref="ASYNC_ERROR_FILE" /> 
        root>
    springProfile>
configuration>

你可能感兴趣的:(SpringBoot,spring,boot,logback,log)