Skip to content

Commit

Permalink
feat(ms-init): 集成log4j2 #174
Browse files Browse the repository at this point in the history
  • Loading branch information
zhonghuasheng committed Dec 27, 2020
1 parent 064e04a commit 7763c33
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 26 deletions.
4 changes: 3 additions & 1 deletion musicstore-springboot-vue/ms-platform/.idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions musicstore-springboot-vue/ms-platform/.idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<artifactId>ms-db-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.zhonghuasheng</groupId>
<artifactId>ms-log-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package com.zhonghuasheng.ms;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.ClassPathResource;

import java.util.Properties;

@SpringBootApplication(exclude = {DruidDataSourceAutoConfigure.class})
public class UserApplication {

public static void main(String[] args) {
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
yaml.setResources(new ClassPathResource("application.yml"));
Properties property = yaml.getObject();
System.setProperty("SERVICE_NAME", property.getProperty("spring.application.name"));
System.setProperty("FILE_PATH", property.getProperty("logging.file.path"));
SpringApplication.run(UserApplication.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import com.zhonghuasheng.ms.model.User;
import com.zhonghuasheng.ms.service.IUserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Log4j2
@RestController
@RequestMapping("/user")
public class UserController {
Expand All @@ -17,6 +19,7 @@ public class UserController {

@GetMapping("/{userId}")
public User getUserById(@PathVariable("userId") Long userId) {
log.info(userId);
return userService.getById(userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ spring:
enabled: true
time-between-log-stats-millis: 30000
mybatis-plus:
type-aliases-package: com.zhonghuasheng.ms.model
type-aliases-package: com.zhonghuasheng.ms.model
logging:
file:
path: /var/logs/ms-platform/
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea/
target
*.iml
../.idea/
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>ms-common</artifactId>
<groupId>com.zhonghuasheng</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>ms-log-spring-boot-starter</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.4.2</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--monitorInterval:Log4j2 自动检测修改配置文件和重新配置本身,设置间隔秒数-->
<configuration monitorInterval="5">
<!--日志级别以及优先级排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
<!--变量配置-->
<Properties>
<!-- 格式化输出:
%date表示日期,%thread表示线程名,
%-5level:级别从左显示5个字符宽度
%msg:日志消息,%n是换行符-->
<!-- %logger{36} 表示 Logger 名字最长36个字符 -->
<property name="LOG_PATTERN" value="[${sys:SERVICE_NAME}] [%date{yyyy-MM-dd HH:mm:ss.SSS}] [%thread] %-5level %logger{36} - %msg%n" />
<!-- 定义日志存储的路径,不要配置相对路径 -->
<property name="FILE_PATH" value="${sys:FILE_PATH}/${sys:SERVICE_NAME}" />
<property name="FILE_NAME" value="${sys:SERVICE_NAME}" />
</Properties>

<appenders>
<console name="Console" target="SYSTEM_OUT">
<!--输出日志的格式-->
<PatternLayout pattern="${LOG_PATTERN}"/>
<!--控制台只输出level及其以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
</console>

<!--文件会打印出所有信息-->
<!--<File name="Filelog" fileName="${FILE_PATH}/log4j2-all.log" append="true">
<PatternLayout pattern="${LOG_PATTERN}"/>
</File>-->
<RandomAccessFile name="Filelog" fileName="${FILE_PATH}/log4j2-all.log" append="true" immediateFlush="false">
<PatternLayout pattern="${LOG_PATTERN}"/>
</RandomAccessFile>

<!-- 这个会打印出所有的info及以下级别的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档-->
<RollingFile name="RollingFileInfo" fileName="${FILE_PATH}/info.log" filePattern="${FILE_PATH}/${FILE_NAME}-INFO-%d{yyyy-MM-dd-HH}_%i.log.gz">
<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="${LOG_PATTERN}"/>
<Policies>
<!--interval属性用来指定多久滚动一次,单位与RollingFile的filePattern一致,如果filePattern中配置的文件重命名规则是%d{yyyy-MM-dd HH-mm-ss}-%i,最小的时间粒度是ss,即秒钟。-->
<TimeBasedTriggeringPolicy interval="1"/>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
<!-- DefaultRolloverStrategy同一文件夹下15个文件开始覆盖-->
<DefaultRolloverStrategy max="15"/>
</RollingFile>

<!-- 这个会打印出所有的warn及以下级别的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档-->
<RollingFile name="RollingFileWarn" fileName="${FILE_PATH}/warn.log" filePattern="${FILE_PATH}/${FILE_NAME}-WARN-%d{yyyy-MM-dd HH}_%i.log.gz">
<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="${LOG_PATTERN}"/>
<Policies>
<!--interval属性用来指定多久滚动一次,默认是1 hour-->
<TimeBasedTriggeringPolicy interval="1"/>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
<!-- DefaultRolloverStrategy同一文件夹下15个文件开始覆盖-->
<DefaultRolloverStrategy max="15"/>
</RollingFile>

<!-- 这个会打印出所有的error及以下级别的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档-->
<RollingFile name="RollingFileError" fileName="${FILE_PATH}/error.log" filePattern="${FILE_PATH}/${FILE_NAME}-ERROR-%d{yyyy-MM-dd}_%i.log.gz">
<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="${LOG_PATTERN}"/>
<Policies>
<!--interval属性用来指定多久滚动一次,默认是1 hour-->
<TimeBasedTriggeringPolicy interval="1"/>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
<!-- DefaultRolloverStrategy同一文件夹下15个文件开始覆盖-->
<DefaultRolloverStrategy max="15"/>
</RollingFile>
</appenders>

<!--Logger节点用来单独指定日志的形式,比如要为指定包下的class指定不同的日志级别等。-->
<!--然后定义loggers,只有定义了logger并引入的appender,appender才会生效-->
<loggers>
<!--使用AsyncLogger的异步日志方式-->
<!--过滤掉spring和mybatis的一些无用的DEBUG信息-->
<AsyncLogger name="org.mybatis" level="info" includeLocation="true">
<AppenderRef ref="Console"/>
</AsyncLogger >
<!--监控系统信息-->
<!--若是additivity设为false,则 子Logger 只会在自己的appender里输出,而不会在 父Logger 的appender里输出。-->
<AsyncLogger name="org.springframework" level="info" includeLocation="true">
<AppenderRef ref="Console"/>
</AsyncLogger >

<AsyncRoot level="info">
<appender-ref ref="Console"/>
<appender-ref ref="Filelog"/>
<appender-ref ref="RollingFileInfo"/>
<appender-ref ref="RollingFileWarn"/>
<appender-ref ref="RollingFileError"/>
</AsyncRoot>
</loggers>
</configuration>
1 change: 1 addition & 0 deletions musicstore-springboot-vue/ms-platform/ms-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<packaging>pom</packaging>
<modules>
<module>ms-db-spring-boot-starter</module>
<module>ms-log-spring-boot-starter</module>
</modules>


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
spring:
profiles:
active: dev
active: dev
31 changes: 8 additions & 23 deletions musicstore-springboot-vue/ms-platform/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,31 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>2.4.0</version>
</dependency>
<!--数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

<dependencyManagement>
<!--<dependencies>
<dependencies>
<dependency>
<groupId>com.zhonghuasheng</groupId>
<artifactId>ms-config</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>-->
</dependencies>
</dependencyManagement>

<build>
Expand Down

0 comments on commit 7763c33

Please sign in to comment.