Skip to content
This repository was archived by the owner on Feb 18, 2024. It is now read-only.

Commit 8503d02

Browse files
authored
Merge pull request #430 from sofastack/yuan_fix_logback_sample
Optimize logback sample
2 parents 9dcf665 + b2bbad2 commit 8503d02

File tree

15 files changed

+234
-161
lines changed

15 files changed

+234
-161
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
# 支持基座、模块使用采用独立日志配置打印logback日志
2+
原理详看[这里](https://github.com/sofastack/sofa-serverless/blob/master/docs/content/zh-cn/docs/contribution-guidelines/runtime/logj42.md)
3+
4+
# 实验内容
5+
## 实验应用
6+
### base
7+
base 为普通 springboot 改造成的基座,改造内容为在 pom 里增加如下依赖
8+
```xml
9+
<!-- sofa-serverless 相关依赖 -->
10+
<dependency>
11+
<groupId>com.alipay.sofa.serverless</groupId>
12+
<artifactId>sofa-serverless-base-starter</artifactId>
13+
</dependency>
14+
<dependency>
15+
<groupId>com.alipay.sofa</groupId>
16+
<artifactId>web-ark-plugin</artifactId>
17+
</dependency>
18+
<!-- spring boot 相关依赖 -->
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter-web</artifactId>
22+
</dependency>
23+
<dependency>
24+
<!-- spring boot 默认日志实现框架 logback -->
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-logging</artifactId>
27+
</dependency>
28+
```
29+
30+
注意⚠️:需要基座、模块采用独立日志配置特性,要求,sofa-ark-common 包版本不低于 2.2.6
31+
32+
基座自定义日志配置参考 logback-spring.xml,其中为控制台输出自定义pattern,日志前方添加 ${appname} 000,并且定义appender将日志输出到基座名目录下 ${logging.file.path}/${appname}/app-default.log
33+
```xml
34+
<?xml version="1.0" encoding="UTF-8" ?>
35+
<configuration>
36+
<springProperty scope="context" name="appname" source="spring.application.name"/>
37+
<springProperty scope="context" name="logging.file.path" source="logging.file.path"/>
38+
<property name="level" value="${logLevel:-info}"/>
39+
<property name="the3rdLevel" value="${the3rdLevel:-WARN}"/>
40+
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
41+
<encoder>
42+
<pattern>${appname} 000 %date %5level %6relative --- [%15thread] [%-40logger{40}] [%C:%L] : [%X{traceId:-0}] %msg%n</pattern>
43+
</encoder>
44+
</appender>
45+
<appender name="APP-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
46+
<append>true</append>
47+
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
48+
<level>${level}</level>
49+
</filter>
50+
<file>${logging.file.path}/${appname}/app-default.log</file>
51+
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
52+
<FileNamePattern>${logging.file.path}/${appname}/app-default.log.%d{yyyy-MM-dd}</FileNamePattern>
53+
<MaxHistory>30</MaxHistory>
54+
</rollingPolicy>
55+
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
56+
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
57+
<charset>UTF-8</charset>
58+
</encoder>
59+
</appender>
60+
<logger name="org.hibernate" level="${the3rdLevel}"/>
61+
<logger name="org.springframework" level="${the3rdLevel}"/>
62+
<logger name="com.alibaba" level="${the3rdLevel}"/>
63+
<logger name="org.apache" level="${the3rdLevel}"/>
64+
<root level="${level}">
65+
<appender-ref ref="CONSOLE"/>
66+
<appender-ref ref="APP-APPENDER"/>
67+
</root>
68+
</configuration>
69+
```
70+
注意⚠️:基座、模块日志隔离能力,依赖 logback 原生 context selector 特性,需要在jvm启动参数或系统属性中指定 contextSelector
71+
72+
方法一:添加jvm启动参数 -Dlogback.ContextSelector=com.alipay.sofa.ark.common.adapter.ArkLogbackContextSelector
73+
74+
方法二:添加系统属性,需要保证在首次获取 logger 前设置
75+
76+
```java
77+
@ImportResource({ "classpath*:META-INF/spring/service.xml"})
78+
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class})
79+
public class BaseApplication {
80+
81+
static {
82+
ArkConfigs.setEmbedEnable(true);
83+
// 建议加到jvm 参数中
84+
// 需要保证在 slf4j static bind 之前,(如,首次 getLogger、类加载 SpringApplication 之前)
85+
System.setProperty(ClassicConstants.LOGBACK_CONTEXT_SELECTOR,
86+
"com.alipay.sofa.ark.common.adapter.ArkLogbackContextSelector");
87+
}
88+
89+
private static Logger LOGGER = LoggerFactory.getLogger(BaseApplication.class);
90+
91+
public static void main(String[] args) {
92+
ConfigurableApplicationContext context = SpringApplication.run(
93+
BaseApplication.class, args);
94+
}
95+
}
96+
```
97+
98+
### biz
99+
biz 原来是普通 springboot,修改打包插件方式为 sofaArk biz 模块打包方式,打包为 ark biz jar 包,打包插件配置如下:
100+
```xml
101+
<plugin>
102+
<groupId>com.alipay.sofa</groupId>
103+
<artifactId>sofa-ark-maven-plugin</artifactId>
104+
<version>${sofa.ark.version}</version>
105+
<executions>
106+
<execution>
107+
<id>default-cli</id>
108+
<goals>
109+
<goal>repackage</goal>
110+
</goals>
111+
</execution>
112+
</executions>
113+
<configuration>
114+
<skipArkExecutable>true</skipArkExecutable>
115+
<outputDirectory>./target</outputDirectory>
116+
<bizName>biz1-logback</bizName>
117+
<webContextPath>biz1</webContextPath>
118+
<declaredMode>true</declaredMode>
119+
</configuration>
120+
</plugin>
121+
```
122+
注意这里将不同 biz 的 web context path 修改成不同的值,以此才能成功在一个 tomcat host 里安装多个 web 应用。
123+
124+
模块自定义日志配置见模块项目资源目录中的 logback-spring.xml,其中为控制台输出自定义pattern,日志前方添加 ${appname} 111,并且定义appender将日志输出到模块名目录下 ${logging.file.path}/${appname}/app-default.log
125+
126+
```xml
127+
<?xml version="1.0" encoding="UTF-8" ?>
128+
<configuration>
129+
<springProperty scope="context" name="appname" source="spring.application.name"/>
130+
<springProperty scope="context" name="logging.file.path" source="logging.file.path"/>
131+
<property name="level" value="${logLevel:-info}"/>
132+
<!-- <property name="appid" value="${appname}"/>-->
133+
<property name="the3rdLevel" value="${the3rdLevel:-WARN}"/>
134+
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
135+
<encoder>
136+
<pattern>${appname} 111 %date %5level %6relative --- [%15thread] [%-40logger{40}] [%C:%L] : [%X{traceId:-0}] %msg%n</pattern>
137+
</encoder>
138+
</appender>
139+
<appender name="APP-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
140+
<append>true</append>
141+
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
142+
<level>${level}</level>
143+
</filter>
144+
<file>${logging.file.path}/${appname}/app-default.log</file>
145+
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
146+
<FileNamePattern>${logging.file.path}/${appname}/app-default.log.%d{yyyy-MM-dd}</FileNamePattern>
147+
<MaxHistory>30</MaxHistory>
148+
</rollingPolicy>
149+
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
150+
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
151+
<charset>UTF-8</charset>
152+
</encoder>
153+
</appender>
154+
<logger name="org.hibernate" level="${the3rdLevel}"/>
155+
<logger name="org.springframework" level="${the3rdLevel}"/>
156+
<logger name="com.alibaba" level="${the3rdLevel}"/>
157+
<logger name="org.apache" level="${the3rdLevel}"/>
158+
<root level="${level}">
159+
<appender-ref ref="CONSOLE"/>
160+
<appender-ref ref="APP-APPENDER"/>
161+
</root>
162+
</configuration>
163+
```
164+
165+
## 实验任务
166+
### 执行 mvn clean package -DskipTests
167+
可在各 bundle 的 target 目录里查看到打包生成的 ark-biz jar 包
168+
### 启动基座应用 base,确保基座启动成功
169+
### 执行 curl 命令安装 biz
170+
```shell
171+
curl --location --request POST 'localhost:1238/installBiz' \
172+
--header 'Content-Type: application/json' \
173+
--data '{
174+
"bizName": "biz1-logback",
175+
"bizVersion": "0.0.1-SNAPSHOT",
176+
// local path should start with file://, alse support remote url which can be downloaded
177+
"bizUrl": "file:///xxxx/samples/springboot-samples/logging/logback/biz1/target/biz1-logback-0.0.1-SNAPSHOT-ark-biz.jar"
178+
}'
179+
```
180+
181+
### 验证
182+
183+
1. 先查看基座启动日志,可以见到日志中有"base 000" 字样,满足我们日志配置中的pattern,同时在 logging.file.path=./logging/logback/logs/ 目录下存在基座日志文件
184+
![img.png](img.png)
185+
2. 再启动模块后,查看模块启动日志,可以见到日志中有"biz1 111" 字样,满足我们日志配置中的pattern,同时在 logging.file.path=./logging/logback/logs/ 目录下存在模块日志文件
186+
![img_1.png](img_1.png)
187+
3. 发起请求验证模块web服务
188+
189+
```shell
190+
curl http://localhost:8080/biz2
191+
```
192+
返回 `hello to /biz1 deploy`,同时查看控制台日志输出,满足我们日志配置中的pattern
193+
```log
194+
biz1 111 2023-12-27 20:05:55,543 INFO 25790 --- [http-nio-8080-exec-1] [c.a.sofa.web.biz1.rest.SampleController ] [com.alipay.sofa.web.biz1.rest.SampleController:21] : [0] /biz1 web test: into sample controller
195+
```
196+
197+
## 注意事项
198+
这里主要使用简单应用做验证,如果复杂应用,需要注意模块做好瘦身,基座有的依赖,模块尽可能设置成 provided,尽可能使用基座的依赖。

samples/springboot-samples/logging/logback/base/pom.xml

-9
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
<groupId>org.springframework.boot</groupId>
2020
<artifactId>spring-boot-starter-web</artifactId>
2121
</dependency>
22-
<!-- 一定要排除logback,因为基座cp中的ark-all包含了该jar,
23-
会导致该jar中的类被app cl和container cl都能加载到,会出现class not find-->
2422
<dependency>
2523
<groupId>org.springframework.boot</groupId>
2624
<artifactId>spring-boot-starter-logging</artifactId>
@@ -36,17 +34,10 @@
3634
<groupId>com.alipay.sofa.serverless</groupId>
3735
<artifactId>sofa-serverless-base-starter</artifactId>
3836
</dependency>
39-
40-
4137
<dependency>
4238
<groupId>com.alipay.sofa</groupId>
4339
<artifactId>web-ark-plugin</artifactId>
4440
</dependency>
45-
<dependency>
46-
<groupId>com.lmax</groupId>
47-
<artifactId>disruptor</artifactId>
48-
<version>${disruptor.version}</version>
49-
</dependency>
5041
</dependencies>
5142

5243
<build>
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
spring.application.name=base
2+
logging.file.path=./logging/logback/logs/
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,35 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
22
<configuration>
33
<springProperty scope="context" name="appname" source="spring.application.name"/>
4+
<springProperty scope="context" name="logging.file.path" source="logging.file.path"/>
45
<property name="level" value="${logLevel:-info}"/>
5-
<!-- <property name="appid" value="${appname}"/>-->
66
<property name="the3rdLevel" value="${the3rdLevel:-WARN}"/>
77
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
88
<encoder>
99
<pattern>${appname} 000 %date %5level %6relative --- [%15thread] [%-40logger{40}] [%C:%L] : [%X{traceId:-0}] %msg%n</pattern>
1010
</encoder>
1111
</appender>
12+
<appender name="APP-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
13+
<append>true</append>
14+
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
15+
<level>${level}</level>
16+
</filter>
17+
<file>${logging.file.path}/${appname}/app-default.log</file>
18+
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
19+
<FileNamePattern>${logging.file.path}/${appname}/app-default.log.%d{yyyy-MM-dd}</FileNamePattern>
20+
<MaxHistory>30</MaxHistory>
21+
</rollingPolicy>
22+
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
23+
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
24+
<charset>UTF-8</charset>
25+
</encoder>
26+
</appender>
1227
<logger name="org.hibernate" level="${the3rdLevel}"/>
1328
<logger name="org.springframework" level="${the3rdLevel}"/>
1429
<logger name="com.alibaba" level="${the3rdLevel}"/>
1530
<logger name="org.apache" level="${the3rdLevel}"/>
1631
<root level="${level}">
1732
<appender-ref ref="CONSOLE"/>
33+
<appender-ref ref="APP-APPENDER"/>
1834
</root>
1935
</configuration>
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
spring.application.name=biz1
2+
logging.file.path=./logging/logback/logs/

samples/springboot-samples/logging/logback/biz1/src/main/resources/logback-spring.xml

+17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
22
<configuration>
33
<springProperty scope="context" name="appname" source="spring.application.name"/>
4+
<springProperty scope="context" name="logging.file.path" source="logging.file.path"/>
45
<property name="level" value="${logLevel:-info}"/>
56
<!-- <property name="appid" value="${appname}"/>-->
67
<property name="the3rdLevel" value="${the3rdLevel:-WARN}"/>
@@ -9,11 +10,27 @@
910
<pattern>${appname} 111 %date %5level %6relative --- [%15thread] [%-40logger{40}] [%C:%L] : [%X{traceId:-0}] %msg%n</pattern>
1011
</encoder>
1112
</appender>
13+
<appender name="APP-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
14+
<append>true</append>
15+
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
16+
<level>${level}</level>
17+
</filter>
18+
<file>${logging.file.path}/${appname}/app-default.log</file>
19+
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
20+
<FileNamePattern>${logging.file.path}/${appname}/app-default.log.%d{yyyy-MM-dd}</FileNamePattern>
21+
<MaxHistory>30</MaxHistory>
22+
</rollingPolicy>
23+
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
24+
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
25+
<charset>UTF-8</charset>
26+
</encoder>
27+
</appender>
1228
<logger name="org.hibernate" level="${the3rdLevel}"/>
1329
<logger name="org.springframework" level="${the3rdLevel}"/>
1430
<logger name="com.alibaba" level="${the3rdLevel}"/>
1531
<logger name="org.apache" level="${the3rdLevel}"/>
1632
<root level="${level}">
1733
<appender-ref ref="CONSOLE"/>
34+
<appender-ref ref="APP-APPENDER"/>
1835
</root>
1936
</configuration>

samples/springboot-samples/logging/logback/biz2/pom.xml

-72
This file was deleted.

0 commit comments

Comments
 (0)