Home | 简体中文 | 繁体中文 | 杂文 | Search | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 作品与服务 | Email

5.14. Logging

日志的用法,首先开发中我们根据实际的需要打印不同级别的日志。

		
package cn.netkiller.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestController {

	private static final Logger log = LoggerFactory.getLogger(TestController.class);

	@RequestMapping("/test/log")
	@ResponseBody
	public String log() {
		String message = "Test";
		log.debug(message);
		log.info(message);
		log.warn(message);
		log.error(message);
		log.trace(message);
		return message;
	}
}
		
		

然后通过application.properties配置那些需要显示,那些不需要,以及显示的级别是什么。

5.14.1. 禁止控制台输出日志

禁止控制台日志输出,同时将日志写入日志文件.

src/main/resources/application.properties

			
logging.path=/tmp
logging.file=/tmp/spring.log
logging.level.root=WARN
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR
			
			

src/main/resources/logback.xml

			
$ cat src/main/resources/logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <include resource="org/springframework/boot/logging/logback/defaults.xml" />
  <include resource="org/springframework/boot/logging/logback/file-appender.xml" />

  <root level="INFO">
    <appender-ref ref="FILE" />
  </root>
</configuration>
			
			

使用 java -jar project-version-xxx.jar 启动后控制不会再输出日志

5.14.2. 日志输出级别

显示所有DEBUG信息

logging.level.root=DEBUG
			

仅仅显示 springframework 调试信息

logging.level.org.springframework.web=DEBUG
			

仅仅显示 cn.netkiller.web.TestController 调试信息

private static final Logger log = LoggerFactory.getLogger(TestController.class);

log.debug(message);

logging.level.cn.netkiller.web.TestController=DEBUG
			
comments powered by Disqus