You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by GitBox <gi...@apache.org> on 2020/12/07 06:44:52 UTC

[GitHub] [skywalking] muse-dev[bot] commented on a change in pull request #5914: add error log(log4j,logback) to span log event

muse-dev[bot] commented on a change in pull request #5914:
URL: https://github.com/apache/skywalking/pull/5914#discussion_r537266222



##########
File path: apm-sniffer/optional-plugins/logger-plugin/src/main/java/org/apache/skywalking/apm/plugin/logger/ContextConfig.java
##########
@@ -0,0 +1,246 @@
+/*
+ * 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.apm.plugin.logger;
+
+import lombok.Setter;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.AllArgsConstructor;
+import org.apache.skywalking.apm.agent.core.boot.AgentPackageNotFoundException;
+import org.apache.skywalking.apm.agent.core.boot.AgentPackagePath;
+import org.apache.skywalking.apm.agent.core.context.ContextManager;
+import org.apache.skywalking.apm.agent.core.logging.api.ILog;
+import org.apache.skywalking.apm.agent.core.logging.api.LogManager;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.stream.Collectors;
+
+/**
+ * contains all config of the logger plugin.
+ */
+@Getter
+public class ContextConfig {
+
+    private final LoggerConfig logbackConfig;
+    private final LoggerConfig log4jConfig;
+    private final LoggerConfig log4j2Config;
+
+    private ContextConfig(LoggerConfig logbackConfig, LoggerConfig log4jConfig, LoggerConfig log4j2Config) {
+        this.logbackConfig = logbackConfig;
+        this.log4jConfig = log4jConfig;
+        this.log4j2Config = log4j2Config;
+    }
+
+    public static ContextConfig getInstance() {
+        return HolderContextConfig.INSTANCE;
+    }
+
+    //use singleton
+    private static class HolderContextConfig {
+        private final static ContextConfig INSTANCE = initContextConfig();
+
+        private static ContextConfig initContextConfig() {
+            // judge whether has config file
+            final ILog logger = LogManager.getLogger(HolderContextConfig.class);
+            LoggerConfig logbackConfig = null, log4jConfig = null, log4j2Config = null;
+            File configFile = null;
+            try {
+                configFile = new File(AgentPackagePath.getPath(), "/config/logger-plugin/logconfig.properties");
+            } catch (AgentPackageNotFoundException e) {
+                logger.error("Agent package not found.", e);
+            }
+            // not has config file, make config default
+            if (configFile == null || !configFile.exists()) {
+                List<String> packages = new ArrayList<>();
+                packages.add("*");
+                logbackConfig = new LoggerConfig("logback", packages, LogLevel.ERROR);
+                log4jConfig = new LoggerConfig("log4j", packages, LogLevel.ERROR);
+                log4j2Config = new LoggerConfig("log4j2", packages, LogLevel.ERROR);
+            } else {
+                // use config file to init ContextConfig
+                try (FileInputStream configFileInputStream = new FileInputStream(configFile)) {
+                    List<LoggerConfig> configs = parseConfigFile(configFileInputStream);
+                    // initialization of variables which are described in config file
+                    for (LoggerConfig loggerConfig : configs) {
+                        if ("logback".equals(loggerConfig.getName())) {
+                            logbackConfig = fillLoggerConfig(loggerConfig);
+                        } else if ("log4j".equals(loggerConfig.getName())) {
+                            log4jConfig = fillLoggerConfig(loggerConfig);
+                        } else if ("log4j2".equals(loggerConfig.getName())) {
+                            log4j2Config = fillLoggerConfig(loggerConfig);
+                        } else {
+                            logger.error("logconfig.properties was not configured properly.Please check again.");
+                            return null;
+                        }
+                    }
+                } catch (IOException e) {
+                    logger.error("Logger plugin initialized failure.Please check again.", e);
+                }
+            }
+            if (logbackConfig.level == LogLevel.FATAL) {

Review comment:
       *NULL_DEREFERENCE:*  object `logbackConfig` last assigned on line 69 could be null and is dereferenced at line 104.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org