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下面即可。