在生产环境中,只打印error级别的错误,在测试环境中,可以调成debug
application.properties文件
## 默认使用logback logging.level.root=error ##warn,debug logging.level.org.springframework.web=error logging.file=d://log/my.log logging.pattern.console=%d{yyyy/MM/dd-HH:mm:ss} [%thread] %-5level %logger- %msg%n logging.pattern.file=%d{yyyy/MM/dd-HH:mm} [%thread] %-5level %logger- %msg%n
package com.wzy.ctl; import com.wzy.entity.User; import com.wzy.ser.UserSer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.List; @RestController @RequestMapping("user") public class UserCtl { @Resource private UserSer u; private final static Logger logger = LoggerFactory.getLogger(UserCtl.class); @RequestMapping(method=RequestMethod.GET,value = "getUser") public List<User> getUser(){ try{ float a = 1 / 0; } catch (Exception e){ logger.error("出现异常"+e.getMessage()); } return u.getUser(); } }
当系统出现异常后:
有一点值得注意:当日志一直打印,会出现linux系统内存不足的情况,可以做个定时任务,定期清理一下日志