Skip to content

Commit baa605b

Browse files
authored
Merge pull request #61 from dgzpg/feat-addDynamicallyAdjustLogLevel
Feat add dynamically adjust log level
2 parents 5ccf47a + cda1bc3 commit baa605b

12 files changed

Lines changed: 361 additions & 23 deletions

File tree

examples/src/main/java/group/rxcloud/capa/examples/log/DemoLog.java renamed to examples/src/main/java/group/rxcloud/capa/examples/telemetry/DemoLog.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package group.rxcloud.capa.examples.log;
17+
package group.rxcloud.capa.examples.telemetry;
1818

1919
import lombok.extern.slf4j.Slf4j;
2020

@@ -30,6 +30,11 @@
3030
public class DemoLog {
3131

3232
public static void main(String[] args) {
33-
log.info("test");
33+
try {
34+
log.info("test");
35+
}catch (Exception e){
36+
System.out.println();
37+
}
38+
3439
}
3540
}

pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,5 +414,4 @@
414414
</plugin>
415415
</plugins>
416416
</build>
417-
418417
</project>

sdk-component/pom.xml

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
<kotlin-stdlib.version>1.4.10</kotlin-stdlib.version>
3636
<log4j.version>2.8.2</log4j.version>
3737
<logback.version>1.1.7</logback.version>
38+
<guava-version>19.0</guava-version>
39+
<commons-lang3-version>3.11</commons-lang3-version>
40+
<commons-collections-version>3.2.2</commons-collections-version>
3841
</properties>
3942

4043
<dependencies>
@@ -98,7 +101,13 @@
98101
<optional>true</optional>
99102
<version>${logback.version}</version>
100103
</dependency>
101-
104+
<dependency>
105+
<groupId>ch.qos.logback</groupId>
106+
<artifactId>logback-classic</artifactId>
107+
<!-- log4j-slf4j-impl and logback-classic cannot exist at the same time. -->
108+
<optional>true</optional>
109+
<version>${logback.version}</version>
110+
</dependency>
102111
<dependency>
103112
<groupId>org.junit.jupiter</groupId>
104113
<artifactId>junit-jupiter-engine</artifactId>
@@ -109,6 +118,21 @@
109118
<artifactId>mockito-core</artifactId>
110119
<scope>test</scope>
111120
</dependency>
121+
<dependency>
122+
<groupId>com.google.guava</groupId>
123+
<artifactId>guava</artifactId>
124+
<version>${guava-version}</version>
125+
</dependency>
126+
<dependency>
127+
<groupId>org.apache.commons</groupId>
128+
<artifactId>commons-lang3</artifactId>
129+
<version>${commons-lang3-version}</version>
130+
</dependency>
131+
<dependency>
132+
<groupId>commons-collections</groupId>
133+
<artifactId>commons-collections</artifactId>
134+
<version>${commons-collections-version}</version>
135+
</dependency>
112136
</dependencies>
113137

114138
<build>

sdk-component/src/main/java/group/rxcloud/capa/component/log/agent/CapaLog4jAppenderAgent.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package group.rxcloud.capa.component.log.agent;
1818

19+
import group.rxcloud.capa.component.log.enums.CapaLogLevel;
20+
import group.rxcloud.capa.component.log.manager.LogManager;
1921
import group.rxcloud.capa.infrastructure.CapaClassLoader;
2022
import org.apache.logging.log4j.core.Appender;
2123
import org.apache.logging.log4j.core.Filter;
@@ -28,6 +30,7 @@
2830
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
2931

3032
import java.io.Serializable;
33+
import java.util.Optional;
3134

3235
/**
3336
* The abstract log4j appender. Extend this and provide your specific impl.
@@ -97,7 +100,12 @@ public static CapaLog4jAppender buildCapaLog4jAppender() {
97100

98101
@Override
99102
public void append(LogEvent event) {
100-
logAppender.appendLog(event);
103+
if (event != null && event.getLevel() != null) {
104+
Optional<CapaLogLevel> capaLogLevel = CapaLogLevel.toCapaLogLevel(event.getLevel().name());
105+
if (capaLogLevel.isPresent() && LogManager.whetherLogsCanBeOutput(capaLogLevel.get())) {
106+
logAppender.appendLog(event);
107+
}
108+
}
101109
}
102110

103111
/**

sdk-component/src/main/java/group/rxcloud/capa/component/log/agent/CapaLogbackAppenderAgent.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@
1616
*/
1717
package group.rxcloud.capa.component.log.agent;
1818

19+
import ch.qos.logback.classic.spi.ILoggingEvent;
1920
import ch.qos.logback.core.UnsynchronizedAppenderBase;
21+
import group.rxcloud.capa.component.log.enums.CapaLogLevel;
22+
import group.rxcloud.capa.component.log.manager.LogManager;
2023
import group.rxcloud.capa.infrastructure.CapaClassLoader;
2124

