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
    
  • 相关阅读:
    在java中使用ffmpeg将amr格式的语音转为mp3格式
    keras实现不同形态的模型
    TF版本的Word2Vec和余弦相似度的计算
    TensorFlow中的两种conv2d方法和kernel_initializer
    CNN中的padding
    记一次sqoop同步到mysql
    理解HDFS
    TensorFlow中的优化算法
    使用TensorFlow实现分类
    使用TensorFlow实现回归预测
  • 原文地址:https://www.cnblogs.com/imzhizi/p/spring-boot-logging-easy-way.html
Copyright © 2011-2022 走看看