Skip to content

Commit a8203af

Browse files
committed
style: Lower the log adjustment level to the aws implementation part.
Lower the log adjustment level to the aws implementation part.
1 parent d1d349f commit a8203af

File tree

10 files changed

+412
-15
lines changed

10 files changed

+412
-15
lines changed

capa-spi-aws-log/src/main/java/group/rxcloud/capa/spi/aws/log/appender/CapaAwsLog4jAppender.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
import group.rxcloud.capa.infrastructure.hook.Mixer;
2020
import group.rxcloud.capa.infrastructure.hook.TelemetryHooks;
21+
import group.rxcloud.capa.spi.aws.log.enums.CapaLogLevel;
2122
import group.rxcloud.capa.spi.aws.log.manager.LogAppendManager;
23+
import group.rxcloud.capa.spi.aws.log.manager.LogManager;
2224
import group.rxcloud.capa.spi.log.CapaLog4jAppenderSpi;
2325
import io.opentelemetry.api.common.AttributeKey;
2426
import io.opentelemetry.api.common.Attributes;
@@ -78,10 +80,13 @@ public void appendLog(LogEvent event) {
7880
|| event.getMessage() == null) {
7981
return;
8082
}
81-
String message = event.getMessage().getFormattedMessage();
82-
ReadOnlyStringMap contextData = event.getContextData();
83-
Map<String, String> MDCTags = contextData == null ? new HashMap<>() : contextData.toMap();
84-
LogAppendManager.appendLogs(message, MDCTags, event.getLevel().name());
83+
Optional<CapaLogLevel> capaLogLevel = CapaLogLevel.toCapaLogLevel(event.getLevel().name());
84+
if(capaLogLevel.isPresent() && LogManager.logsCanOutput(capaLogLevel.get())){
85+
String message = event.getMessage().getFormattedMessage();
86+
ReadOnlyStringMap contextData = event.getContextData();
87+
Map<String, String> MDCTags = contextData == null ? new HashMap<>() : contextData.toMap();
88+
LogAppendManager.appendLogs(message, MDCTags, event.getLevel().name());
89+
}
8590
} catch (Exception e) {
8691
try {
8792
//Enhance function without affecting function

capa-spi-aws-log/src/main/java/group/rxcloud/capa/spi/aws/log/appender/CapaAwsLogbackAppender.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import ch.qos.logback.classic.spi.ILoggingEvent;
2020
import group.rxcloud.capa.infrastructure.hook.Mixer;
2121
import group.rxcloud.capa.infrastructure.hook.TelemetryHooks;
22+
import group.rxcloud.capa.spi.aws.log.enums.CapaLogLevel;
2223
import group.rxcloud.capa.spi.aws.log.manager.LogAppendManager;
24+
import group.rxcloud.capa.spi.aws.log.manager.LogManager;
2325
import group.rxcloud.capa.spi.log.CapaLogbackAppenderSpi;
2426
import io.opentelemetry.api.common.AttributeKey;
2527
import io.opentelemetry.api.common.Attributes;
@@ -74,9 +76,12 @@ public void appendLog(ILoggingEvent event) {
7476
if (event == null || event.getLevel() == null) {
7577
return;
7678
}
77-
String message = event.getFormattedMessage();
78-
Map<String, String> MDCTags = event.getMDCPropertyMap();
79-
LogAppendManager.appendLogs(message, MDCTags, event.getLevel().levelStr);
79+
Optional<CapaLogLevel> capaLogLevel = CapaLogLevel.toCapaLogLevel(event.getLevel().levelStr);
80+
if (capaLogLevel.isPresent() && LogManager.logsCanOutput(capaLogLevel.get())) {
81+
String message = event.getFormattedMessage();
82+
Map<String, String> MDCTags = event.getMDCPropertyMap();
83+
LogAppendManager.appendLogs(message, MDCTags, event.getLevel().levelStr);
84+
}
8085
} catch (Exception e) {
8186
LONG_COUNTER.ifPresent(longCounter -> {
8287
try {
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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.spi.aws.log.configuration;
18+
19+
import group.rxcloud.capa.component.CapaLogProperties;
20+
import group.rxcloud.capa.infrastructure.hook.MergedPropertiesConfig;
21+
import group.rxcloud.capa.infrastructure.hook.Mixer;
22+
import io.opentelemetry.api.common.AttributeKey;
23+
import io.opentelemetry.api.common.Attributes;
24+
import io.opentelemetry.api.metrics.LongCounter;
25+
import io.opentelemetry.api.metrics.Meter;
26+
27+
import java.util.Optional;
28+
29+
/**
30+
* Log switch configuration
31+
*/
32+
public class LogConfiguration {
33+
34+
/**
35+
* Log switch config file name.
36+
*/
37+
private static final String LOG_CONFIGURATION_COMMON_FILE_NAME = "capa-component-log-configuration.properties";
38+
private static MergedPropertiesConfig mergedPropertiesConfig;
39+
40+
static {
41+
Mixer.configurationHooksNullable().ifPresent(hooks -> {
42+
// TODO: 2021/12/3 Use Configuration extension api to get merged file.
43+
try {
44+
mergedPropertiesConfig = new MergedPropertiesConfig(
45+
LOG_CONFIGURATION_COMMON_FILE_NAME,
46+
hooks.defaultConfigurationAppId(),
47+
CapaLogProperties.Settings.getCenterConfigAppId());
48+
} catch (Throwable throwable) {
49+
Mixer.telemetryHooksNullable().ifPresent(telemetryHooks -> {
50+
Meter meter = telemetryHooks.buildMeter("LogsConfiguration").block();
51+
LongCounter longCounter = meter.counterBuilder("LogsError").build();
52+
Optional<LongCounter> longCounterOptional = Optional.ofNullable(longCounter);
53+
longCounterOptional.ifPresent(counter -> {
54+
longCounter.bind(Attributes.of(AttributeKey.stringKey("LogsConfigurationError"), throwable.getMessage()))
55+
.add(1);
56+
});
57+
});
58+
}
59+
});
60+
}
61+
62+
public static boolean containsKey(String key) {
63+
try {
64+
return mergedPropertiesConfig != null
65+
&& mergedPropertiesConfig.containsKey(key);
66+
} catch (Exception e) {
67+
return false;
68+
}
69+
}
70+
71+
public static String get(String key) {
72+
try {
73+
return mergedPropertiesConfig == null
74+
? ""
75+
: mergedPropertiesConfig.get(key);
76+
} catch (Exception e) {
77+
return "";
78+
}
79+
}
80+
}
81+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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.spi.aws.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+
/**
28+
* Standard order of log priorities:ALL,TRACE,DEBUG,INFO,WARN,ERROR,FATAL,OFF
29+
*/
30+
ALL(1, "ALL"),
31+
TRACE(2, "TRACE"),
32+
DEBUG(3, "DEBUG"),
33+
INFO(4, "INFO"),
34+
WARN(5, "WARN"),
35+
ERROR(6, "ERROR"),
36+
FATAL(7, "FATAL"),
37+
OFF(8, "OFF");
38+
39+
final int level;
40+
final String levelName;
41+
42+
CapaLogLevel(int level, String levelName) {
43+
this.level = level;
44+
this.levelName = levelName;
45+
}
46+
47+
/**
48+
* Convert logLevelArg to {@link CapaLogLevel}
49+
*/
50+
public static Optional<CapaLogLevel> toCapaLogLevel(String logLevelArg) {
51+
return Arrays.stream(CapaLogLevel.values())
52+
.filter(logLevel -> logLevel.levelName.equalsIgnoreCase(logLevelArg))
53+
.findAny();
54+
}
55+
56+
/**
57+
* Get level.
58+
*/
59+
public int getLevel() {
60+
return level;
61+
}
62+
63+
public String getLevelName() {
64+
return levelName;
65+
}
66+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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.spi.aws.log.manager;
18+
19+
import group.rxcloud.capa.spi.aws.log.configuration.LogConfiguration;
20+
import group.rxcloud.capa.spi.aws.log.enums.CapaLogLevel;
21+
22+
import java.util.Optional;
23+
24+
/**
25+
* LogManager, to manage log output levels.
26+
*/
27+
public class LogManager {
28+
29+
/**
30+
* Dynamically adjust the log level switch name.
31+
*/
32+
private static final String LOG_SWITCH_NAME = "logSwitch";
33+
/**
34+
* Output log level name.
35+
*/
36+
private static final String OUTPUT_LOG_LEVEL_NAME = "outputLogLevel";
37+
38+
/**
39+
* If the configured log level false, then it returns false and no log is output.
40+
* <p>
41+
* If configured logSwitch is not empty and true or logSwitch is empty, then process the output log level judgment logic.
42+
* <p>
43+
* If the configured output log level is judged to be empty, then can output the log and return true.
44+
* <p>
45+
* If the configured output log level is not empty, but it is not a standard log level, you can output and return true.
46+
* <p>
47+
* If the configured output log level is not empty and the log level is standard,
48+
* then need to compare whether the actual output log level is higher than or equal to the configured log level.
49+
* If it is higher than or equal to the configured log level, it can be output and return true, otherwise it cannot be output,returns false.
50+
*
51+
* @param capaLogLevel
52+
* @return
53+
*/
54+
public static Boolean logsCanOutput(CapaLogLevel capaLogLevel) {
55+
if (LogConfiguration.containsKey(LOG_SWITCH_NAME)
56+
&& String.valueOf(Boolean.FALSE).equalsIgnoreCase(LogConfiguration.get(LOG_SWITCH_NAME))) {
57+
return Boolean.FALSE;
58+
}
59+
if (LogConfiguration.containsKey(OUTPUT_LOG_LEVEL_NAME)) {
60+
Optional<CapaLogLevel> outputLogLevel = CapaLogLevel.toCapaLogLevel(LogConfiguration.get(OUTPUT_LOG_LEVEL_NAME));
61+
if (outputLogLevel.isPresent()) {
62+
return capaLogLevel.getLevel() >= outputLogLevel.get().getLevel();
63+
}
64+
}
65+
return Boolean.TRUE;
66+
}
67+
}

capa-spi-aws-log/src/main/java/group/rxcloud/capa/spi/aws/log/service/CloudWatchLogsService.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ public class CloudWatchLogsService {
6060
private static final String CLOUD_WATCH_LOGS_ERROR_METRIC_NAME = "LogsError";
6161
private static final String CLOUD_WATCH_LOGS_PUT_LOG_EVENT_ERROR_TYPE = "PutLogEventError";
6262
private static final String CLOUD_WATCH_LOGS_PUT_LOG_EVENTS_ERROR_TYPE = "PutLogEventsError";
63-
private static final String CLOUD_WATCH_LOGS_RESPONSE_NULL_VALUE = "NULL";
6463
private static final Integer COUNTER_NUM = 1;
6564

6665
private static final Optional<TelemetryHooks> TELEMETRY_HOOKS;
@@ -118,9 +117,6 @@ public static void putLogEvent(String message, String logStreamName) {
118117
|| !putLogEventsResponse.sdkHttpResponse().isSuccessful()) {
119118
try {
120119
//Enhance function without affecting function
121-
String statusCode = putLogEventsResponse == null || putLogEventsResponse.sdkHttpResponse() == null
122-
? CLOUD_WATCH_LOGS_RESPONSE_NULL_VALUE
123-
: String.valueOf(putLogEventsResponse.sdkHttpResponse().statusCode());
124120
LONG_COUNTER.ifPresent(longCounter -> {
125121
longCounter.bind(Attributes.of(AttributeKey.stringKey(CLOUD_WATCH_LOGS_PUT_LOG_EVENT_ERROR_TYPE), CLOUD_WATCH_LOGS_PUT_LOG_EVENT_ERROR_TYPE))
126122
.add(COUNTER_NUM);
@@ -167,9 +163,6 @@ public static void putLogEvents(List<String> messages, String logStreamName) {
167163
|| !putLogEventsResponse.sdkHttpResponse().isSuccessful()) {
168164
try {
169165
//Enhance function without affecting function
170-
String statusCode = putLogEventsResponse == null || putLogEventsResponse.sdkHttpResponse() == null
171-
? CLOUD_WATCH_LOGS_RESPONSE_NULL_VALUE
172-
: String.valueOf(putLogEventsResponse.sdkHttpResponse().statusCode());
173166
LONG_COUNTER.ifPresent(longCounter -> {
174167
longCounter.bind(Attributes.of(AttributeKey.stringKey(CLOUD_WATCH_LOGS_PUT_LOG_EVENTS_ERROR_TYPE), CLOUD_WATCH_LOGS_PUT_LOG_EVENTS_ERROR_TYPE))
175168
.add(COUNTER_NUM);

0 commit comments

Comments
 (0)