日志
1. log4j2
1.1 pom依赖
<dependencies> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.6</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.8.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.8.2</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-jcl</artifactId> <version>2.8.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.10.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.yaml/snakeyaml --> <dependency> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> <version>1.25</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-yaml</artifactId> <version>2.9.8</version> </dependency> </dependencies>
1.2 yml-config
# https://blog.csdn.net/u010598111/article/details/80556437 # 共有8个级别,按照从低到高为:ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF。 Configuration: status: info monitorInterval: 30 Properties: # 定义全局变量 Property: # 缺省配置(用于开发环境)。其他环境需要在VM参数中指定,如下: - name: log.level.console value: info - name: log.path value: log - name: project.name value: xinyar-erp-auto - name: log.pattern value: "%d{yyyy-MM-dd HH:mm:ss.SSS} -%5p ${PID:-} [%15.15t] %C{1.} : %m%n" Appenders: Console: #输出到控制台 name: CONSOLE target: SYSTEM_OUT PatternLayout: pattern: ${log.pattern} # 启动日志 RollingFile: - name: ROLLING_FILE fileName: D:/${log.path}/${project.name}.log filePattern: "D:/${log.path}/${project.name}-%d{yyyy-MM-dd}-%i.log" PatternLayout: pattern: ${log.pattern} Filters: # 一定要先去除不接受的日志级别,然后获取需要接受的日志级别 ThresholdFilter: - level: error onMatch: DENY onMismatch: NEUTRAL - level: info onMatch: ACCEPT onMismatch: DENY Policies: TimeBasedTriggeringPolicy: # 按天分类 modulate: true interval: 1 SizeBasedTriggeringPolicy: # 设置文件最大大小 size: 2MB DefaultRolloverStrategy: # 文件最多100个 max: 100 # 错误日志 - name: EXCEPTION_ROLLING_FILE ignoreExceptions: false fileName: D:/${log.path}/exception/${project.name}_exception.log filePattern: "D:/${log.path}/exception/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz" ThresholdFilter: level: error onMatch: ACCEPT onMismatch: DENY PatternLayout: pattern: ${log.pattern} Policies: TimeBasedTriggeringPolicy: # 按天分类 modulate: true interval: 1 DefaultRolloverStrategy: # 文件最多100个 max: 1 Loggers: Root: level: info AppenderRef: - ref: CONSOLE - ref: ROLLING_FILE - ref: EXCEPTION_ROLLING_FILE # 可以控制哪个包下是否可以输出(意义不大) # Logger: # - name: platform # level: info # additivity: true # AppenderRef: # - ref: CONSOLE # - ref: PLATFORM_ROLLING_FILE # - name: com.aa # level: error # additivity: true # AppenderRef: # - ref: EXCEPTION_ROLLING_FILE