Home  >  Article  >  Java  >  How to solve the error when integrating log4j with springboot

How to solve the error when integrating log4j with springboot

王林
王林forward
2023-05-11 10:07:051720browse

    1. Adding dependencies

    1.1. Adding dependencies

    You need to introduce dependency support for log4j. It is recommended that you determine the version to use.

    
            org.apache.logging.log4j
            log4j-to-slf4j
            2.11.2
    

    1.2. Eliminate dependencies

    springboot has built-in support for logs by default, and all of them need to be removed, otherwise it will affect the dependency of log4j.

    
            org.springframework.boot
            spring-boot-starter-logging
            
                
                   *
                    *
                
            
    

    2. Configuration log

    2.1. Log printing record

     Configure related configurations according to your own needs. What needs to be noted here is to use xml files for configuration. There are pitfalls in using properties. The file name is customized. There are no requirements. It will be specified in the configuration file.

    
    
        beordie
        
        
        
        
        
    
        
            
                info
            
            
                ${CONSOLE_LOG_PATTERN}
                UTF-8
            
        
    
        
            ${path}/debug.log
            
                %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
                UTF-8
            
            
                ${log}/debug/log-debug-%d{yyyy-MM-dd}.%i.log
                
                    100MB
                
                15
            
            
                debug
                ACCEPT
                DENY
            
        
    
        
            ${log}/info.log
            
                %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
                UTF-8
            
            
                ${log}/info/log-info-%d{yyyy-MM-dd}.%i.log
                
                    100MB
                
                15
            
            
                info
                ACCEPT
                DENY
            
        
    
        
            ${log}/warn.log
            
                %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
                UTF-8
            
            
                ${log}/warn/log-warn-%d{yyyy-MM-dd}.%i.log
                
                    100MB
                
                15
            
            
                warn
                ACCEPT
                DENY
            
        
    
        
            ${log}/error.log
            
                %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
                UTF-8
            
            
                ${log}/error/log-error-%d{yyyy-MM-dd}.%i.log
                
                    100MB
                
                15
            
            
                ERROR
                ACCEPT
                DENY
            
        
    
        
            
        
    
        
            
            
            
            
            
        
    

    2.2. Specify the configuration file

    You also need to specify the configuration in the project configuration file of spring boot.

    logging:
      config: classpath:log4j.xml

    Complete the overall configuration of the log here. Start the project and get the log object through the following statement to print the log.

    private final Logger LOGGING = LoggerFactory.getLogger(ArticleController.class);

    Supplement: log4j tuning and precautions

    Log mode-synchronization/asynchronous

    log4j2 provides AsyncAppender, AsyncLogger and global asynchronous, enable The method is as follows:

    • Synchronous mode: The default configuration is synchronous mode, that is, no AsyncAppender and AsyncLogger are used.

    • Global asynchronous: The configuration is configured in a synchronous manner. Global asynchronous can be turned on by adding jvm startup parameters without modifying the configuration and application.

    • Mixed asynchronous: Use a mixed configuration of asynchronous Logger and synchronous Logger, and do not enable global asynchronous, that is, part of the Logger configuration is AsyncLogger and part of Logger.

    Notes on using log mode:

    • If you use asynchronous, it is recommended to use AsyncLogger implementation instead of AsyncAppender.

    • If you use synchronization, you can only use one of AsyncLogger, AsyncAppender and global async. You cannot configure AsyncAppender and AsyncLogger at the same time, or enable global async when async is configured.

    Log rolling and clearing strategy

    log4j2 provides a file size-based rolling strategy and a time-based rolling strategy, or both can be used together , here are the size-based rolling policy configuration and the size/time-based dual rolling policy configuration:

    • Size-based rolling policy: roll according to size, enable compression, and retain at most N File

    • Based on size/time dual rolling rolling strategy: rolling based on size and time, enabling compression, controlling the maximum number of logs retained per unit time and controlling the total log retention time.

    The above is the detailed content of How to solve the error when integrating log4j with springboot. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete