Spring Boot (教程十: 日志)

GitHub 地址:

https://github.com/asd821300801/Spring-Boot.git


使用SLF4J记录日志



在开发中我们不建议使用 System.out 因为大量的使用 System.out 会增加资源的消耗。
spring boot支持的日志框架有,logback,Log4j2,Log4j和Java Util Logging
spring Boot 提供了一套日志系统,logback是最优先的选择。


  • 在 src/main/resources 下面创建logback.xml (根据不同环境来定义不同的日志输出,那么取名为logback-spring.xml即可,官方优先推荐使用-spring.*的配置方式)文件。




创建配置文件


  • logback.xml


<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <logger name="org.springframework.web" level="INFO"/>

</configuration>

1



在代码中调用


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

private Logger logger =  LoggerFactory.getLogger(this.getClass());


完整代码


  • Slf4jController.java
  • 包所在:com.example.log


package com.example.log;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/log")
public class Slf4jController {
 //日志
    private Logger logger =  LoggerFactory.getLogger(this.getClass());
    @RequestMapping("/log")
    public String log(){
        logger.info("当前类:com.example.log.Slf4jController");
        return "日志测试。";
    }
}

访问:http://localhost:8080/log/log.action 之后打印输出日志

2


参考:http://www.iteye.com/topic/1144412

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值