Skip to content

Commit 1e120eb

Browse files
QilongZhangkhotyn
authored andcommitted
Add spring boot starter (rabbitmq#32)
* add log-sofa-boot-starter * fix rabbitmq#30 rabbitmq#29 rabbitmq#28 rabbitmq#26 rabbitmq#20
1 parent 33a390c commit 1e120eb

File tree

100 files changed

+2598
-532
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+2598
-532
lines changed

README.md

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,63 @@ public enum AdapterLevel {
199199
}
200200
```
201201

202-
## 四、LICENSE
202+
## 四、配置说明
203+
### sofa.middleware.log.disable
204+
默认配置为 false, 禁止所有 logback/log4j/log4j2 日志打印. 日志默认打印在 ROOT Appender.
205+
206+
**注: 该配置项只支持通过 -D 作为 VM 参数传入,例如 -Dsofa.middleware.log.disable=true**
207+
208+
### logback.middleware.log.disable
209+
默认配置为 false, 禁止使用 logback 日志实现
210+
**注: 该配置项只支持通过 -D 作为 VM 参数传入,例如 -Dlogback.middleware.log.disable=true**
211+
212+
### log4j2.middleware.log.disable
213+
默认配置为 false, 禁止使用 log4j2 日志实现
214+
**注: 该配置项只支持通过 -D 作为 VM 参数传入,例如 -Dlog4j2.middleware.log.disable=true**
215+
216+
### log4j.middleware.log.disable
217+
默认配置为 false, 禁止使用 log4j 日志实现
218+
**注: 该配置项只支持通过 -D 作为 VM 参数传入,例如 -Dlog4j.middleware.log.disable=true**
219+
220+
### 自身日志级别配置
221+
sofa-common-tools 在初始化绑定日志实现类时,会使用 `System.out` 打印详情信息,例如:
222+
```text
223+
Sofa-Middleware-Log:DEBUG Actual binding is of type [ test.space Logback ]
224+
```
225+
可以通过 `sofa.middleware.log.internal.level=debug` 设置改日志类型打印级别,默认为 INFO.
226+
**注: 该配置项只支持通过 -D 作为 VM 参数传入,例如 -Dsofa.middleware.log.internal.level=debug**
227+
228+
### 配置日志级别
229+
+ 全局配置:支持使用 `*` 通配符配置,例如 `logging.level.com.* = info`, 则 `logging.levle.com.alipay.sofa.rpc` 日志空间的级别为 `info`
230+
+ 单个日志空间配置:`logging.level.${spaceid}=info`,例如 `logging.levle.com.alipay.sofa.rpc=info`。则 RPC 日志空间级别为 `info`
231+
**支持通过 -D 作为 VM 参数传入以及在 Spring Boot 的 application.yml 配置文件传入(依赖 log-sofa-boot-starter)**
232+
233+
### 自定义日志配置文件
234+
每个人日志空间中,logback/log4j2/log4j 都会提供一份默认的日志配置文件,可以使用 `logging.config.{space id}=log-conf-xx.xml` 覆盖默认配置文件。
235+
**支持通过 -D 作为 VM 参数传入以及在 Spring Boot 的 application.yml 配置文件传入(依赖 log-sofa-boot-starter)**
236+
237+
### 日志打印控制台
238+
+ 全局配置
239+
+ 开关 `sofa.middleware.log.console` 将所有日志空间均打印在 console 端, 默认为 false.
240+
+ 日志级别控制 `sofa.middleware.log.console.level` 配置所有打印在 console 端的日志空间级别,默认为 INFO.
241+
+ 日志空间独立配置
242+
+ 开关 `sofa.middleware.log.${spaceid}.console` 指定 space id 日志空间打印在 console 端,默认为 false. 优先级高于全局配置。
243+
+ 日志级别控制 `sofa.middleware.log.{space id}.console.level` 配置 space id 日志空间打印在 console 的日志级别,默认为 INFO. 优先级高于全局配置。
244+
**支持通过 -D 作为 VM 参数传入以及在 Spring Boot 的 application.yml 配置文件传入(依赖 log-sofa-boot-starter)**
245+
246+
### 控制台日志格式
247+
+ logback: `sofa.middleware.log.console.logback.pattern` 配置打印在 console 端的 logback 日志格式. 默认为 `%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID:- } --- [%15.15t] %-40.40logger{39} : %m%n`
248+
+ log4j2: `sofa.middleware.log.console.log4j2.pattern` 配置打印在 console 端的 log4j 日志格式. 默认为 `%d{yyyy-MM-dd HH:mm:ss.SSS} %5p %X{PID} --- [%15.15t] %-40.40logger{39} : %m%n`
249+
**支持通过 -D 作为 VM 参数传入以及在 Spring Boot 的 application.yml 配置文件传入(依赖 log-sofa-boot-starter)**
250+
251+
## 五、编译
252+
Maven 3.2.5+, JDK Version 1.7+
253+
254+
## 六、LICENSE
203255

204256
[Apache 2.0](./LICENSE)
205257

206-
## 、Contribution
258+
## 、Contribution
207259

208260
[Contribution Guide](./CONTRIBUTING.md)
209261

core/pom.xml

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>sofa-common-tools-parent</artifactId>
7+
<groupId>com.alipay.sofa.common</groupId>
8+
<version>1.0.15-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>sofa-common-tools</artifactId>
13+
14+
<properties>
15+
<!-- log4j2 2.3 for jdk6 -->
16+
<log4j2.version>2.3</log4j2.version>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.slf4j</groupId>
22+
<artifactId>slf4j-api</artifactId>
23+
<version>${slf4j.version}</version>
24+
</dependency>
25+
26+
<!--log4j + slf4j-->
27+
<dependency>
28+
<groupId>org.slf4j</groupId>
29+
<artifactId>slf4j-log4j12</artifactId>
30+
<version>1.7.21</version>
31+
<scope>provided</scope>
32+
</dependency>
33+
34+
35+
<!-- Log4j2 + Slf4j -->
36+
<dependency>
37+
<groupId>org.apache.logging.log4j</groupId>
38+
<artifactId>log4j-slf4j-impl</artifactId>
39+
<version>${log4j2.version}</version>
40+
<scope>provided</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.apache.logging.log4j</groupId>
44+
<artifactId>log4j-api</artifactId>
45+
<version>${log4j2.version}</version>
46+
<scope>provided</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.apache.logging.log4j</groupId>
50+
<artifactId>log4j-core</artifactId>
51+
<version>${log4j2.version}</version>
52+
<scope>provided</scope>
53+
</dependency>
54+
55+
56+
<!-- logback + slf4j -->
57+
<dependency>
58+
<groupId>ch.qos.logback</groupId>
59+
<artifactId>logback-classic</artifactId>
60+
<version>${logback.version}</version>
61+
<scope>provided</scope>
62+
</dependency>
63+
<dependency>
64+
<groupId>ch.qos.logback</groupId>
65+
<artifactId>logback-core</artifactId>
66+
<version>${logback.version}</version>
67+
<scope>provided</scope>
68+
</dependency>
69+
70+
<!-- commons-logging slf4j-jcl log4j-->
71+
<dependency>
72+
<groupId>commons-logging</groupId>
73+
<artifactId>commons-logging</artifactId>
74+
<version>1.1.1</version>
75+
<scope>provided</scope>
76+
</dependency>
77+
<dependency>
78+
<groupId>org.slf4j</groupId>
79+
<artifactId>slf4j-jcl</artifactId>
80+
<version>1.6.1</version>
81+
<scope>provided</scope>
82+
</dependency>
83+
84+
<dependency>
85+
<groupId>junit</groupId>
86+
<artifactId>junit</artifactId>
87+
<version>4.11</version>
88+
<scope>test</scope>
89+
</dependency>
90+
91+
<dependency>
92+
<groupId>commons-io</groupId>
93+
<artifactId>commons-io</artifactId>
94+
<version>2.4</version>
95+
<scope>test</scope>
96+
</dependency>
97+
</dependencies>
98+
99+
<build>
100+
<plugins>
101+
<plugin>
102+
<groupId>org.apache.maven.plugins</groupId>
103+
<artifactId>maven-compiler-plugin</artifactId>
104+
<version>2.0.2</version>
105+
<configuration>
106+
<source>1.6</source>
107+
<target>1.6</target>
108+
<encoding>UTF-8</encoding>
109+
</configuration>
110+
</plugin>
111+
</plugins>
112+
</build>
113+
114+
</project>

src/main/java/com/alipay/sofa/common/log/Constants.java renamed to core/src/main/java/com/alipay/sofa/common/log/Constants.java

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@
2222
import java.io.File;
2323

2424
/**
25-
* Constants middleware log 中涉及的常量信息,其中日志级别默认为 INFO
26-
* <p>
25+
* Class to hold configuration keys.
26+
*
2727
* Created by yangguanchao on 16/10/26.
2828
*/
2929
public interface Constants {
30-
//app中配置,如果没有配置最终会使用(ROOT Log);
30+
// default logger, use ROOT logger if not configured in app.
3131
Logger DEFAULT_LOG = LoggerFactory
3232
.getLogger("com.alipay.sofa.common.log");
33+
String LOG_START = "*";
3334
String LOG_DIRECTORY = "log";
3435
String LOG_XML_CONFIG_FILE_NAME = "log-conf.xml";
3536
String LOG_XML_CONFIG_FILE_ENV_PATTERN = "log-conf-%s.xml";
@@ -39,28 +40,60 @@ public interface Constants {
3940
String LOG_PATH_PREFIX = "logging.path.";
4041

4142
String OLD_LOG_PATH = "loggingRoot";
43+
String LOG_LEVEL = "logging.level";
4244
String LOG_LEVEL_PREFIX = "logging.level.";
45+
String LOG_CONFIG_PREFIX = "logging.config.";
4346
String DEFAULT_MIDDLEWARE_SPACE_LOG_LEVEL = "INFO";
44-
// 指定特定spaceName的环境配置,值格式: {spaceName:dev, spaceName:test, spaceName:product}, 多个用 '&' 符隔开
47+
String IS_DEFAULT_LOG_PATH = "isDefaultLogPath";
48+
String IS_DEFAULT_LOG_LEVEL = "isDefaultLogLevel";
49+
// specify log conf file according different environment, such as log-conf-dev.xml
50+
// value pattern is similar to log.env.suffix=spaceName:dev&spaceName:test
4551
String LOG_ENV_SUFFIX = "log.env.suffix";
46-
//系统变量key -D
52+
// file encoding configured by VM option
4753
String LOG_ENCODING_PROP_KEY = "file.encoding";
48-
//禁用所有日志实现
54+
// disable space log
4955
String SOFA_MIDDLEWARE_LOG_DISABLE_PROP_KEY = "sofa.middleware.log.disable";
50-
//禁用 commons-logging 的 log4j 日志实现
56+
// disable log4j bridge to commons logging
5157
String LOG4J_COMMONS_LOGGING_MIDDLEWARE_LOG_DISABLE_PROP_KEY = "log4j.commons.logging.middleware.log.disable";
52-
//禁用log4j日志实现
58+
// disable log4j space factory
5359
String LOG4J_MIDDLEWARE_LOG_DISABLE_PROP_KEY = "log4j.middleware.log.disable";
54-
//禁用log4j2日志实现
60+
// disable log4j2 space factory
5561
String LOG4J2_MIDDLEWARE_LOG_DISABLE_PROP_KEY = "log4j2.middleware.log.disable";
56-
//禁用logback日志实现
62+
// disable logback space factory
5763
String LOGBACK_MIDDLEWARE_LOG_DISABLE_PROP_KEY = "logback.middleware.log.disable";
58-
//默认日志编码
64+
// UTF-8 encoding
5965
String UTF8_STR = "UTF-8";
60-
//默认的中间件日志打印路径
66+
// default log path
6167
String LOGGING_PATH_DEFAULT = System.getProperty("user.home")
6268
+ File.separator + "logs";
63-
// 默认优先级为0,越大越高
69+
// default priority, Larger numbers indicate higher priority
6470
int DEFAULT_PRIORITY = 0;
6571

72+
// PID key
73+
String PROCESS_MARKER = "PID";
74+
// config prefix
75+
String SOFA_MIDDLEWARE_CONFIG_PREFIX = "sofa.middleware.log.";
76+
// global switch to log on console
77+
String SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH = "sofa.middleware.log.console";
78+
// single space switch to log on console
79+
String SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_SWITCH = "sofa.middleware.log.%s.console";
80+
// sofa-common-tools 自身日志开关
81+
// internal log level config.
82+
String SOFA_MIDDLEWARE_LOG_INTERNAL_LEVEL = "sofa.middleware.log.internal.level";
83+
// global console log level
84+
String SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_LEVEL = "sofa.middleware.log.console.level";
85+
// single space console log level
86+
String SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_LEVEL = "sofa.middleware.log.%s.console.level";
87+
// logback log pattern on console
88+
String SOFA_MIDDLEWARE_LOG_CONSOLE_LOGBACK_PATTERN = "sofa.middleware.log.console.logback.pattern";
89+
// log4j2 log pattern on console
90+
String SOFA_MIDDLEWARE_LOG_CONSOLE_LOG4J2_PATTERN = "sofa.middleware.log.console.log4j2.pattern";
91+
// default logback log pattern
92+
String SOFA_MIDDLEWARE_LOG_CONSOLE_LOGBACK_PATTERN_DEFAULT = "%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID:- } --- [%15.15t] %-40.40logger{39} : %m%n";
93+
// default log4j2 log pattern
94+
String SOFA_MIDDLEWARE_LOG_CONSOLE_LOG4J2_PATTERN_DEFAULT = "%d{yyyy-MM-dd HH:mm:ss.SSS} %5p %X{PID} --- [%15.15t] %-40.40logger{39} : %m%n";
95+
// logging path file
96+
String LOGGING_CONFIG_PATH = "logging.config.%s";
97+
// config marker
98+
String SOFA_LOG_FIRST_INITIALIZE = "sofa.log.first.initialize";
6699
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alipay.sofa.common.log;
18+
19+
import com.alipay.sofa.common.utils.StringUtil;
20+
21+
import java.util.HashMap;
22+
import java.util.Map;
23+
24+
import static com.alipay.sofa.common.log.Constants.SOFA_MIDDLEWARE_LOG_INTERNAL_LEVEL;
25+
26+
/**
27+
* This class used to output log statements from within the sofa-common-tools package.
28+
* <p>
29+
* And also it is used to output log statements to console appender.
30+
*
31+
* @author qilong.zql
32+
* @since 1.0.15
33+
*/
34+
public class LogLog {
35+
private static final String DEBUG_PREFIX = "Sofa-Middleware-Log:DEBUG ";
36+
private static final String INFO_PREFIX = "Sofa-Middleware-Log:INFO ";
37+
private static final String ERR_PREFIX = "Sofa-Middleware-Log:ERROR ";
38+
private static final String WARN_PREFIX = "Sofa-Middleware-Log:WARN ";
39+
40+
private static final Map<String, Level> LEVELS = new HashMap<String, Level>();
41+
42+
private volatile static Level consoleLogLevel = Level.WARN;
43+
44+
static {
45+
LEVELS.put("DEBUG", Level.DEBUG);
46+
LEVELS.put("INFO", Level.INFO);
47+
LEVELS.put("WARN", Level.WARN);
48+
LEVELS.put("ERROR", Level.ERROR);
49+
}
50+
51+
public static void setConsoleLevel(String level) {
52+
Level value = LEVELS.get(level.toUpperCase());
53+
if (!StringUtil.isBlank(level) && value != null) {
54+
consoleLogLevel = value;
55+
}
56+
}
57+
58+
public static void debug(String msg) {
59+
if (isDebug()) {
60+
System.out.println(DEBUG_PREFIX + msg);
61+
}
62+
}
63+
64+
public static void info(String msg) {
65+
if (isInfo()) {
66+
System.out.println(INFO_PREFIX + msg);
67+
}
68+
}
69+
70+
public static void warn(String msg) {
71+
if (isWarn()) {
72+
System.err.println(WARN_PREFIX + msg);
73+
}
74+
}
75+
76+
public static void error(String msg, Throwable throwable) {
77+
if (isError()) {
78+
System.err.println(ERR_PREFIX + msg);
79+
if (throwable != null) {
80+
throwable.printStackTrace();
81+
}
82+
}
83+
}
84+
85+
private static boolean isDebug() {
86+
setConsoleLevel(System.getProperty(SOFA_MIDDLEWARE_LOG_INTERNAL_LEVEL, "WARN"));
87+
return consoleLogLevel.equals(Level.DEBUG);
88+
}
89+
90+
private static boolean isInfo() {
91+
return isDebug() || consoleLogLevel.equals(Level.INFO);
92+
}
93+
94+
private static boolean isWarn() {
95+
return isInfo() || consoleLogLevel.equals(Level.WARN);
96+
}
97+
98+
private static boolean isError() {
99+
return isWarn() || consoleLogLevel.equals(Level.ERROR);
100+
}
101+
102+
enum Level {
103+
DEBUG, INFO, WARN, ERROR
104+
}
105+
}

0 commit comments

Comments
 (0)