spring-boot configuration slf4j log

SLF4J, the Simple Logging Facade for Java, is not a specific logging solution, it only serves a variety of logging systems. According to the official statement, SLF4J is a simple facade for logging system, allowing end users to use the logging system they want when deploying their applications.

slf4j has been integrated inside spring-boot, so we only need to do a simple configuration for slf4j:

First, we need a file for log-related configuration:

logback.xml:

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

    <!-- 格式化输出:%date表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度 %msg:日志消息,%n是换行符-->
    <property name="LOG_PATTERN" value="%date{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" />
    <!-- 定义日志存储的路径,不要配置相对路径 -->
    <property name="FILE_PATH" value="E:/logs/demo.%d{yyyy-MM-dd}.%i.log" />

    <!-- 控制台输出日志 -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <!-- 按照上面配置的LOG_PATTERN来打印日志 -->
            <pattern>${LOG_PATTERN}</pattern>
        </encoder>
    </appender>

    <!--每天生成一个日志文件,保存15天的日志文件。rollingFile是用来切分文件的 -->
    <appender name="FILE"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${FILE_PATH}</fileNamePattern>
            <!-- keep 15 days' worth of history -->
            <maxHistory>15</maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <!-- 日志文件的最大大小 -->
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>

        <encoder>
            <pattern>${LOG_PATTERN}</pattern>
        </encoder>
    </appender>
    <!-- project default level -->
    <logger name="src" level="INFO" />

    <!-- 日志输出级别 常用的日志级别按照从高到低依次为:ERROR、WARN、INFO、DEBUG。 -->
    <root level="INFO">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE" />
    </root>
</configuration>

Then specify the location of the configuration file in the application:

## 日志配置
logging:
  config: src/main/resources/logback.xml
  level:
    com.github.springbootmiaosha.dao: trace

logging.level is used to specify the output level of logs in a specific Mapper. The above configuration means that all Mapper log output levels under the com.ruifeng.demo.dao package are Trace, and the SQL for operating the database will be printed out. Set it to trace during development to facilitate the location of problems. In a production environment, set this log level to the error level.

After the configuration is complete, we can use it wherever we want:

package com.github.springbootmiaosha;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author lizhangyu
 */
@SpringBootApplication
public class SpringbootMiaoshaApplication {

    private static final Logger logger = LoggerFactory.getLogger(SpringbootMiaoshaApplication.class);

    public static void main(String[] args) {
        logger.info("SpringBoot开始加载");
        SpringApplication.run(SpringbootMiaoshaApplication.class, args);
        logger.info("SpringBoot加载完毕");
    }

}

Guess you like

Origin blog.csdn.net/qq_37469055/article/details/92849354