You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by ha...@apache.org on 2021/07/27 05:25:13 UTC

[skywalking] 01/01: Add logging module to load log configuration dynamically

This is an automated email from the ASF dual-hosted git repository.

hanahmily pushed a commit to branch dynamic-logging
in repository https://gitbox.apache.org/repos/asf/skywalking.git

commit 2c61fb239384fe0701e9aead563d2f596397c1b0
Author: Gao Hongtao <ha...@gmail.com>
AuthorDate: Tue Jul 27 13:23:43 2021 +0800

    Add logging module to load log configuration dynamically
    
    Signed-off-by: Gao Hongtao <ha...@gmail.com>
---
 oap-server/pom.xml                                 |  1 +
 oap-server/server-bootstrap/pom.xml                |  5 ++
 .../src/main/resources/application.yml             |  4 +
 oap-server/server-logging/pom.xml                  | 48 +++++++++++
 .../oap/server/logging/module/LoggingModule.java   | 37 ++++++++
 .../oap/server/logging/provider/LoggingConfig.java | 26 ++++++
 .../logging/provider/LoggingConfigWatcher.java     | 58 +++++++++++++
 .../server/logging/provider/LoggingProvider.java   | 98 ++++++++++++++++++++++
 .../logging/provider/log4j/OapConfiguration.java   | 30 +++++++
 .../provider/log4j/OapConfigurationFactory.java    | 55 ++++++++++++
 ...ywalking.oap.server.library.module.ModuleDefine | 19 +++++
 ...alking.oap.server.library.module.ModuleProvider | 19 +++++
 12 files changed, 400 insertions(+)

diff --git a/oap-server/pom.xml b/oap-server/pom.xml
index 6fa7990..e06934a 100755
--- a/oap-server/pom.xml
+++ b/oap-server/pom.xml
@@ -48,6 +48,7 @@
         <module>server-tools</module>
         <module>server-fetcher-plugin</module>
         <module>server-health-checker</module>
+        <module>server-logging</module>
     </modules>
 
     <properties>
diff --git a/oap-server/server-bootstrap/pom.xml b/oap-server/server-bootstrap/pom.xml
index e9dbcca..284be4f 100644
--- a/oap-server/server-bootstrap/pom.xml
+++ b/oap-server/server-bootstrap/pom.xml
@@ -245,6 +245,11 @@
             <artifactId>configuration-nacos</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.skywalking</groupId>
+            <artifactId>server-logging</artifactId>
+            <version>${project.version}</version>
+        </dependency>
     </dependencies>
 
     <build>
diff --git a/oap-server/server-bootstrap/src/main/resources/application.yml b/oap-server/server-bootstrap/src/main/resources/application.yml
index d79a40c..3323569 100755
--- a/oap-server/server-bootstrap/src/main/resources/application.yml
+++ b/oap-server/server-bootstrap/src/main/resources/application.yml
@@ -507,3 +507,7 @@ configuration-discovery:
 receiver-event:
   selector: ${SW_RECEIVER_EVENT:default}
   default:
