<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
spring:
  thymeleaf:
    mode: HTML
  profiles:
    active: dev
开发环境
server:
  port: 8080
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/blog?UseUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
    username: root
    password: admin
  jpa:
    hibernate:
      ddl-auto: update #实体类发生变化时,数据库自动更新
    show-sql: true #控制台显示sql
    database-platform: org.hibernate.dialect.MySQLDialect #设置方言,不设置报错‘hibernate.dialect‘ not set
logging:
  level:
    root: info
    com.example.demo: debug
  file:
    name: log/blog-dev.log
注意:
logging.file新版本已被废弃;logging.file.name和logging.file.path同时配置,只会生效logging.file.name;
建议使用logging.file.name
可以在资源目录下配置logback-spring.xml(官方推荐)进行更详细的日志输出,文件参考 https://www.cnblogs.com/kwdlh/p/13900044.html
生产环境
server:
  port: 8082
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/blog?UseUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
    username: root
    password: admin
  jpa:
    hibernate:
      ddl-auto: none
    show-sql: true
    database-platform: org.hibernate.dialect.MySQLDialect
logging:
  level:
    root: warn
    com.example.demo: info
  file:
    name: log/blog-dev.log
@ControllerAdvice
public class ContExceptionHandler {
    private final Logger logger= LoggerFactory.getLogger(this.getClass());
    @ExceptionHandler(Exception.class)
    public ModelAndView handler(HttpServletRequest request,Exception e){
        logger.error("Request Url: {}, Exception: {}",request.getRequestURL(),e.getMessage());
        //某些异常不用自己处理,交给springboot处理
        if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class)!=null){
            throw e;
        }
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("url",request.getRequestURL())
                .addObject("exception",e)
                .setViewName("/error/error-info");
        return modelAndView;
    }
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>error-info</title>
</head>
<body>
<h1>查看网页源代码查看错误信息</h1>
<div>
    <div th:utext="‘<!--‘" th:remove="tag">
    </div>
    <div th:utext="‘Failed Request URL : ‘+ ${url}" th:remove="tag"></div>
    <div th:utext="‘Exception message : ‘ + ${exception.message}"
         th:remove="tag"></div>
    <ul th:remove="tag">
        <li th:each="stackTrace : ${exception.stackTrace}" th:remove="tag">
            <span th:utext="${stackTrace}" th:remove="tag"></span>
        </li>
    </ul>
    <div th:utext="‘-->‘" th:remove="tag"></div>
</div>
</body>
</html>
自定义异常
@ResponseStatus(HttpStatus.NOT_FOUND)
public class BlogNotFoundException extends RuntimeException{
    public BlogNotFoundException() {
        super();
    }
    public BlogNotFoundException(String message) {
        super(message);
    }
    public BlogNotFoundException(String message, Throwable cause) {
        super(message, cause);
    }
}
原文:https://www.cnblogs.com/kwdlh/p/13900035.html