[ 
https://issues.apache.org/jira/browse/SCB-752?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16547531#comment-16547531
 ] 

ASF GitHub Bot commented on SCB-752:
------------------------------------

liubao68 closed pull request #813: [SCB-752] provide a configuration to 
determine whether to output merged.log4j.properties
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/813
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/Log4jUtils.java
 
b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/Log4jUtils.java
index d0bcffc3a..9e95fcab3 100644
--- 
a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/Log4jUtils.java
+++ 
b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/Log4jUtils.java
@@ -19,7 +19,6 @@
 
 import java.io.File;
 import java.io.IOException;
-import java.net.URISyntaxException;
 import java.util.Arrays;
 import java.util.Comparator;
 import java.util.List;
@@ -35,8 +34,13 @@
 import org.springframework.core.io.Resource;
 
 public final class Log4jUtils {
+
   private static final String MERGED_FILE = "merged.log4j.properties";
 
+  public static final String OUTPUT_CONFIG_ENABLED = 
"log4j.logger.outputConfig.enabled";
+
+  public static final String OUTPUT_CONFIG_ENABLED_TRUE = "true";
+
   // spring boot的包装中会重复调用init,需要规避一下
   private static boolean inited = false;
 
@@ -72,13 +76,17 @@ public static void init(List<String> locationPatterns) 
throws Exception {
       PropertyConfigurator.configure(properties);
       inited = true;
 
-      // 如果最高优先级的文件是在磁盘上,且有写权限,则将merge的结果输出到该目录,方便维护时观察生效的参数
-      outputFile(loader.getFoundResList(), properties);
+      if (OUTPUT_CONFIG_ENABLED_TRUE.equals(
+          properties.getProperty(OUTPUT_CONFIG_ENABLED, 
OUTPUT_CONFIG_ENABLED_TRUE))) {
+        // If the property file with the highest priority is on a hard 
disk(not in a jar package)
+        // and we have write access, output the merged property file for the 
purpose of debugging
+        outputFile(loader.getFoundResList(), properties);
+      }
     }
   }
 
   private static void outputFile(List<Resource> resList,
-      Properties properties) throws IOException, URISyntaxException {
+      Properties properties) throws IOException {
     //不可以作为class的变量初始化,因为在outputFile前一句log机制才初始化完成的
     //must create org.slf4j.impl.Log4jLoggerAdapter by LoggerExtFactory
     //in order to redefine Log4jLoggerAdapter before other class load 
Log4jLoggerAdapter
diff --git 
a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/Log4jUtilsTest.java
 
b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/Log4jUtilsTest.java
new file mode 100644
index 000000000..dba8e00a8
--- /dev/null
+++ 
b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/Log4jUtilsTest.java
@@ -0,0 +1,136 @@
+/*
+ * 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.servicecomb.foundation.common.utils;
+
+import static org.junit.Assert.fail;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
+import javax.xml.ws.Holder;
+
+import org.apache.log4j.PropertyConfigurator;
+import org.apache.servicecomb.foundation.common.config.impl.PropertiesLoader;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.core.io.Resource;
+
+import mockit.Deencapsulation;
+import mockit.Mock;
+import mockit.MockUp;
+
+public class Log4jUtilsTest {
+
+  @Before
+  public void before() {
+    Deencapsulation.setField(Log4jUtils.class, "inited", false);
+  }
+
+  @Test
+  public void init() {
+    Holder<Boolean> propertiesLoaded = new Holder<>(false);
+    Holder<Boolean> logConfigured = new Holder<>(false);
+    Holder<Boolean> mergedFileWritten = new Holder<>(false);
+    final Properties logProperties = new Properties();
+    logProperties.setProperty("paas.logs.file", "cse.log");
+    final ArrayList<Resource> logResList = new ArrayList<>();
+    new MockUp<PropertiesLoader>() {
+      @Mock
+      Properties load() {
+        propertiesLoaded.value = true;
+        return logProperties;
+      }
+
+      @Mock
+      List<Resource> getFoundResList() {
+        return logResList;
+      }
+    };
+    new MockUp<PropertyConfigurator>() {
+      @Mock
+      void configure(Properties properties) {
+        logConfigured.value = true;
+        Assert.assertSame(properties, logProperties);
+      }
+    };
+    new MockUp<Log4jUtils>() {
+      @Mock
+      void outputFile(List<Resource> resList, Properties properties) {
+        mergedFileWritten.value = true;
+        Assert.assertSame(logResList, resList);
+        Assert.assertSame(logProperties, properties);
+      }
+    };
+
+    Assert.assertFalse(Deencapsulation.getField(Log4jUtils.class, "inited"));
+    try {
+      Log4jUtils.init();
+    } catch (Exception e) {
+      fail(e.getMessage());
+    }
+    Assert.assertTrue(Deencapsulation.getField(Log4jUtils.class, "inited"));
+    Assert.assertTrue(propertiesLoaded.value);
+    Assert.assertTrue(logConfigured.value);
+    Assert.assertTrue(mergedFileWritten.value);
+  }
+
+  @Test
+  public void initOnMergedFileOutputDisabled() {
+    Holder<Boolean> propertiesLoaded = new Holder<>(false);
+    Holder<Boolean> logConfigured = new Holder<>(false);
+    Holder<Boolean> mergedFileWritten = new Holder<>(false);
+    final Properties logProperties = new Properties();
+    logProperties.setProperty("log4j.logger.outputConfig.enabled", "false");
+    final ArrayList<Resource> logResList = new ArrayList<>();
+    new MockUp<PropertiesLoader>() {
+      @Mock
+      Properties load() {
+        propertiesLoaded.value = true;
+        return logProperties;
+      }
+    };
+    new MockUp<PropertyConfigurator>() {
+      @Mock
+      void configure(Properties properties) {
+        logConfigured.value = true;
+        Assert.assertSame(properties, logProperties);
+      }
+    };
+    new MockUp<Log4jUtils>() {
+      @Mock
+      void outputFile(List<Resource> resList, Properties properties) {
+        mergedFileWritten.value = true;
+        Assert.assertSame(logResList, resList);
+        Assert.assertSame(logProperties, properties);
+      }
+    };
+
+    Assert.assertFalse(Deencapsulation.getField(Log4jUtils.class, "inited"));
+    try {
+      Log4jUtils.init();
+    } catch (Exception e) {
+      fail(e.getMessage());
+    }
+    Assert.assertTrue(Deencapsulation.getField(Log4jUtils.class, "inited"));
+    Assert.assertTrue(propertiesLoaded.value);
+    Assert.assertTrue(logConfigured.value);
+    Assert.assertFalse(mergedFileWritten.value);
+  }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


> User can switch off outputting merged log4j property file
> ---------------------------------------------------------
>
>                 Key: SCB-752
>                 URL: https://issues.apache.org/jira/browse/SCB-752
>             Project: Apache ServiceComb
>          Issue Type: New Feature
>          Components: Java-Chassis
>            Reporter: YaoHaishi
>            Assignee: YaoHaishi
>            Priority: Minor
>             Fix For: java-chassis-1.0.0
>
>
> On service start up, ServiceComb-java-chassis will try to output a merged 
> log4j property file onto hard disk and users cannot switch off this function.
> Maybe we can provide a configuration to let users decide whether this 
> function should work.
> Configuration key is 
> {code:java}
> log4j.logger.outputConfig.enabled
> {code}
> this is configured in log4j.properties file and the value is true or false.
> Default value is true.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to