【springBoot】日志分隔

springBoot项目日志分隔

logging: #默认输出到 console, file 10m滚动
  level: #trace debug info warn error 5个级别,支持不同包名设置不同级别
    root: info #整个项目默认
    com.zjasm.change: debug #打印sql
    com.alibaba.nacos.client.naming: warn
  file:
    name: ./logs/${spring.application.name}.log #配置根目录文件名称
  pattern:
    #console输出格式
    console: '%d{yyyy-MM-dd HH:mm:ss.SSS} %yellow(%-5level) [%thread] %boldMagenta(%-50.50logger) : %message%n'
    #file输出格式
    file: '%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{50} : %message%n'

jackson类部分字段返回

@JsonView
指定controller返回哪些字段

首先定义视图

public class View {

    //定义了一种视图,定义成interface就好了,不需要实现
    public interface Summary {}

    //定义了SummaryWithRecipients的视图,注意这个视图extends了Summary。所以这个视图包含了Summary视图。也就是,输出这个视图时候,同时输出Summary视图。
    public interface SummaryWithRecipients extends Summary {}

}

在实体类上加上@JsonView指定视图

public class Message {

    @JsonView(View.Summary.class)//这句话指定了id的视图类型是View.Summary.class。Controller的可以根据视图类来指定输出何种视图。View.Summary.class的定义在view文件夹里。
    private Long id;

    @JsonView(View.Summary.class)
    private String title;

    @JsonView(View.Summary.class)
    private User author;

    @JsonView(View.SummaryWithRecipients.class)//这句话指定了recipients的视图类型是View.SummaryWithRecipients.class
    private List<User> recipients;
}

然后controller层这样写

//定义了API的视图类型。也就是说,这个API在序列化的时候,仅仅对视图为View.Summary.class的数据进行序列化。
// 其中View.Summary.class的定义在view文件夹下面;数据的视图定义在DO文件夹下。
@JsonView(View.Summary.class)
@RequestMapping("/")
public List<Message> getAllMessages() {
    return messageService.getAll();
}

最后返回的message类只会返回有注解的字段