25+
import java.util.Optional;
26+
2227
/**
2328
* The agent of the logback impl.
2429
*/
25-
public class CapaLogbackAppenderAgent<EVENT> extends UnsynchronizedAppenderBase<EVENT> {
30+
public class CapaLogbackAppenderAgent extends UnsynchronizedAppenderBase<ILoggingEvent> {
2631

2732
/**
2833
* The log component type.
@@ -58,20 +63,25 @@ public static CapaLogbackAppender buildCapaLogbackAppender() {
5863
* @param event The log event.
5964
*/
6065
@Override
61-
protected void append(EVENT event) {
62-
logbackAppender.appendLog(event);
66+
protected void append(ILoggingEvent event) {
67+
if (event != null && event.getLevel() != null) {
68+
Optional<CapaLogLevel> capaLogLevel = CapaLogLevel.toCapaLogLevel(event.getLevel().levelStr);
69+
if (capaLogLevel.isPresent() && LogManager.whetherLogsCanBeOutput(capaLogLevel.get())) {
70+
logbackAppender.appendLog(event);
71+
}
72+
}
6373
}
6474

6575
/**
6676
* The abstract api of the logback appender impl.Implement this and provide your specific impl.
6777
*/
68-
public interface CapaLogbackAppender<EVENT> {
78+
public interface CapaLogbackAppender {
6979

7080
/**
7181
* Deal with the log.
7282
*
7383
* @param event The log event.
7484
*/
75-
void appendLog(EVENT event);
85+
void appendLog(ILoggingEvent event);
7686
}
7787
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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 group.rxcloud.capa.component.log.configuration;
18+
19+
import com.google.common.collect.Lists;
20+
import com.google.common.collect.Maps;
21+
import group.rxcloud.capa.infrastructure.hook.ConfigurationHooks;
22+
import group.rxcloud.capa.infrastructure.hook.Mixer;
23+
import group.rxcloud.cloudruntimes.domain.core.configuration.SubConfigurationResp;
24+
import group.rxcloud.cloudruntimes.utils.TypeRef;
25+
import org.apache.commons.collections.CollectionUtils;
26+
import org.apache.commons.lang3.StringUtils;
27+
import reactor.core.publisher.Flux;
28+
29+
import java.util.Map;
30+
import java.util.Optional;
31+
32+
/**
33+
* Log switch configuration
34+
*/
35+
public class LogSwitchConfiguration {
36+
37+
/**
38+
* Capa configuration hooks.
39+
*/
40+
private static final Optional<ConfigurationHooks> configurationHooks;
41+
/**
42+
* Log switch config file name.
43+
*/
44+
private static final String LOGS_SWITCH_CONFIG_FILE_NAME = "log-level-switch.properties";
45+
/**
46+
* Log switch config's value.
47+
*/
48+
private static Map<String, Boolean> logsSwitchConfig = Maps.newHashMap();
49+
50+
/**
51+
* Init configuration hooks and subscribe configuration.
52+
*/
53+
static {
54+
configurationHooks = Mixer.configurationHooksNullable();
55+
configurationHooks.ifPresent(configurationHooks -> {
56+
subscribeConfiguration(configurationHooks);
57+
});
58+
}
59+
60+
/**
61+
* Subscribe configuration.
62+
*
63+
* @param configurationHooks
64+
*/
65+
private static void subscribeConfiguration(ConfigurationHooks configurationHooks) {
66+
String storeName = configurationHooks.registryStoreNames().get(0);
67+
String appId = configurationHooks.defaultConfigurationAppId();
68+
Flux<SubConfigurationResp<Map>> configFlux = configurationHooks.subscribeConfiguration(
69+
storeName,
70+
appId,
71+
Lists.newArrayList(LOGS_SWITCH_CONFIG_FILE_NAME),
72+
null,
73+
StringUtils.EMPTY,
74+
StringUtils.EMPTY,
75+
TypeRef.get(Map.class));
76+
configFlux.subscribe(resp -> {
77+
if (CollectionUtils.isNotEmpty(resp.getItems())) {
78+
logsSwitchConfig = resp.getItems().get(0).getContent();
79+
}
80+
});
81+
}
82+
83+
/**
84+
* Get the log switch config's values.
85+
*
86+
* @return the log switch config's values.
87+
*/
88+
public static Map<String, Boolean> getLogsSwitchConfig() {
89+
return logsSwitchConfig;
90+
}
91+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 group.rxcloud.capa.component.log.enums;
18+
19+
import java.util.Arrays;
20+
import java.util.Optional;
21+
22+
/**
23+
* Capa log level.
24+
*/
25+
public enum CapaLogLevel {
26+
/**
27+
* Standard order of log priorities:ALL,TRACE,DEBUG,INFO,WARN,ERROR,FATAL,OFF
28+
*/
29+
ALL(1, "ALL"),
30+
TRACE(2, "TRACE"),
31+
DEBUG(3, "DEBUG"),
32+
INFO(4, "INFO"),
33+
WARN(5, "WARN"),
34+
ERROR(6, "ERROR"),
35+
FATAL(7, "FATAL"),
36+
OFF(8, "OFF");
37+
38+
final int level;
39+
final String levelName;
40+
41+
CapaLogLevel(int level, String levelName) {
42+
this.level = level;
43+
this.levelName = levelName;
44+
}
45+
46+
/**
47+
* Convert logLevelArg to {@link CapaLogLevel}
48+
*
49+
* @param logLevelArg
50+
* @return
51+
*/
52+
public static Optional<CapaLogLevel> toCapaLogLevel(String logLevelArg) {
53+
return Arrays.stream(CapaLogLevel.values())
54+
.filter(logLevel -> logLevel.levelName.equalsIgnoreCase(logLevelArg))
55+
.findAny();
56+
}
57+
58+
/**
59+
* Get level.
60+
*
61+
* @return
62+
*/
63+
public int getLevel() {
64+
return level;
65+
}
66+
67+
public String getLevelName() {
68+
return levelName;
69+
}
70+
}

0 commit comments

Comments
 (0)