You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2022/06/09 10:54:41 UTC

[dubbo] branch 3.0 updated: [3.0] Add some logger related command (#10132)

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

albumenj pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.0 by this push:
     new 372f6a697a [3.0] Add some logger related command (#10132)
372f6a697a is described below

commit 372f6a697a0a4ebfdbf637c5f7e3dde8662bbe53
Author: Albumen Kevin <jh...@gmail.com>
AuthorDate: Thu Jun 9 18:54:27 2022 +0800

    [3.0] Add some logger related command (#10132)
---
 .../apache/dubbo/common/logger/LoggerFactory.java  | 60 ++++++++++++++++++--
 .../apache/dubbo/qos/command/impl/LoggerInfo.java  | 37 ++++++++++++
 .../dubbo/qos/command/impl/SwitchLogLevel.java     | 66 ++++++++++++++++++++++
 .../dubbo/qos/command/impl/SwitchLogger.java       | 39 +++++++++++++
 .../org.apache.dubbo.qos.command.BaseCommand       |  3 +
 .../dubbo/qos/command/util/CommandHelperTest.java  |  6 ++
 6 files changed, 205 insertions(+), 6 deletions(-)

diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/logger/LoggerFactory.java b/dubbo-common/src/main/java/org/apache/dubbo/common/logger/LoggerFactory.java
index 8622e3368d..9f787c51b2 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/logger/LoggerFactory.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/logger/LoggerFactory.java
@@ -26,6 +26,8 @@ import org.apache.dubbo.rpc.model.FrameworkModel;
 
 import java.io.File;
 import java.util.Arrays;
+import java.util.HashMap;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
@@ -60,15 +62,17 @@ public class LoggerFactory {
                 break;
             default:
                 List<Class<? extends LoggerAdapter>> candidates = Arrays.asList(
-                        Log4jLoggerAdapter.class,
-                        Slf4jLoggerAdapter.class,
-                        Log4j2LoggerAdapter.class,
-                        JclLoggerAdapter.class,
-                        JdkLoggerAdapter.class
+                    Log4jLoggerAdapter.class,
+                    Slf4jLoggerAdapter.class,
+                    Log4j2LoggerAdapter.class,
+                    JclLoggerAdapter.class,
+                    JdkLoggerAdapter.class
                 );
                 for (Class<? extends LoggerAdapter> clazz : candidates) {
                     try {
-                        setLoggerAdapter(clazz.newInstance());
+                        LoggerAdapter loggerAdapter = clazz.newInstance();
+                        loggerAdapter.getLogger(LoggerFactory.class);
+                        setLoggerAdapter(loggerAdapter);
                         break;
                     } catch (Throwable ignored) {
                     }
@@ -150,4 +154,48 @@ public class LoggerFactory {
         return LOGGER_ADAPTER.getFile();
     }
 
+    /**
+     * Get the available adapter names
+     *
+     * @return available adapter names
+     */
+    public static List<String> getAvailableAdapter() {
+        Map<Class<? extends LoggerAdapter>, String> candidates = new HashMap<>();
+        candidates.put(Log4jLoggerAdapter.class, "log4j");
+        candidates.put(Slf4jLoggerAdapter.class, "slf4j");
+        candidates.put(Log4j2LoggerAdapter.class, "log4j2");
+        candidates.put(JclLoggerAdapter.class, "jcl");
+        candidates.put(JdkLoggerAdapter.class, "jdk");
+        List<String> result = new LinkedList<>();
+        for (Map.Entry<Class<? extends LoggerAdapter>, String> entry : candidates.entrySet()) {
+            try {
+                LoggerAdapter loggerAdapter = entry.getKey().newInstance();
+                loggerAdapter.getLogger(LoggerFactory.class);
+                result.add(entry.getValue());
+            } catch (Throwable ignored) {
+            }
+        }
+        return result;
+    }
+
+    /**
+     * Get the current adapter name
+     *
+     * @return current adapter name
+     */
+    public static String getCurrentAdapter() {
+        Map<Class<? extends LoggerAdapter>, String> candidates = new HashMap<>();
+        candidates.put(Log4jLoggerAdapter.class, "log4j");
+        candidates.put(Slf4jLoggerAdapter.class, "slf4j");
+        candidates.put(Log4j2LoggerAdapter.class, "log4j2");
+        candidates.put(JclLoggerAdapter.class, "jcl");
+        candidates.put(JdkLoggerAdapter.class, "jdk");
+
+        String name = candidates.get(LOGGER_ADAPTER.getClass());
+        if (name == null) {
+            name = LOGGER_ADAPTER.getClass().getSimpleName();
+        }
+        return name;
+    }
+
 }
diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/LoggerInfo.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/LoggerInfo.java
new file mode 100644
index 0000000000..ba08675a35
--- /dev/null
+++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/LoggerInfo.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.dubbo.qos.command.impl;
+
+import org.apache.dubbo.common.logger.Level;
+import org.apache.dubbo.common.logger.LoggerFactory;
+import org.apache.dubbo.qos.command.BaseCommand;
+import org.apache.dubbo.qos.command.CommandContext;
+import org.apache.dubbo.qos.command.annotation.Cmd;
+
+@Cmd(name = "loggerInfo", summary = "Print logger info", example = {
+    "loggerInfo"
+})
+public class LoggerInfo implements BaseCommand {
+    @Override
+    public String execute(CommandContext commandContext, String[] args) {
+        String availableAdapters = String.join(", ", LoggerFactory.getAvailableAdapter().toArray(new String[0]));
+        String currentAdapter = LoggerFactory.getCurrentAdapter();
+        Level level = LoggerFactory.getLevel();
+
+        return "Available logger adapters: [" + availableAdapters + "]. Current Adapter: [" + currentAdapter + "]. Log level: " + level.name();
+    }
+}
diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/SwitchLogLevel.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/SwitchLogLevel.java
new file mode 100644
index 0000000000..930b6dd865
--- /dev/null
+++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/SwitchLogLevel.java
@@ -0,0 +1,66 @@
+/*
+ * 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.dubbo.qos.command.impl;
+
+import org.apache.dubbo.common.logger.Level;
+import org.apache.dubbo.common.logger.LoggerFactory;
+import org.apache.dubbo.qos.command.BaseCommand;
+import org.apache.dubbo.qos.command.CommandContext;
+import org.apache.dubbo.qos.command.annotation.Cmd;
+
+import java.util.Locale;
+
+@Cmd(name = "switchLogLevel", summary = "Switch log level", example = {
+    "switchLogLevel info"
+})
+public class SwitchLogLevel implements BaseCommand {
+    @Override
+    public String execute(CommandContext commandContext, String[] args) {
+        if (args.length != 1) {
+            return "Unexpected argument length.";
+        }
+        Level level;
+        switch (args[0]) {
+            case "0":
+                level = Level.ALL;
+                break;
+            case "1":
+                level = Level.TRACE;
+                break;
+            case "2":
+                level = Level.DEBUG;
+                break;
+            case "3":
+                level = Level.INFO;
+                break;
+            case "4":
+                level = Level.WARN;
+                break;
+            case "5":
+                level = Level.ERROR;
+                break;
+            case "6":
+                level = Level.OFF;
+                break;
+            default:
+                level = Level.valueOf(args[0].toUpperCase(Locale.ROOT));
+                break;
+        }
+        LoggerFactory.setLevel(level);
+        return "OK";
+    }
+}
diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/SwitchLogger.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/SwitchLogger.java
new file mode 100644
index 0000000000..0908a4b8b2
--- /dev/null
+++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/SwitchLogger.java
@@ -0,0 +1,39 @@
+/*
+ * 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.dubbo.qos.command.impl;
+
+import org.apache.dubbo.common.logger.LoggerFactory;
+import org.apache.dubbo.qos.command.BaseCommand;
+import org.apache.dubbo.qos.command.CommandContext;
+import org.apache.dubbo.qos.command.annotation.Cmd;
+import org.apache.dubbo.rpc.model.FrameworkModel;
+
+@Cmd(name = "switchLogger", summary = "Switch logger", example = {
+    "switchLogger slf4j"
+})
+public class SwitchLogger implements BaseCommand {
+    private FrameworkModel frameworkModel;
+
+    @Override
+    public String execute(CommandContext commandContext, String[] args) {
+        if (args.length != 1) {
+            return "Unexpected argument length.";
+        }
+        LoggerFactory.setLoggerAdapter(frameworkModel, args[0]);
+        return "OK";
+    }
+}
diff --git a/dubbo-plugin/dubbo-qos/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.qos.command.BaseCommand b/dubbo-plugin/dubbo-qos/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.qos.command.BaseCommand
index 2e68fed8f0..9611af4838 100644
--- a/dubbo-plugin/dubbo-qos/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.qos.command.BaseCommand
+++ b/dubbo-plugin/dubbo-qos/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.qos.command.BaseCommand
@@ -29,3 +29,6 @@ getEnabledRouterSnapshot=org.apache.dubbo.qos.command.impl.GetEnabledRouterSnaps
 enableRouterSnapshot=org.apache.dubbo.qos.command.impl.EnableRouterSnapshot
 disableRouterSnapshot=org.apache.dubbo.qos.command.impl.DisableRouterSnapshot
 getRecentRouterSnapshot=org.apache.dubbo.qos.command.impl.GetRecentRouterSnapshot
+loggerInfo=org.apache.dubbo.qos.command.impl.LoggerInfo
+switchLogger=org.apache.dubbo.qos.command.impl.SwitchLogger
+switchLogLevel=org.apache.dubbo.qos.command.impl.SwitchLogLevel
diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/util/CommandHelperTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/util/CommandHelperTest.java
index ad95a7456f..1021f267b1 100644
--- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/util/CommandHelperTest.java
+++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/util/CommandHelperTest.java
@@ -31,6 +31,7 @@ import org.apache.dubbo.qos.command.impl.GetRouterSnapshot;
 import org.apache.dubbo.qos.command.impl.Help;
 import org.apache.dubbo.qos.command.impl.InvokeTelnet;
 import org.apache.dubbo.qos.command.impl.Live;
+import org.apache.dubbo.qos.command.impl.LoggerInfo;
 import org.apache.dubbo.qos.command.impl.Ls;
 import org.apache.dubbo.qos.command.impl.Offline;
 import org.apache.dubbo.qos.command.impl.OfflineApp;
@@ -47,6 +48,8 @@ import org.apache.dubbo.qos.command.impl.SelectTelnet;
 import org.apache.dubbo.qos.command.impl.SetProfilerWarnPercent;
 import org.apache.dubbo.qos.command.impl.ShutdownTelnet;
 import org.apache.dubbo.qos.command.impl.Startup;
+import org.apache.dubbo.qos.command.impl.SwitchLogLevel;
+import org.apache.dubbo.qos.command.impl.SwitchLogger;
 import org.apache.dubbo.qos.command.impl.Version;
 import org.apache.dubbo.rpc.model.FrameworkModel;
 
@@ -109,6 +112,9 @@ public class CommandHelperTest {
         expectedClasses.add(EnableRouterSnapshot.class);
         expectedClasses.add(DisableRouterSnapshot.class);
         expectedClasses.add(GetRecentRouterSnapshot.class);
+        expectedClasses.add(LoggerInfo.class);
+        expectedClasses.add(SwitchLogger.class);
+        expectedClasses.add(SwitchLogLevel.class);
         assertThat(classes, containsInAnyOrder(expectedClasses.toArray(new Class<?>[0])));
     }