Skip to content

Commit

Permalink
replace logback to log4j2
Browse files Browse the repository at this point in the history
  • Loading branch information
ponfee committed Feb 23, 2024
1 parent 63f2718 commit e1d7d9b
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 104 deletions.
20 changes: 20 additions & 0 deletions disjob-admin/ruoyi-admin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- apache log4j2 for log implementation -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</dependency>

<!-- SpringBoot集成thymeleaf模板 -->
Expand Down
5 changes: 1 addition & 4 deletions disjob-admin/ruoyi-admin/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ server:
min-spare: 100

# 日志配置
logging:
level:
com.ruoyi: debug
org.springframework: warn
logging.config: classpath:log4j2.xml

# 用户配置
user:
Expand Down
59 changes: 59 additions & 0 deletions disjob-admin/ruoyi-admin/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--日志级别以及优先级排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
<!-- Log4j 2.x 配置文件:每30秒自动检查和应用配置文件的更新;log4j-core-2.11.0.jar!/Log4j-config.xsd -->
<Configuration status="WARN" monitorInterval="1800" strict="true" name="disjob-log">

<properties>
<!-- 系统属性变量:System.getProperty("log.home"),${sys:variable_name:-default_value} -->
<!-- pattern: https://logging.apache.org/log4j/2.x/manual/layouts.html -->
<property name="LOG_HOME">${sys:log.home:-../logs}</property>
<property name="APP_NAME">${sys:app.name:-disjob-admin}</property>
<property name="PATTERN_CONSOLE">%d{yyyy-MM-dd HH:mm:ss.SSS} | %highlight{%-5p} | %t | %c{1} >>>> %msg%n</property>
<property name="PATTERN_FILE">%d{yyyy-MM-dd HH:mm:ss.SSS} | %-5p | %pid | %t | %c{1} : %msg%n</property>
</properties>

<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="${PATTERN_CONSOLE}" />
</Console>

<!-- 输出到文件,按天或者超过100MB分割 -->
<RollingFile name="RollingFile" fileName="${LOG_HOME}/${APP_NAME}.log" filePattern="${LOG_HOME}/$${date:yyyy-MM}/${APP_NAME}-%d{yyyy-MM-dd}-%i.log.gz">
<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY" />
<PatternLayout pattern="${PATTERN_FILE}" />
<Policies>
<!-- 启动时就重新滚动日志 -->
<!-- <OnStartupTriggeringPolicy /> -->

<!-- 如果启用此配置,则日志会按文件名生成新压缩文件,即如果filePattern配置的日期格式为 %d{yyyy-MM-dd HH},
则每小时生成一个压缩文件,如果filePattern配置的日期格式为 %d{yyyy-MM-dd} ,则天生成一个压缩文件,
modulate=true用来调整时间,interval属性用来指定多久滚动一次,默认是1 hour,
比如现在是早上3am,interval=4,那么第一次滚动是在4am,接着是8am,而不是7am
-->
<!-- <TimeBasedTriggeringPolicy interval="1" modulate="true" /> -->
<TimeBasedTriggeringPolicy />

<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
<!-- DefaultRolloverStrategy属性如不设置,则默认为最多同一文件夹下7个文件 -->
<DefaultRolloverStrategy max="100" />
</RollingFile>

<Async name="AsyncRollingFile">
<AppenderRef ref="RollingFile" />
<LinkedTransferQueue />
</Async>
</Appenders>

<Loggers>
<Logger name="com.ruoyi" level="INFO" />
<Logger name="cn.ponfee.disjob" level="INFO" />

<Root level="WARN">
<AppenderRef ref="Console" />
<AppenderRef ref="AsyncRollingFile" />
</Root>
</Loggers>

</Configuration>
95 changes: 0 additions & 95 deletions disjob-admin/ruoyi-admin/src/main/resources/logback.xml

This file was deleted.

24 changes: 19 additions & 5 deletions disjob-samples/conf-common/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
<properties>
<!-- 系统属性变量:System.getProperty("log.home"),${sys:variable_name:-default_value} -->
<!-- pattern: https://logging.apache.org/log4j/2.x/manual/layouts.html -->
<!-- Replace ${sys:PID} with %pid in log4j2 configuration: https://github.com/spring-projects/spring-boot/issues/15426 -->
<!-- https://github.com/spring-projects/spring-boot/blob/main/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2.xml -->
<property name="LOG_HOME">${sys:log.home:-../logs}</property>
<property name="APP_NAME">${sys:app.name:-disjob-samples}</property>
<property name="PATTERN_CONSOLE">%d{yyyy-MM-dd HH:mm:ss.SSS} | %highlight{%-5p} | %t | %c{1} >>>> %msg%n</property>
<property name="PATTERN_FILE">%d{yyyy-MM-dd HH:mm:ss.SSS} | %-5p | ${sys:PID} | %t | %c{1} : %msg%n</property>
<property name="PATTERN_FILE">%d{yyyy-MM-dd HH:mm:ss.SSS} | %-5p | %pid | %t | %c{1} : %msg%n</property>
</properties>

<Appenders>
Expand Down Expand Up @@ -46,19 +48,31 @@
</Async>
</Appenders>

<!--
https://blog.csdn.net/HongZeng_CSDN/article/details/130094219
Root Logger:提供默认的Level和Appender设置,当一个普通Logger没有显式地设置Level或Appender时,则会继承Root Logger的设置
additivity:指定是否同时输出日志到父类的appender,缺省为true
Logger查找:沿着类路径(包路径)逐级向上查找匹配的Logger
Root Logger
├── com.example (package)
│ ├── com.example.MyClass1 (class)
│ ├── com.example.MyClass2 (class)
│ └── ...
├── com.example.subpackage (package)
│ ├── com.example.subpackage.MyClass3 (class)
│ └── ...
└── ...
-->
<Loggers>
<!-- additivity指定是否同时输出日志到父类的appender,缺省为true -->
<Logger name="cn.ponfee.disjob" level="INFO" />
<Logger name="com.zaxxer.hikari" level="INFO" />

<!--
<Logger name="cn.ponfee.disjob.registry" level="DEBUG" />
<Logger name="cn.ponfee.disjob.supervisor" level="DEBUG" />
<Logger name="cn.ponfee.disjob.supervisor.dao" level="DEBUG" />
<Logger name="cn.ponfee.disjob.worker" level="DEBUG" />
-->

<!-- 当根据日志名字获取不到指定的日志器时就使用Root作为默认的日志器 -->
<Root level="WARN">
<AppenderRef ref="Console" />
<AppenderRef ref="AsyncRollingFile" />
Expand Down

0 comments on commit e1d7d9b

Please sign in to comment.