incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chet...@apache.org
Subject svn commit: r1571319 - in /sling/trunk/bundles/commons/log/src/main: java/org/apache/sling/commons/log/logback/internal/ java/org/apache/sling/commons/log/logback/internal/util/ resources/OSGI-INF/metatype/
Date Mon, 24 Feb 2014 15:23:26 GMT
Author: chetanm
Date: Mon Feb 24 15:23:26 2014
New Revision: 1571319

URL: http://svn.apache.org/r1571319
Log:
SLING-3275 - Add Buffered Logging Configuration in "Apache Sling Logging Logger"

Exposed a config on LogWriter to enabled buffered logging

Added:
    sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/util/SlingContextUtil.java
  (with props)
Modified:
    sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogConfigManager.java
    sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogWriter.java
    sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/util/Util.java
    sling/trunk/bundles/commons/log/src/main/resources/OSGI-INF/metatype/metatype.properties
    sling/trunk/bundles/commons/log/src/main/resources/OSGI-INF/metatype/metatype.xml

Modified: sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogConfigManager.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogConfigManager.java?rev=1571319&r1=1571318&r2=1571319&view=diff
==============================================================================
--- sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogConfigManager.java
(original)
+++ sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogConfigManager.java
Mon Feb 24 15:23:26 2014
@@ -60,6 +60,8 @@ public class LogConfigManager implements
 
     public static final String LOG_FILE_SIZE = "org.apache.sling.commons.log.file.size";
 
+    public static final String LOG_FILE_BUFFERED = "org.apache.sling.commons.log.file.buffered";
+
     public static final String LOG_PATTERN = "org.apache.sling.commons.log.pattern";
 
     public static final String LOG_PATTERN_DEFAULT = "%d{dd.MM.yyyy HH:mm:ss.SSS} *%level*
[%thread] %logger %msg%n";
@@ -418,7 +420,10 @@ public class LogConfigManager implements
                 fileSize = fileSizeProp.toString();
             }
 
-            LogWriter newWriter = new LogWriter(pid, getAppnderName(logFileName), fileNum,
fileSize, logFileName);
+            boolean bufferedLogging = Util.toBoolean(configuration.get(LogConfigManager.LOG_FILE_BUFFERED),
false);
+
+            LogWriter newWriter = new LogWriter(pid, getAppnderName(logFileName), fileNum,
+                    fileSize, logFileName, bufferedLogging);
             if (oldWriter != null) {
                 writerByFileName.remove(oldWriter.getFileName());
             }

Modified: sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogWriter.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogWriter.java?rev=1571319&r1=1571318&r2=1571319&view=diff
==============================================================================
--- sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogWriter.java
(original)
+++ sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogWriter.java
Mon Feb 24 15:23:26 2014
@@ -27,9 +27,11 @@ import ch.qos.logback.core.ConsoleAppend
 import ch.qos.logback.core.Context;
 import ch.qos.logback.core.OutputStreamAppender;
 import ch.qos.logback.core.encoder.Encoder;
+import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
 import ch.qos.logback.core.rolling.FixedWindowRollingPolicy;
 import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy;
 import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;
+import org.apache.sling.commons.log.logback.internal.util.SlingContextUtil;
 import org.apache.sling.commons.log.logback.internal.util.SlingRollingFileAppender;
 
 /**
@@ -71,7 +73,9 @@ public class LogWriter {
 
     private final String appenderName;
 
-    public LogWriter(String configurationPID, String appenderName, int logNumber, String
logRotation, String fileName) {
+    private final boolean bufferedLogging;
+
+    public LogWriter(String configurationPID, String appenderName, int logNumber, String
logRotation, String fileName, boolean bufferedLogging) {
         this.appenderName = appenderName;
         if (fileName == null || fileName.length() == 0) {
             fileName = FILE_NAME_CONSOLE;
@@ -89,10 +93,11 @@ public class LogWriter {
         this.fileName = fileName;
         this.logNumber = logNumber;
         this.logRotation = logRotation;
+        this.bufferedLogging = bufferedLogging;
     }
 
     public LogWriter(String appenderName,String fileName, int logNumber, String logRotation)
{
-        this(null, appenderName, logNumber, logRotation, fileName);
+        this(null, appenderName, logNumber, logRotation, fileName, false);
     }
 
     public String getConfigurationPID() {
@@ -124,12 +129,14 @@ public class LogWriter {
     }
 
     public Appender<ILoggingEvent> createAppender(final Context context, final Encoder<ILoggingEvent>
encoder) {
-
+        SlingContextUtil ctxUtil = new SlingContextUtil(context, this);
         OutputStreamAppender<ILoggingEvent> appender;
         if (FILE_NAME_CONSOLE.equals(fileName)) {
             appender = new ConsoleAppender<ILoggingEvent>();
             appender.setName(FILE_NAME_CONSOLE);
         } else {
+            ctxUtil.addInfo("Configuring appender "+getFileName());
+
             SlingRollingFileAppender<ILoggingEvent> rollingAppender = new SlingRollingFileAppender<ILoggingEvent>();
             rollingAppender.setAppend(true);
             rollingAppender.setFile(getFileName());
@@ -186,12 +193,15 @@ public class LogWriter {
                 rollingAppender.setRollingPolicy(pol);
             } else {
                 TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<ILoggingEvent>();
-                policy.setFileNamePattern(createFileNamePattern(getFileName(), getLogRotation()));
+                String fileNamePattern = createFileNamePattern(getFileName(), getLogRotation());
+                policy.setFileNamePattern(fileNamePattern);
                 policy.setMaxHistory(getLogNumber());
                 policy.setContext(context);
                 policy.setParent(rollingAppender);
                 policy.start();
                 rollingAppender.setTriggeringPolicy(policy);
+
+                ctxUtil.addInfo("Configured TimeBasedRollingPolicy with pattern "+ fileNamePattern);
             }
 
             rollingAppender.setLogWriter(this);
@@ -200,9 +210,19 @@ public class LogWriter {
             appender = rollingAppender;
         }
 
+        if(bufferedLogging && encoder instanceof LayoutWrappingEncoder){
+            ((LayoutWrappingEncoder) encoder).setImmediateFlush(false);
+            ctxUtil.addInfo("Setting immediateFlush to false");
+        } else{
+            ctxUtil.addInfo("immediateFlush property not modified. Defaults to true");
+        }
+
         appender.setContext(context);
         appender.setEncoder(encoder);
         appender.start();
+
+        ctxUtil.addInfo("Completed configuring appender with name "+getFileName());
+
         return appender;
     }
 

Added: sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/util/SlingContextUtil.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/util/SlingContextUtil.java?rev=1571319&view=auto
==============================================================================
--- sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/util/SlingContextUtil.java
(added)
+++ sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/util/SlingContextUtil.java
Mon Feb 24 15:23:26 2014
@@ -0,0 +1,40 @@
+/*
+ * 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.sling.commons.log.logback.internal.util;
+
+import ch.qos.logback.core.Context;
+import ch.qos.logback.core.spi.ContextAwareBase;
+
+/**
+ * Custom util such that origin can be customized
+ */
+public class SlingContextUtil extends ContextAwareBase {
+    private final Object origin;
+
+    public SlingContextUtil(Context context, Object origin) {
+        this.origin = origin;
+        setContext(context);
+    }
+
+    @Override
+    protected Object getDeclaredOrigin() {
+        return origin;
+    }
+}

