From b2e5bdc3681a6688f313439502f0e35229bf2395 Mon Sep 17 00:00:00 2001 From: = Date: Thu, 9 Dec 2021 16:16:49 +0800 Subject: [PATCH] style: Change the structure of the log to make the log sdk more lightweight. Change the structure of the log to make the log sdk more lightweight. Lower the log adjustment level to the specific implementation part. #82 --- .../capa/spi/log/CapaLog4jAppenderSpi.java | 11 +- .../capa/spi/log/CapaLogbackAppenderSpi.java | 11 +- .../log/configuration/LogConfiguration.java | 81 ------------ .../capa/spi/log/enums/CapaLogLevel.java | 66 --------- .../capa/spi/log/manager/LogManager.java | 125 ------------------ 5 files changed, 2 insertions(+), 292 deletions(-) delete mode 100644 sdk-spi/src/main/java/group/rxcloud/capa/spi/log/configuration/LogConfiguration.java delete mode 100644 sdk-spi/src/main/java/group/rxcloud/capa/spi/log/enums/CapaLogLevel.java delete mode 100644 sdk-spi/src/main/java/group/rxcloud/capa/spi/log/manager/LogManager.java diff --git a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLog4jAppenderSpi.java b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLog4jAppenderSpi.java index 12fbf3f..7481fc7 100644 --- a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLog4jAppenderSpi.java +++ b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLog4jAppenderSpi.java @@ -17,22 +17,13 @@ package group.rxcloud.capa.spi.log; import group.rxcloud.capa.component.log.CapaLog4jAppenderAgent; -import group.rxcloud.capa.spi.log.enums.CapaLogLevel; -import group.rxcloud.capa.spi.log.manager.LogManager; import org.apache.logging.log4j.core.LogEvent; -import java.util.Optional; - public abstract class CapaLog4jAppenderSpi implements CapaLog4jAppenderAgent.CapaLog4jAppender { @Override public void append(LogEvent event) { - if (event != null && event.getLevel()!= null) { - Optional capaLogLevel = CapaLogLevel.toCapaLogLevel(event.getLevel().name()); - if (capaLogLevel.isPresent() && LogManager.whetherLogsCanOutput(capaLogLevel.get())) { - this.appendLog(event); - } - } + this.appendLog(event); } protected abstract void appendLog(LogEvent event); diff --git a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLogbackAppenderSpi.java b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLogbackAppenderSpi.java index 7950033..79efb09 100644 --- a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLogbackAppenderSpi.java +++ b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLogbackAppenderSpi.java @@ -19,22 +19,13 @@ import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.UnsynchronizedAppenderBase; import group.rxcloud.capa.component.log.CapaLogbackAppenderAgent; -import group.rxcloud.capa.spi.log.enums.CapaLogLevel; -import group.rxcloud.capa.spi.log.manager.LogManager; - -import java.util.Optional; public abstract class CapaLogbackAppenderSpi extends UnsynchronizedAppenderBase implements CapaLogbackAppenderAgent.CapaLogbackAppender { @Override public void append(ILoggingEvent event) { - if (event != null && event.getLevel() != null) { - Optional capaLogLevel = CapaLogLevel.toCapaLogLevel(event.getLevel().levelStr); - if (capaLogLevel.isPresent() && LogManager.whetherLogsCanOutput(capaLogLevel.get())) { - this.appendLog(event); - } - } + this.appendLog(event); } protected abstract void appendLog(ILoggingEvent event); diff --git a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/configuration/LogConfiguration.java b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/configuration/LogConfiguration.java deleted file mode 100644 index 8074a9f..0000000 --- a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/configuration/LogConfiguration.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package group.rxcloud.capa.spi.log.configuration; - -import group.rxcloud.capa.component.CapaLogProperties; -import group.rxcloud.capa.infrastructure.hook.MergedPropertiesConfig; -import group.rxcloud.capa.infrastructure.hook.Mixer; -import io.opentelemetry.api.common.AttributeKey; -import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.metrics.LongCounter; -import io.opentelemetry.api.metrics.Meter; - -import java.util.Optional; - -/** - * Log switch configuration - */ -public class LogConfiguration { - - /** - * Log switch config file name. - */ - private static final String LOG_CONFIGURATION_COMMON_FILE_NAME = "capa-component-log-configuration.properties"; - private static MergedPropertiesConfig mergedPropertiesConfig; - - static { - Mixer.configurationHooksNullable().ifPresent(hooks -> { - // TODO: 2021/12/3 Use Configuration extension api to get merged file. - try { - mergedPropertiesConfig = new MergedPropertiesConfig( - LOG_CONFIGURATION_COMMON_FILE_NAME, - hooks.defaultConfigurationAppId(), - CapaLogProperties.Settings.getCenterConfigAppId()); - } catch (Throwable throwable) { - Mixer.telemetryHooksNullable().ifPresent(telemetryHooks -> { - Meter meter = telemetryHooks.buildMeter("LogsConfiguration").block(); - LongCounter longCounter = meter.counterBuilder("LogsError").build(); - Optional longCounterOptional = Optional.ofNullable(longCounter); - longCounterOptional.ifPresent(counter -> { - longCounter.bind(Attributes.of(AttributeKey.stringKey("LogsConfigurationError"), throwable.getMessage())) - .add(1); - }); - }); - } - }); - } - - public static boolean containsKey(String key) { - try { - return mergedPropertiesConfig != null - && mergedPropertiesConfig.containsKey(key); - } catch (Exception e) { - return false; - } - } - - public static String get(String key) { - try { - return mergedPropertiesConfig == null - ? "" - : mergedPropertiesConfig.get(key); - } catch (Exception e) { - return ""; - } - } -} - diff --git a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/enums/CapaLogLevel.java b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/enums/CapaLogLevel.java deleted file mode 100644 index df3a2be..0000000 --- a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/enums/CapaLogLevel.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package group.rxcloud.capa.spi.log.enums; - -import java.util.Arrays; -import java.util.Optional; - -/** - * Capa log level. - */ -public enum CapaLogLevel { - - /** - * Standard order of log priorities:ALL,TRACE,DEBUG,INFO,WARN,ERROR,FATAL,OFF - */ - ALL(1, "ALL"), - TRACE(2, "TRACE"), - DEBUG(3, "DEBUG"), - INFO(4, "INFO"), - WARN(5, "WARN"), - ERROR(6, "ERROR"), - FATAL(7, "FATAL"), - OFF(8, "OFF"); - - final int level; - final String levelName; - - CapaLogLevel(int level, String levelName) { - this.level = level; - this.levelName = levelName; - } - - /** - * Convert logLevelArg to {@link CapaLogLevel} - */ - public static Optional toCapaLogLevel(String logLevelArg) { - return Arrays.stream(CapaLogLevel.values()) - .filter(logLevel -> logLevel.levelName.equalsIgnoreCase(logLevelArg)) - .findAny(); - } - - /** - * Get level. - */ - public int getLevel() { - return level; - } - - public String getLevelName() { - return levelName; - } -} diff --git a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/manager/LogManager.java b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/manager/LogManager.java deleted file mode 100644 index 8c3df4d..0000000 --- a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/manager/LogManager.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package group.rxcloud.capa.spi.log.manager; - -import group.rxcloud.capa.spi.log.configuration.LogConfiguration; -import group.rxcloud.capa.spi.log.enums.CapaLogLevel; - -/** - * LogManager, to manage log output levels. - */ -public class LogManager { - - /** - * Dynamically adjust the log level switch name. - */ - private static final String LOG_LEVEL_SWITCH_NAME = "logLevelSwitch"; - /** - * All level switch name. - */ - private static final String ALL_LEVEL_SWITCH_NAME = "allLevelSwitch"; - /** - * Trace level switch name. - */ - private static final String TRACE_LEVEL_SWITCH_NAME = "traceLevelSwitch"; - /** - * Debug level switch name. - */ - private static final String DEBUG_LEVEL_SWITCH_NAME = "debugLevelSwitch"; - /** - * Info level switch name. - */ - private static final String INFO_LEVEL_SWITCH_NAME = "infoLevelSwitch"; - /** - * Warn level switch. - */ - private static final String WARN_LEVEL_SWITCH_NAME = "warnLevelSwitch"; - /** - * Error level switch. - */ - private static final String ERROR_LEVEL_SWITCH_NAME = "errorLevelSwitch"; - /** - * Fatal level switch - */ - private static final String FATAL_LEVEL_SWITCH_NAME = "fatalLevelSwitch"; - /** - * Off level switch. - */ - private static final String OFF_LEVEL_SWITCH_NAME = "offLevelSwitch"; - - /** - * Whether logs can be output. - * If there is no corresponding configuration information or the switch of dynamic configuration log level is set to false, - * then only logs of info level and above will be output. - *

