spring-boot结合logback日志框架

本文介绍了Spring-Boot如何与logback集成,无需额外加载jar包,只需在application.properties中配置。示例展示了logback.xml的简单配置,并提供了一个测试类的示例代码。在实际项目中,当将应用打包为war并部署到服务器时,可能出现logback日志无法打印的问题,解决办法是将logback.xml重命名为logback-spring.xml并置于resources目录下。

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

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

#backlog setting
logging.config=logback.xml  

logback.xml内容如下:

<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
    <!-- 控制台设置 -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoder 默认配置为PatternLayoutEncoder -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%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 = LoggerFactory.getLogger(TestController.class);
@RequestMapping(“”)
public void test() {
logger.debug(“test”);
System.out.println(“调用了Test”);
}
}

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

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

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

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

logging.level.*= # Log levels severity mapping. For instance `logging.level.org.springframework=DEBUG`

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

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

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

logging.pattern.level= # 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日志打印不出来。原因logging.config指定的路径文件一直识别出来讲backlog.xml改名为logback-spring.xml放在resources下面即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值