Fixed BUG-42577: Adding remove log archive policy to prevent indefinite growth.
authorJavier Armendáriz <javier.armendariz@openbravo.com>
Wed, 18 Dec 2019 13:15:18 +0100
changeset 36896 005eb39e9993
parent 36895 41b7178a6bd7
child 36897 7a648e0c57d3
Fixed BUG 42577: Adding remove log archive policy to prevent indefinite growth.

Using current log4j config, only files under the same period of time are limited
(i.e. when logfile is bigger than 100MB, there can be max 30 log archives for the
same day).

The new rule added applies to log archives with the format openbravo-*.log.gz and
will remove the oldest archive until there are max 30 files. This process will be
performed every time a log rollover occurs.

This commit also adds a build validation step that overrides the current config
with the updated one _only_ if its a exact copy of the previous default config.
config/log4j2-web.xml.template
src-util/buildvalidation/build/classes/org/openbravo/buildvalidation/CopyLog4jConfigurationFromTemplates.class
src-util/buildvalidation/src/org/openbravo/buildvalidation/CopyLog4jConfigurationFromTemplates.java
--- a/config/log4j2-web.xml.template	Wed Dec 18 10:30:37 2019 +0530
+++ b/config/log4j2-web.xml.template	Wed Dec 18 13:15:18 2019 +0100
@@ -12,7 +12,7 @@
  * under the License.
  * The Original Code is Openbravo ERP.
  * The Initial Developer of the Original Code is Openbravo SLU
- * All portions are Copyright (C) 2018 Openbravo SLU
+ * All portions are Copyright (C) 2018-2019 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -46,7 +46,13 @@
         <SizeBasedTriggeringPolicy size="100MB" />
         <TimeBasedTriggeringPolicy />
       </Policies>
-      <DefaultRolloverStrategy max="30"/>
+      <DefaultRolloverStrategy max="30">
+        <Delete basePath="${logDir}">
+          <IfFileName glob="openbravo-*.log.gz">
+            <IfAccumulatedFileCount exceeds="30"/>
+          </IfFileName>
+        </Delete>
+      </DefaultRolloverStrategy>
     </RollingFile>
 
     <Console name="Console" target="SYSTEM_OUT">
Binary file src-util/buildvalidation/build/classes/org/openbravo/buildvalidation/CopyLog4jConfigurationFromTemplates.class has changed
--- a/src-util/buildvalidation/src/org/openbravo/buildvalidation/CopyLog4jConfigurationFromTemplates.java	Wed Dec 18 10:30:37 2019 +0530
+++ b/src-util/buildvalidation/src/org/openbravo/buildvalidation/CopyLog4jConfigurationFromTemplates.java	Wed Dec 18 13:15:18 2019 +0100
@@ -22,7 +22,10 @@
 import java.nio.file.NoSuchFileException;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
+import java.security.MessageDigest;
 import java.util.ArrayList;
+import java.util.Base64;
 import java.util.List;
 
 /**
@@ -37,6 +40,8 @@
   private static final String LOG4J_WEB_CONF_FILE = "log4j2-web.xml";
   private static final String LOG4J_TEST_CONF_FILE = "log4j2-test.xml";
 
+  private static final String DEFECTIVE_CONFIG_MD5_HASH_AS_BASE_64 = "6iGQxrhHHGR7JVS7PKS0mw==";
+
   @Override
   public List<String> execute() {
     try {
@@ -44,6 +49,7 @@
       copyFromTemplateFile(sourcePath + CONFIG_DIR + LOG4J_CONF_FILE);
       copyFromTemplateFile(sourcePath + CONFIG_DIR + LOG4J_WEB_CONF_FILE);
       copyFromTemplateFile(sourcePath + TEST_SRC_DIR + LOG4J_TEST_CONF_FILE);
+      replaceDefectiveLog4jWebConfig(sourcePath + CONFIG_DIR + LOG4J_WEB_CONF_FILE);
     } catch (Exception e) {
       System.out.println(
           "Copy log4j config from templates failed: Log4j may not be properly configured. Please check your configuration files manually.");
@@ -52,6 +58,29 @@
     return new ArrayList<>();
   }
 
+  /**
+   * Replace existing log4j-web config with the current template only if it is an exact copy of an
+   * older version that has a bug that lets log archives grow indefinitely.
+   *
+   * See issue https://issues.openbravo.com/view.php?id=42556 for more info.
+   */
+  private void replaceDefectiveLog4jWebConfig(String targetPath) throws Exception {
+    Path target = Paths.get(targetPath);
+
+    if (Files.exists(target) && fileMatchesMd5(target, DEFECTIVE_CONFIG_MD5_HASH_AS_BASE_64)) {
+      Path source = Paths.get(targetPath + ".template");
+      Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
+      System.out.println(targetPath
+          + " is overriden with template file content. Please check this configuration is correct.");
+    }
+  }
+
+  private boolean fileMatchesMd5(Path file, String md5sumBase64) throws Exception {
+    byte[] fileHash = MessageDigest.getInstance("MD5").digest(Files.readAllBytes(file));
+    String fileMd5 = Base64.getEncoder().encodeToString(fileHash);
+    return fileMd5.equals(md5sumBase64);
+  }
+
   private void copyFromTemplateFile(String targetPath) throws Exception {
     Path source = Paths.get(targetPath + ".template");
     Path target = Paths.get(targetPath);