SpringBoot build integrated applications of Logback

A sort of dependency

  When we build applications with the IDEA of a simple monomer, it depends POM is already integrated in the dependence of logback;

  We can open pom.xml

  

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

  Click into the spring-boot-starter-parent where dependent on the spring-boot-dependencies

 

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.1.7.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
  </parent>

 

  And then into the spring-boot-dependencies found in spring-boot-starter

  <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>2.1.7.RELEASE</version>
  </dependency>

  And then into the spring-boot-starter to find spring-boot-starter-logging

   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-logging</artifactId>
      <version>2.1.7.RELEASE</version>
      <scope>compile</scope>
    </dependency>

  Click to go to be able to see relied logback

<dependencies>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.2.3</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-to-slf4j</artifactId>
      <version>2.11.2</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>jul-to-slf4j</artifactId>
      <version>1.7.26</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>

 Two xml configuration

First, the official recommended the name xml format: logback-spring.xml instead logback.xml, as to why, because with spring suffixes may use <springProfile> label  

We create a new logback-spring.xml follows in the resource directory

 

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>

    <springProperty scope="context" name="logLevel" source="logging.pattern.level"/>
    <springProperty scope="context" name="logPath" source="logging.path"/>
    <springProperty scope="context" name="fileName" source="logging.file"/>

    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>
                %date [%thread] %-5level %logger{80} - %msg%n
            </pattern>
        </layout>
    </appender>

    <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>DENY</onMatch>
            <onMismatch>ACCEPT</onMismatch>
        </filter>
        <encoder>
            <pattern>
                %date [%thread] %-5level %logger{80} - %msg%n
            </pattern>
        </encoder>
        <!--滚动策略-->
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!--路径-->
            <fileNamePattern>${logPath}/${fileName}-%d{yyyy-MM-dd}-%i.log</fileNamePattern>
            <maxFileSize>200MB</maxFileSize>
            <maxHistory>60</maxHistory>
        </rollingPolicy>

    </appender>


    <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERROR</level>
        </filter>
        <encoder>
            <pattern>
                %date [%thread] %-5level %logger{80} - %msg%n
            </pattern>
        </encoder>
        <!--滚动策略-->
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!--路径-->
            <fileNamePattern>${logPath}/${fileName}-%d{yyyy-MM-dd}-error-%i.log</fileNamePattern>
            <maxFileSize>200MB</maxFileSize>
            <maxHistory> 60 </ maxHistory> 
        </rollingPolicy>
    </ the appender> 

    <! - here arranged below the log level for a specific packet -> 
    <! - the profile logging level is set item -> 
    <name = Logger "com.pyq.blog" Level = "$ {logLevel}" /> 
    <-! open comment when you want to print their own local development sql but do not submit -> 

    <springProfile name = "dev"> 
        <-! this log level is that for all packages -> 
        <= the root level "$ {} the logLevel"> 
            <REF-REF = the appender "consolelog" /> 
<-! <REF-REF = the appender "fileInfoLog" /> - > 
            <-! <REF-REF = the appender "fileErrorLog" /> -> 
        </ the root> 
    </ springProfile> 

    <name = springProfile "Test"> 
        <= the root Level "$ {} the logLevel"> 
            <-! here log level for all packages ->${logLevel}">
            <!--<appender-ref ref="consoleLog" />-->
            <appender-ref ref="fileInfoLog" />
            <appender-ref ref="fileErrorLog" />
        </root>
    </springProfile>

    <springProfile name="prod">
        <root level="${logLevel}">
            <!--此处是针对所有包的日志级别-->
            <!--<appender-ref ref="consoleLog" />-->
            <appender-ref ref="fileInfoLog" />
            <appender-ref ref="fileErrorLog" />
        </root>
    </springProfile>

</configuration>

 

  

  The above package name their own replacement

  Three yml configuration log

logging:
  path: D:\log
  file: ${spring.application.name}
  pattern:
    level: INFO

  

Guess you like

Origin www.cnblogs.com/pangyangqi/p/11443035.html