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

How to handle logs in Springboot project

王林
Release: 2023-05-24 13:34:06
forward
1546 people have browsed it

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>
Copy after login

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);

    }
}
Copy after login

Add dependencies:

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

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template