- * If the value of the dynamic configuration log level switch is true, only the log with the log level configured as true will be output. - *

- * If the log level is not in the {@link CapaLogLevel#values()}, the log is not output. - * - * @return If the log can be output, it is true, otherwise it is false. - */ - public static Boolean whetherLogsCanOutput(CapaLogLevel capaLogLevel) { - if (!LogConfiguration.containsKey(LOG_LEVEL_SWITCH_NAME) || Boolean.FALSE.equals(LogConfiguration.get(LOG_LEVEL_SWITCH_NAME))) { - return isLogsLevelClosedWithDefault(capaLogLevel); - } - switch (capaLogLevel) { - case ALL: - return LogConfiguration.containsKey(ALL_LEVEL_SWITCH_NAME) - ? Boolean.parseBoolean(LogConfiguration.get(ALL_LEVEL_SWITCH_NAME)) - : Boolean.FALSE; - case TRACE: - return LogConfiguration.containsKey(TRACE_LEVEL_SWITCH_NAME) - ? Boolean.parseBoolean(LogConfiguration.get(TRACE_LEVEL_SWITCH_NAME)) - : Boolean.FALSE; - case DEBUG: - return LogConfiguration.containsKey(DEBUG_LEVEL_SWITCH_NAME) - ? Boolean.parseBoolean(LogConfiguration.get(DEBUG_LEVEL_SWITCH_NAME)) - : Boolean.FALSE; - case INFO: - return LogConfiguration.containsKey(INFO_LEVEL_SWITCH_NAME) - ? Boolean.parseBoolean(LogConfiguration.get(INFO_LEVEL_SWITCH_NAME)) - : Boolean.FALSE; - case WARN: - return LogConfiguration.containsKey(WARN_LEVEL_SWITCH_NAME) - ? Boolean.parseBoolean(LogConfiguration.get(WARN_LEVEL_SWITCH_NAME)) - : Boolean.FALSE; - case ERROR: - return LogConfiguration.containsKey(ERROR_LEVEL_SWITCH_NAME) - ? Boolean.parseBoolean(LogConfiguration.get(ERROR_LEVEL_SWITCH_NAME)) - : Boolean.FALSE; - case FATAL: - return LogConfiguration.containsKey(FATAL_LEVEL_SWITCH_NAME) - ? Boolean.parseBoolean(LogConfiguration.get(FATAL_LEVEL_SWITCH_NAME)) - : Boolean.FALSE; - case OFF: - return LogConfiguration.containsKey(OFF_LEVEL_SWITCH_NAME) - ? Boolean.parseBoolean(LogConfiguration.get(OFF_LEVEL_SWITCH_NAME)) - : Boolean.FALSE; - default: - return Boolean.FALSE; - } - } - - /** - * Logs of the {@link CapaLogLevel#INFO} level or higher are normally output, but logs of the {@link CapaLogLevel#INFO} level lower are not output. - * - * @return whether the capaLogLevel's priority is equal or more then {@link CapaLogLevel#INFO}. - */ - private static Boolean isLogsLevelClosedWithDefault(CapaLogLevel capaLogLevel) { - return capaLogLevel.getLevel() >= CapaLogLevel.INFO.getLevel(); - } -}