zoukankan      html  css  js  c++  java
  • Spring Boot 笔记

    POM.xml

    使用 slf4(接口)log4j2(实现) 做日志框架. 首先干掉自带的 starter-logging, 那是 logback, 然后使用 lombok 插件来简化工作.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <artifactId>spring-boot-starter-logging</artifactId>
                <groupId>org.springframework.boot</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    

    用法

    都差不多的用法, 不过可以用 {} 做字符串拼接, 比较舒服.

    然后要加注解 Log4j2, 这是 lombok 提供的.

    @RestController
    @Log4j2
    public class Controller {
        @GetMapping("/users/{id}")
        public Optional<User> getOne(@PathVariable Long id) {
            log.trace("require user {} info", id);
            log.debug("require user {} info", id);
            log.info("require user {} info", id);
            log.warn("require user {} info", id);        
            return userService.getUserById(id);
        }
    }
    

    日志级别调整
    application.yml中可以配置级别, 支持多项配置, root 意味着整个项目的日志级别, 某一个包则定义特定包的日志级别, 很容易理解.

    logging:
      level:
        com.example.repo: warn
    #   com.example.repo.service: warn
    #   root: warn
    
  • 相关阅读:
    ASP.NET中常用的优化性能的方法
    把WinDbg集成到Visual Studio中
    提高ASP.net的性能
    Msn Library
    [转帖]OutOfMemoryException问题的处理
    一完美的关于请求的目录不存在而需要url重写的解决方案!
    在 ASP.NET 中执行 URL 重写
    转 内存不断上升处理方法
    IIS 6 通配符应用映射和HttpHandler配置
    Java开源
  • 原文地址:https://www.cnblogs.com/imzhizi/p/spring-boot-logging-easy-way.html
Copyright © 2011-2022 走看看