Propchange: sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/util/SlingContextUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/util/Util.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/util/Util.java?rev=1571319&r1=1571318&r2=1571319&view=diff
==============================================================================
--- sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/util/Util.java
(original)
+++ sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/util/Util.java
Mon Feb 24 15:23:26 2014
@@ -79,6 +79,26 @@ public class Util {
     //-------------Taken from org.apache.sling.commons.osgi.PropertiesUtil
 
     /**
+     * Returns the boolean value of the parameter or the
+     * <code>defaultValue</code> if the parameter is <code>null</code>.
+     * If the parameter is not a <code>Boolean</code> it is converted
+     * by calling <code>Boolean.valueOf</code> on the string value of the
+     * object.
+     * @param propValue the property value or <code>null</code>
+     * @param defaultValue the default boolean value
+     */
+    public static boolean toBoolean(Object propValue, boolean defaultValue) {
+        propValue = toObject(propValue);
+        if (propValue instanceof Boolean) {
+            return (Boolean) propValue;
+        } else if (propValue != null) {
+            return Boolean.valueOf(String.valueOf(propValue));
+        }
+
+        return defaultValue;
+    }
+
+    /**
      * Returns the parameter as an integer or the
      * <code>defaultValue</code> if the parameter is <code>null</code>
or if
      * the parameter is not an <code>Integer</code> and cannot be converted to

Modified: sling/trunk/bundles/commons/log/src/main/resources/OSGI-INF/metatype/metatype.properties
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/main/resources/OSGI-INF/metatype/metatype.properties?rev=1571319&r1=1571318&r2=1571319&view=diff
==============================================================================
--- sling/trunk/bundles/commons/log/src/main/resources/OSGI-INF/metatype/metatype.properties
(original)
+++ sling/trunk/bundles/commons/log/src/main/resources/OSGI-INF/metatype/metatype.properties
Mon Feb 24 15:23:26 2014
@@ -77,4 +77,10 @@ log.config.packagingData.description = I
 
 log.config.maxCallerDataDepth.name = Max Caller Depth
 log.config.maxCallerDataDepth.description = The stack data depth computed during caller data
\
-  extraction. See http://logback.qos.ch/manual/layouts.html#caller
\ No newline at end of file
+  extraction. See http://logback.qos.ch/manual/layouts.html#caller
+
+log.file.buffered.name = Buffered Logging
+log.file.buffered.description = By default logging events are immediately written to disk
\
+  and will not be lost in case your application exits without properly closing appenders.
\
+  If set to true  and if appenders are not closed properly when your application exits, then
\
+  logging events not yet written to disk may be lost. See http://logback.qos.ch/manual/encoders.html#immediateFlush
\ No newline at end of file

Modified: sling/trunk/bundles/commons/log/src/main/resources/OSGI-INF/metatype/metatype.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/main/resources/OSGI-INF/metatype/metatype.xml?rev=1571319&r1=1571318&r2=1571319&view=diff
==============================================================================
--- sling/trunk/bundles/commons/log/src/main/resources/OSGI-INF/metatype/metatype.xml (original)
+++ sling/trunk/bundles/commons/log/src/main/resources/OSGI-INF/metatype/metatype.xml Mon
Feb 24 15:23:26 2014
@@ -80,6 +80,9 @@
         <metatype:AD id="org.apache.sling.commons.log.file.size"
             type="String" default="'.'yyyy-MM-dd" name="%log.file.size.name"
             description="%log.file.size.description" />
+        <metatype:AD id="org.apache.sling.commons.log.file.buffered"
+             type="Boolean" default="false" name="%log.file.buffered.name"
+             description="%log.file.buffered.description" />
     </metatype:OCD>
     <metatype:Designate
         pid="org.apache.sling.commons.log.LogManager.factory.writer"



Mime
View raw message