spring_boot combat diary (ii) the use and configuration logback

Log: All information describes the system running log of all.

Log capacity:

  1. customize the output target.

  2. Customize the output format.

  3. carry contextual information

  4. Select the output operation.

  5. Flexible Configuration

Log options:

  Log facade: JCL (and not a Logback Author) SLF4j (same author) jboos-logging (for the non-public)

  Log realization: Log4j (the author himself says sucks) Log4j2 (too complex, the optimal performance, but basically not used) Logback (Log4j second generation) JUL (too shabby)

  Obviously we can and should choose: SLF4j + Logback.

how to use:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
 * Created by kkm on 2019/12/3.
 */
@RunWith(SpringRunner.class)
@SpringBootTest

public class LoggerTest {
    private final Logger logger = LoggerFactory.getLogger(LoggerTest.class);
    @Test
    public void test1(){
        logger.debug("debug...");
        logger.info("info...");
        logger.error("error...");
    }
}
View Code

Right-run test1, you can see the output in the console.

Can not see the debug output, because the info level higher than his, if the below info to be output, then debug output will not.

The following window system can use the keys ctrl + shift + alt + N, then enter the Find box type exhaled Level slf4j to find you can see various levels of logging.

 

 

LoggerFactory logger is instantiated, this place should fill in the corresponding class name.

 Now you can see the output is the current class name, so if you change the other categories, the output will change.

 Some small partners say this every time I write have to write this instantiation, too much trouble, in fact, if you understand inversion of control, then this one is omitted, the use of plug-in lombok, you can use annotations omitted this sentence words, but this is another pit.

 

If there are plug-in lombok, you only need to write

package com.ccpuc;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
 * Created by kkm on 2019/12/8.
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@ slf4j
public class LoggerTest {
    @Test
    public void test1(){
        log.debug("debug...");
        log.info("info...");
        log.error("error...");
    }
}
View Code

We can see the results the same

2019-12-08 21: 06: 46,738 INFO 7384 --- [main] com.ccpuc.LoggerTest: info ...
 2019-12-08 21: 06: 46,738 --- 7384 ERROR [main] com.ccpuc. LoggerTest: error ...
View Code

In addition to using string concatenation, log supports formatted output.

log.info ( "name: {} ,,, id: {}", " braces first", "second braces" );

Output: 2019-12-08 21 is: 10: 53.209 13036 --- the INFO [main] com.ccpuc.LoggerTest: name: a first brace ,,, id: second braces
View Code

These are the console output, logback also supports file output.

 

Logback configuration can be configured by applicatin.yml file.

logging.pattern.console: format output to the console (incidentally, yml use Notes #).

logging.config: Profile Path

logging.path: log file path output (this will be used as the file name of the default spring)

logging.file: log file path output (this can be specified)

logging.level: Specifies the level of the output log, level can also specify a packet to the specified class.

logging:
  pattern:
#    console: "%d - %msg%n"
#  path: C:\Users\kkm\Desktop\actual\dsell_test\log
#  file: C:\Users\kkm\Desktop\actual\dsell_test\log\dsell
#  level:
#    com.ccpuc.LoggerTest : debug

# Remove either take effect.

 Remember to comment out above and then use logback-spring.xml to configure.

Also can be configured through logback-spring.xml, application.yml above configuration is relatively simple. Often need to use xml to configure to meet our daily needs.

Specific comments can look at the code ... explain ...

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

<configuration>
    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
                <!--日志格式-->
                <pattern>
                    %d - %msg%n
                </pattern>
        </layout>
    </appender>

    <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">

        <! - The level of the filter -> 
        < filter class = "ch.qos.logback.classic.filter.LevelFilter" > 
            <! - to filter other than the INFO level, if onMatch, to ACCEPT, is to receive the log, Conversely DENY, the filter is not -> 
            < Level > INFO </ Level > 
            < onMatch > ACCEPT </ onMatch > 
            < onMismatch > DENY </ onMismatch > 
        </ filter > 
        < Encoder > 
            <-! log format -> 
            < pattern >
                %msg%n
            </ Pattern > 
        </ Encoder > 
        <-! Scroll policy, based on time -> 
        < rollingPolicy class = "ch.qos.logback.core.rolling.TimeBasedRollingPolicy" > 
            <-! Output file -> 
            < fileNamePattern >
                C:\Users\kkm\Desktop\actual\dsell_test\log\info\info.%d.log
            </fileNamePattern>
        </rollingPolicy>
    </appender>


    < The appender name = "fileErrorLog" class = "ch.qos.logback.core.rolling.RollingFileAppender" > 
        <-! Filter according to level range, is selected to filter out low-level log information -> 
        < filter class = "CH .qos.logback.classic.filter.ThresholdFilter " > 
            < Level > ERROR </ Level > 
        </ filter > 
        < Encoder > 
            <-! log format -> 
            < pattern >
                %msg%n
            </ Pattern > 
        </ Encoder > 
        <-! Scroll policy, based on time -> 
        < rollingPolicy class = "ch.qos.logback.core.rolling.TimeBasedRollingPolicy" > 
            <-! Output file -> 
            < fileNamePattern >
                C:\Users\kkm\Desktop\actual\dsell_test\log\error\error.%d.log
            </fileNamePattern>
        </rollingPolicy>
    </appender>


    <root level="info">
        <appender-ref ref="consoleLog" />
        <appender-ref ref="fileInfoLog" />
        <appender-ref ref="fileErrorLog" />
    </root>

</configuration>
View Code

 

Guess you like

Origin www.cnblogs.com/miaoliangJUN/p/11974410.html