Springboot中的日志管理
Slf4j+logback来实现
具体实现
重点:
- @Slf4j
private static Logger log = LoggerFactory.getLogger(DemoController.class);
3. 四个级别:debug—info—warning—error,从低到高
4. 注意:报错只能报比设置等级高的错误
设置:
可以在application.properties中设置等级:
# log日志
logging.level.root = info
logging.level.com.example.demo.controller = debug
@Slf4j
@RestController
//@RequestMapping("demo")//加一层项目路径
public class DemoController {
private static Logger log = LoggerFactory.getLogger(DemoController.class);
@RequestMapping("hello world")
@ResponseBody//将函数设置为响应体,而不是实现页面跳转
public String helloWorld(){
System.out.println("hello world");
return "hello world";
}
@RequestMapping("index")
//@ResponseBody//将函数设置为响应体,而不是实现页面跳转
public String index(){
System.out.println("hello index.jsp");
return "index";
}
@RequestMapping("hello")
public String helloworld() {
System.out.println("hello world");
log.debug("hello==={}++++{}", "world", "");
return "hello world";
}
@RequestMapping("/jsonObj")
public Map<String, Object> jsonObj() {
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("msg", "this is response json data");
log.info("json_msg={}", resultMap.toString());
return resultMap;
}
}