进行项目开发时,日志的作用是绝对不可忽略的。记录好日志,是排除bug的良药。
在日志输出时,建议不要使用System.out.println() 输出日志信息,此种方法会增加资源的消耗且灵活性比较弱,不方便进行日志等级的控制,有时会干扰正常的日志输出。
- 为什么采用注解@Slf4j了
因为平常记录日志的时候,常常要写如下代码,才能进行日志输出
private final Logger logger = LoggerFactory.getLogger(Class.class);
本着能不多写就不多写得原则下,推荐使用@Slf4j的方式
@Slf4j @RestController @RequestMapping("/news") public class NewsController { @GetMapping("/queryByName") public String queryByName(@RequestParam(value = "name" ,required = true) String name, @RequestParam(value = "num",required = false)Integer num) { log.info(name); return name+" :"+ num; }
日志输出的格式为:2019-04-17T15:41:50.149+08:00 INFO news [http-nio-8888-exec-2] [com.haoeasy.news.test.NewsController:28] hk
- @Slf4j的安装
- 引入注解
<!--可以引入日志 @Slf4j注解-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
2.IDEA安装lombok插件
File → settings → Plugins ,搜索lombox,如下图
安装完成后,重启即可。