spring-boot结合logback日志框架

时间:2025-04-28 07:15:02

Spring-Boot默认集成了backlog日志框架,无需在加载额外的jar包。

只需要在文件中添加配置即可(非默认方式):

#backlog setting
logging.config=logback.xml  

内容如下:

<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
    <!-- 控制台设置 -->
    <appender name="STDOUT" class="">
        <!-- encoder 默认配置为PatternLayoutEncoder -->
        <encoder>
            <pattern>%d{HH:mm:} [%thread] %-5level %logger{36} - %msg%n
            </pattern>
        </encoder>
    </appender>


    <!-- * 通配符 设置log打印级别 对所有类有效TRACE, DEBUG, INFO, WARN, ERROR, ALL 和 OFF-->
    <root level="DEBUG">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>  

当前仅当只是配置控制台输出方式,需要配置File,已经File策略的请观看博主的该文章介绍。

测试类:

@Controller
@RequestMapping(“test”)
public class TestController {
public static Logger logger = ();
@RequestMapping(“”)
public void test() {
(“test”);
(“调用了Test”);
}
}

关于Spring-Boot给出的配置方式如下:

# LOGGING
= # Location of the logging configuration file. For instance `classpath:` for Logback

-conversion-word=%wEx # Conversion word used when logging exceptions.

logging.file= # Log file name. For instance ``

.*= # Log levels severity mapping. For instance `=DEBUG`

= # Location of the log file. For instance `/var/log`

= # Appender pattern for output to the console. Only supported with the default logback setup.

.file= # Appender pattern for output to the file. Only supported with the default logback setup.

= # Appender pattern for log level (default %5p). Only supported with the default logback setup.

logging.register-shutdown-hook=false # Register a shutdown hook for the logging system when it is initialized.

有兴趣的同学可以去研究下!

PS:最近做一个项目,发现打包成war部署到服务器上面的时候整合的logback日志打印不出来。原因指定的路径文件一直识别出来讲改名为放在resources下面即可。