2, configure a number of data sources (H2 database). 3, configure a number of data sources (Mysql database). 【 attach source 】Spring Boot actual combat series – four, log management 【 attach source 】Spring Boot actual combat series – five, transaction abstraction 【 attach source 】Spring Boot actual combat series – six, currency storage database processing

The final result

  • Console printing
  • Error. Log File saving error log
  • Info. Log Log file saving level >= INFO

The configuration file

logback-spring.xml

<? xml version="1.0" encoding="UTF-8"? ><configuration>
    <! Properties file: Find the corresponding configuration item in the Properties file -->
    <springProperty scope="context" name="logging.file.path" source="logging.file.path"/>
    <springProperty scope="context" name="logging.file.name" source="logging.file.name"/>
    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <charset>UTF-8</charset>
            <! Format output (color matching) : %d indicates the date, %thread indicates the thread name, %-5level indicates the level from the left 5 character width % MSG: log message, %n indicates the newline character -- -->
            <! --<pattern>%yellow(%d{yyyy-MM-dd HH:mm:ss}) %red([%thread]) %highlight(%-5level) %cyan(%logger{50}) - %magenta(%msg) %n</pattern>-->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %highlight(%-5level) %logger{50} - %msg%n</pattern>
        </encoder>
    </appender>


    <! -- Separate logs according to log level and output to different files -->
    <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>
                %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n
            </pattern>
            <charset>UTF-8</charset>
        </encoder>
        <! -- Scroll strategy -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <! You can save logs by hour, by day, by month.
            <fileNamePattern>${logging.file.path}/${logging.file.name}.all.%d{yyyy-MM-dd}.log</fileNamePattern>
            <! -- Save time -->
            <MaxHistory>90</MaxHistory>
            <! -- File size -->
            <totalSizeCap>10GB</totalSizeCap>
        </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>
                %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n
            </pattern>
        </encoder>
        <! -- Scroll strategy -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <! -- -- -- > path
            <fileNamePattern>${logging.file.path}/${logging.file.name}.error.%d{yyyy-MM-dd}.log</fileNamePattern>
            <MaxHistory>90</MaxHistory>
        </rollingPolicy>
    </appender>
    <root level="info">
        <appender-ref ref="consoleLog"/>
        <appender-ref ref="fileInfoLog"/>
        <appender-ref ref="fileErrorLog"/>
    </root>
</configuration
Copy the code

Method of use

  • Add an annotation @slf4j
  • Prints logs of different levels through the log object
@SpringBootApplication
@Slf4j
public class SpringDemoApplication implements CommandLineRunner {


    public static void main(String[] args) {
        SpringApplication.run(SpringDemoApplication.class, args);
    }


    @Override
    public void run(String. args) throws Exception { log.info("this is a demo for log");
        log.warn("this is a demo for log");
        log.error("this is a demo for log");
    }

Copy the code

Source code download address

Github.com/qiulanzhu/S…