Home >Java >javaTutorial >How to handle logs in Springboot project

How to handle logs in Springboot project

王林
王林forward
2023-05-24 13:34:061569browse

How to handle logs in Springboot projectHow to handle logs in Springboot project

As shown above, a new log file will be generated every day, and then the logs will be classified. I only classify error and info here.
How to do it?
First, create a new file in the resource directory and name it logback-spring.xml

How to handle logs in Springboot project

<?xml  version="1.0" encoding="UTF-8" ?>
<configuration>
    <appender>
        <layout>
            <pattern>
                %d - %msg%n
            </pattern>
        </layout>
    </appender>

    <appender>
        <filter>
            <level>
                ERROR
            </level>
            <onmatch>DENY</onmatch>
            <onmismatch>ACCEPT</onmismatch>
        </filter>
        <encoder>
            <pattern>
                %msg%n
            </pattern>
        </encoder>
        <!--  滚动策略-->
        <rollingpolicy>
        <!--   路径-->
            <filenamepattern>
            /var/log/tomcat/sell/info.%d.log
            </filenamepattern>
        </rollingpolicy>
    </appender>

    <appender>
        <filter>
            <level>
                ERROR
            </level>
        </filter>
        <encoder>
            <pattern>
                %msg%n
            </pattern>
        </encoder>
        <!--  滚动策略-->
        <rollingpolicy>
            <!--   路径-->
            <filenamepattern>
                /var/log/tomcat/sell/error.%d.log
            </filenamepattern>
        </rollingpolicy>
    </appender>

    <root>
        <appender-ref></appender-ref>
        <appender-ref></appender-ref>
        <appender-ref></appender-ref>

    </root>
</configuration>

Single case test:

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
@Data
public class LoggerTest {

    @Test
    public void test1(){
        String name="laomi";
        String password="123456";
        log.info("debug.....");
        log.info("info.....");
        log.error("error....");
        log.info("name:{}, password:{}",name,password);

    }
}

Add dependencies:

 <dependency>
            <groupid>org.projectlombok</groupid>
            <artifactid>lombok</artifactid>
            <scope>test</scope>
 </dependency>

How to handle logs in Springboot project

fileNamePattern This is the file path. I found the newly created folder in the same directory of this project

How to handle logs in Springboot project

The above is the detailed content of How to handle logs in Springboot project. 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