您的位置:首页 > 编程语言 > Java开发

spring-boot结合logback日志框架

2016-01-07 16:53 821 查看
Spring-Boot默认集成了backlog日志框架,无需在加载额外的jar包。

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

[code]#backlog setting
logging.config=logback.xml


logback.xml内容如下:

[code]<?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给出的配置方式如下:

[code]# 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.


有兴趣的同学可以去研究下!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: