Fix breakage introduced by logback bump 60/51260/1
authorRobert Varga <rovarga@cisco.com>
Tue, 31 Jan 2017 15:53:45 +0000 (16:53 +0100)
committerRobert Varga <rovarga@cisco.com>
Tue, 31 Jan 2017 15:57:15 +0000 (16:57 +0100)
This is an ugly fix to adjust for changes in logmack 1.1.9.

Change-Id: I424687fe757e5d7cb4525b5954d283f762a0d011
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/config/logback-config/src/main/java/org/opendaylight/controller/config/yang/logback/config/ContextSetterImpl.java
opendaylight/config/logback-config/src/main/java/org/opendaylight/controller/config/yang/logback/config/LogbackModuleFactory.java
opendaylight/config/logback-config/src/test/java/org/opendaylight/controller/config/yang/logback/config/LogbackModuleWithInitialConfigurationTest.java

index 9abf645..cbb2182 100644 (file)
@@ -16,6 +16,7 @@ import ch.qos.logback.core.Appender;
 import ch.qos.logback.core.rolling.FixedWindowRollingPolicy;
 import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy;
 import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;
+import ch.qos.logback.core.util.FileSize;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Sets;
@@ -215,7 +216,7 @@ public class ContextSetterImpl implements ContextSetter, Closeable {
             }
             SizeBasedTriggeringPolicy<ILoggingEvent> triggeringPolicy = new SizeBasedTriggeringPolicy<>();
             triggeringPolicy.setContext(context);
-            triggeringPolicy.setMaxFileSize(appender.getMaxFileSize());
+            triggeringPolicy.setMaxFileSize(FileSize.valueOf(appender.getMaxFileSize()));
             triggeringPolicy.start();
             app.setTriggeringPolicy(triggeringPolicy);
             app.setName(appender.getName());
index bd19d5e..b79e3d3 100644 (file)
@@ -25,9 +25,12 @@ import ch.qos.logback.core.Appender;
 import ch.qos.logback.core.rolling.FixedWindowRollingPolicy;
 import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy;
 import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;
+import ch.qos.logback.core.util.FileSize;
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
+import java.lang.reflect.Field;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
@@ -136,7 +139,7 @@ public class LogbackModuleFactory extends
                     }
                     SizeBasedTriggeringPolicy<ILoggingEvent> triggeringPolicy = (SizeBasedTriggeringPolicy<ILoggingEvent>) rollingApp
                             .getTriggeringPolicy();
-                    app.setMaxFileSize(triggeringPolicy.getMaxFileSize());
+                    app.setMaxFileSize(getMaxFileSize(triggeringPolicy).toString());
                     app.setName(rollingApp.getName());
                     this.rollingDTOs.put(rollingApp.getName(), app);
                 } else if (appender instanceof ch.qos.logback.core.FileAppender<?>) {
@@ -240,4 +243,26 @@ public class LogbackModuleFactory extends
         return defaultModules;
     }
 
+    // Ugly hack to deal with logback changing its api
+    private static final Field MAX_FILE_SIZE_FIELD;
+    static {
+        Field f;
+        try {
+            f = SizeBasedTriggeringPolicy.class.getDeclaredField("maxFileSize");
+            f.setAccessible(true);
+        } catch (NoSuchFieldException | SecurityException e) {
+            throw new ExceptionInInitializerError(e);
+        }
+
+        MAX_FILE_SIZE_FIELD = f;
+    }
+
+    @VisibleForTesting
+    static FileSize getMaxFileSize(SizeBasedTriggeringPolicy<?> policy) {
+        try {
+            return (FileSize) MAX_FILE_SIZE_FIELD.get(policy);
+        } catch (IllegalArgumentException | IllegalAccessException e) {
+            throw new IllegalStateException("Cannot get maxFileSize field", e);
+        }
+    }
 }
index f467bc5..eeed459 100644 (file)
@@ -173,14 +173,14 @@ public class LogbackModuleWithInitialConfigurationTest extends AbstractConfigTes
         rollingApp.setMinIndex(rollingPolicy.getMinIndex());
         SizeBasedTriggeringPolicy<ILoggingEvent> triggeringPolicy = (SizeBasedTriggeringPolicy<ILoggingEvent>) fileAppender
                 .getTriggeringPolicy();
-        rollingApp.setMaxFileSize(triggeringPolicy.getMaxFileSize());
+        rollingApp.setMaxFileSize(LogbackModuleFactory.getMaxFileSize(triggeringPolicy).toString());
         rollingApp.setName(fileAppender.getName());
         rollingApp.setFileNamePattern(rollingPolicy.getFileNamePattern());
         rollingAppenders.add(rollingApp);
 
-        assertEquals(rollingApp.getFileName(), "target/osgi.log");
-        assertEquals(rollingApp.getMaxFileSize(), "50MB");
-        assertEquals(rollingApp.getName(), "VARLOGFILE");
+        assertEquals("target/osgi.log", rollingApp.getFileName());
+        assertEquals("50 MB", rollingApp.getMaxFileSize());
+        assertEquals("VARLOGFILE", rollingApp.getName());
 
         List<ConsoleAppenderTO> consoleAppenders = new ArrayList<>();
         ConsoleAppenderTO consoleApp = new ConsoleAppenderTO();