+
+logging:
+  selector: ${SW_LOGGING:none}
+  default:
diff --git a/oap-server/server-logging/pom.xml b/oap-server/server-logging/pom.xml
new file mode 100644
index 0000000..928d07b
--- /dev/null
+++ b/oap-server/server-logging/pom.xml
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  ~
+  -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>oap-server</artifactId>
+        <groupId>org.apache.skywalking</groupId>
+        <version>8.7.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>server-logging</artifactId>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.skywalking</groupId>
+            <artifactId>server-core</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-core</artifactId>
+        </dependency>
+    </dependencies>
+
+</project>
\ No newline at end of file
diff --git a/oap-server/server-logging/src/main/java/org/apache/skywalking/oap/server/logging/module/LoggingModule.java b/oap-server/server-logging/src/main/java/org/apache/skywalking/oap/server/logging/module/LoggingModule.java
new file mode 100644
index 0000000..c604621
--- /dev/null
+++ b/oap-server/server-logging/src/main/java/org/apache/skywalking/oap/server/logging/module/LoggingModule.java
@@ -0,0 +1,37 @@
+/*
+ * 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 org.apache.skywalking.oap.server.logging.module;
+
+import org.apache.skywalking.oap.server.library.module.ModuleDefine;
+
+/**
+ * LoggingModule intends to provide the dynamic logging
+ */
+public class LoggingModule extends ModuleDefine {
+    public static final String NAME = "logging";
+
+    public LoggingModule() {
+        super(NAME);
+    }
+
+    @Override
+    public Class[] services() {
+        return new Class[0];
+    }
+}
diff --git a/oap-server/server-logging/src/main/java/org/apache/skywalking/oap/server/logging/provider/LoggingConfig.java b/oap-server/server-logging/src/main/java/org/apache/skywalking/oap/server/logging/provider/LoggingConfig.java
new file mode 100644
index 0000000..f7224e5
--- /dev/null
+++ b/oap-server/server-logging/src/main/java/org/apache/skywalking/oap/server/logging/provider/LoggingConfig.java
@@ -0,0 +1,26 @@
+/*
+ * 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 org.apache.skywalking.oap.server.logging.provider;
+
+import lombok.Getter;
+import org.apache.skywalking.oap.server.library.module.ModuleConfig;
+
+@Getter
+public class LoggingConfig extends ModuleConfig {
+}
diff --git a/oap-server/server-logging/src/main/java/org/apache/skywalking/oap/server/logging/provider/LoggingConfigWatcher.java b/oap-server/server-logging/src/main/java/org/apache/skywalking/oap/server/logging/provider/LoggingConfigWatcher.java
new file mode 100644
index 0000000..5cf4c1f
--- /dev/null
+++ b/oap-server/server-logging/src/main/java/org/apache/skywalking/oap/server/logging/provider/LoggingConfigWatcher.java
@@ -0,0 +1,58 @@
+/*
+ * 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 org.apache.skywalking.oap.server.logging.provider;
+
+import java.util.function.Function;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.skywalking.oap.server.configuration.api.ConfigChangeWatcher;
+import org.apache.skywalking.oap.server.library.module.ModuleProvider;
+import org.apache.skywalking.oap.server.logging.module.LoggingModule;
+
+@Slf4j
+public class LoggingConfigWatcher extends ConfigChangeWatcher {
+    private final Function<String, Boolean> configureCaller;
+    private String content;
+
+    public LoggingConfigWatcher(final ModuleProvider provider, final Function<String, Boolean> configureCaller) {
+        super(LoggingModule.NAME, provider, "log4j-xml");
+        this.configureCaller = configureCaller;
+    }
+
+    @Override
+    public void notify(final ConfigChangeEvent value) {
+        if (EventType.DELETE.equals(value.getEventType())) {
+            this.content = "";
+        } else {
+            this.content = value.getNewValue();
+        }
+        try {
+            Boolean applied = this.configureCaller.apply(this.content);
+            if (log.isDebugEnabled() && applied) {
+                log.debug("applied {} B data to logging configuration", this.content.length());
+            }
+        } catch (Throwable t) {
+            log.error("failed to apply configuration to log4j", t);
+        }
+    }
+
+    @Override
+    public String value() {
+        return this.content;
+    }
+}
diff --git a/oap-server/server-logging/src/main/java/org/apache/skywalking/oap/server/logging/provider/LoggingProvider.java b/oap-server/server-logging/src/main/java/org/apache/skywalking/oap/server/logging/provider/LoggingProvider.java
new file mode 100644
index 0000000..b062318
--- /dev/null
+++ b/oap-server/server-logging/src/main/java/org/apache/skywalking/oap/server/logging/provider/LoggingProvider.java
@@ -0,0 +1,98 @@
+/*
+ * 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 org.apache.skywalking.oap.server.logging.provider;
+
+import com.google.common.base.Strings;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.ConfigurationSource;
+import org.apache.skywalking.oap.server.configuration.api.ConfigurationModule;
+import org.apache.skywalking.oap.server.configuration.api.DynamicConfigurationService;
+import org.apache.skywalking.oap.server.library.module.ModuleConfig;
+import org.apache.skywalking.oap.server.library.module.ModuleDefine;
+import org.apache.skywalking.oap.server.library.module.ModuleProvider;
+import org.apache.skywalking.oap.server.library.module.ModuleStartException;
+import org.apache.skywalking.oap.server.library.module.ServiceNotProvidedException;
+import org.apache.skywalking.oap.server.logging.module.LoggingModule;
+import org.apache.skywalking.oap.server.logging.provider.log4j.OapConfiguration;
+
+public class LoggingProvider extends ModuleProvider {
+
+    private LoggingConfigWatcher configWatcher;
+
+    @Override
+    public String name() {
+        return "default";
+    }
+
+    @Override
+    public Class<? extends ModuleDefine> module() {
+        return LoggingModule.class;
+    }
+
+    @Override
+    public ModuleConfig createConfigBeanIfAbsent() {
+        return new LoggingConfig();
+    }
+
+    @Override
+    public void prepare() throws ServiceNotProvidedException, ModuleStartException {
+        final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
+        final OapConfiguration originConfiguration = (OapConfiguration) ctx.getConfiguration();
+        configWatcher = new LoggingConfigWatcher(this, content -> {
+            if (Strings.isNullOrEmpty(content)) {
+                if (ctx.getConfiguration().equals(originConfiguration)) {
+                    ctx.onChange(originConfiguration);
+                    return true;
+                }
+                return false;
+            }
+            OapConfiguration oc;
+            try {
+                oc = new OapConfiguration(ctx, new ConfigurationSource(new ByteArrayInputStream(content.getBytes())));
+            } catch (IOException e) {
+                throw new RuntimeException("failed to parse string from configuration center", e);
+            }
+            oc.initialize();
+            ctx.onChange(oc);
+            return true;
+        });
+    }
+
+    @Override
+    public void start() throws ServiceNotProvidedException, ModuleStartException {
+    }
+
+    @Override
+    public void notifyAfterCompleted() throws ServiceNotProvidedException, ModuleStartException {
+        DynamicConfigurationService dynamicConfigurationService = getManager().find(ConfigurationModule.NAME)
+                                                                              .provider()
+                                                                              .getService(
+                                                                                  DynamicConfigurationService.class);
+        dynamicConfigurationService.registerConfigChangeWatcher(configWatcher);
+
+    }
+
+    @Override
+    public String[] requiredModules() {
+        return new String[]{ConfigurationModule.NAME};
+    }
+}
diff --git a/oap-server/server-logging/src/main/java/org/apache/skywalking/oap/server/logging/provider/log4j/OapConfiguration.java b/oap-server/server-logging/src/main/java/org/apache/skywalking/oap/server/logging/provider/log4j/OapConfiguration.java
new file mode 100644
index 0000000..116503a
--- /dev/null
+++ b/oap-server/server-logging/src/main/java/org/apache/skywalking/oap/server/logging/provider/log4j/OapConfiguration.java
@@ -0,0 +1,30 @@
+/*
+ * 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 org.apache.skywalking.oap.server.logging.provider.log4j;
+
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.ConfigurationSource;
+import org.apache.logging.log4j.core.config.xml.XmlConfiguration;
+
+public class OapConfiguration extends XmlConfiguration {
+    public OapConfiguration(final LoggerContext loggerContext,
+                            final ConfigurationSource configSource) {
+        super(loggerContext, configSource);
+    }
+}
diff --git a/oap-server/server-logging/src/main/java/org/apache/skywalking/oap/server/logging/provider/log4j/OapConfigurationFactory.java b/oap-server/server-logging/src/main/java/org/apache/skywalking/oap/server/logging/provider/log4j/OapConfigurationFactory.java
new file mode 100644
index 0000000..8e3e59f
--- /dev/null
+++ b/oap-server/server-logging/src/main/java/org/apache/skywalking/oap/server/logging/provider/log4j/OapConfigurationFactory.java
@@ -0,0 +1,55 @@
+/*
+ * 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 org.apache.skywalking.oap.server.logging.provider.log4j;
+
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.ConfigurationFactory;
+import org.apache.logging.log4j.core.config.ConfigurationSource;
+import org.apache.logging.log4j.core.config.Order;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+
+@Plugin(name = "OapConfigurationFactory", category = "ConfigurationFactory")
+@Order(10)
+public class OapConfigurationFactory extends ConfigurationFactory {
+    /**
+     * Valid file extensions for XML files.
+     */
+    public static final String[] SUFFIXES = new String[] {".xml", "*"};
+
+    /**
+     * Returns the file suffixes for XML files.
+     * @return An array of File extensions.
+     */
+    @Override
+    public String[] getSupportedTypes() {
+        return SUFFIXES;
+    }
+
+    /**
+     * Return the configuration.
+     * @param loggerContext The logger context
+     * @param source The input source
+     * @return the configuration
+     */
+    @Override
+    public Configuration getConfiguration(final LoggerContext loggerContext, final ConfigurationSource source) {
+        return new OapConfiguration(loggerContext, source);
+    }
+}
diff --git a/oap-server/server-logging/src/main/resources/META-INF/services/org.apache.skywalking.oap.server.library.module.ModuleDefine b/oap-server/server-logging/src/main/resources/META-INF/services/org.apache.skywalking.oap.server.library.module.ModuleDefine
new file mode 100644
index 0000000..2f9eb94
--- /dev/null
+++ b/oap-server/server-logging/src/main/resources/META-INF/services/org.apache.skywalking.oap.server.library.module.ModuleDefine
@@ -0,0 +1,19 @@
+#
+# 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.
+#
+#
+
+org.apache.skywalking.oap.server.logging.module.LoggingModule
diff --git a/oap-server/server-logging/src/main/resources/META-INF/services/org.apache.skywalking.oap.server.library.module.ModuleProvider b/oap-server/server-logging/src/main/resources/META-INF/services/org.apache.skywalking.oap.server.library.module.ModuleProvider
new file mode 100644
index 0000000..32b75fc
--- /dev/null
+++ b/oap-server/server-logging/src/main/resources/META-INF/services/org.apache.skywalking.oap.server.library.module.ModuleProvider
@@ -0,0 +1,19 @@
+#
+# 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.
+#
+#
+
+org.apache.skywalking.oap.server.logging.provider.LoggingProvider