logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1102953 - in /logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src: main/java/org/apache/logging/log4j/core/appender/ main/java/org/apache/logging/log4j/core/appender/rolling/ main/java/org/apache/logging/log4j/core/append...
Date Sat, 14 May 2011 04:52:09 GMT
Author: rgoers
Date: Sat May 14 04:52:08 2011
New Revision: 1102953

URL: http://svn.apache.org/viewvc?rev=1102953&view=rev
Log:
got basic rolling to work

Added:
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderSizeTest.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/resources/log4j-rolling1.xml
Modified:
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/RollingFileAppender.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/FixedWindowRolloverStrategy.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/SizeBasedTriggeringPolicy.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/helper/FileRenameAction.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/helper/GZCompressAction.java

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/RollingFileAppender.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/RollingFileAppender.java?rev=1102953&r1=1102952&r2=1102953&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/RollingFileAppender.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/RollingFileAppender.java
Sat May 14 04:52:08 2011
@@ -40,7 +40,6 @@ public class RollingFileAppender extends
     public final String filePattern;
     private final TriggeringPolicy policy;
     private final RolloverStrategy strategy;
-    private final Lock lock = new ReentrantLock();
     private final boolean bufferedIO;
 
     public RollingFileAppender(String name, Layout layout, TriggeringPolicy policy, RolloverStrategy
strategy,
@@ -71,7 +70,6 @@ public class RollingFileAppender extends
                                               @PluginAttr("filePattern") String filePattern,
                                               @PluginAttr("append") String append,
                                               @PluginAttr("name") String name,
-                                              @PluginAttr("compress") String compress,
                                               @PluginAttr("bufferedIO") String bufferedIO,
                                               @PluginAttr("immediateFlush") String immediateFlush,
                                               @PluginElement("policy") TriggeringPolicy policy,
@@ -85,8 +83,6 @@ public class RollingFileAppender extends
         boolean isBuffered = bufferedIO == null ? true : Boolean.valueOf(bufferedIO);;
         boolean isFlush = immediateFlush == null ? true : Boolean.valueOf(immediateFlush);;
 
-        CompressionType type = CompressionType.NONE;
-
         if (name == null) {
             logger.error("No name provided for FileAppender");
             return null;
@@ -112,15 +108,7 @@ public class RollingFileAppender extends
             return null;
         }
 
-        if (compress != null) {
-            CompressionType t = CompressionType.valueOf(compress.toUpperCase());
-            if (t != null) {
-                type = t;
-            }
-        }
-
-        RollingFileManager manager = RollingFileManager.getFileManager(fileName, filePattern,
isAppend, isBuffered,
-            type);
+        RollingFileManager manager = RollingFileManager.getFileManager(fileName, filePattern,
isAppend, isBuffered);
         if (manager == null) {
             return null;
         }

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/FixedWindowRolloverStrategy.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/FixedWindowRolloverStrategy.java?rev=1102953&r1=1102952&r2=1102953&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/FixedWindowRolloverStrategy.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/FixedWindowRolloverStrategy.java
Sat May 14 04:52:08 2011
@@ -17,7 +17,6 @@
 package org.apache.logging.log4j.core.appender.rolling;
 
 import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.core.appender.CompressionType;
 import org.apache.logging.log4j.core.appender.rolling.helper.Action;
 import org.apache.logging.log4j.core.appender.rolling.helper.FileRenameAction;
 import org.apache.logging.log4j.core.appender.rolling.helper.GZCompressAction;
@@ -107,17 +106,12 @@ public class FixedWindowRolloverStrategy
             String compressedName = renameTo;
             Action compressAction = null;
 
-            switch (manager.getCompressionType()) {
-                case GZIP: {
-                    renameTo = renameTo.substring(0, renameTo.length() - 3);
-                    compressAction = new GZCompressAction(new File(renameTo), new File(compressedName),
true);
-                    break;
-                }
-                case ZIP: {
-                    renameTo = renameTo.substring(0, renameTo.length() - 4);
-                    compressAction = new ZipCompressAction(new File(renameTo), new File(compressedName),
true);
-                    break;
-                }
+            if (renameTo.endsWith(".gz")) {
+                renameTo = renameTo.substring(0, renameTo.length() - 3);
+                compressAction = new GZCompressAction(new File(renameTo), new File(compressedName),
true);
+            } else if (renameTo.endsWith(".zip")) {
+                renameTo = renameTo.substring(0, renameTo.length() - 4);
+                compressAction = new ZipCompressAction(new File(renameTo), new File(compressedName),
true);
             }
 
             FileRenameAction renameAction =

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java?rev=1102953&r1=1102952&r2=1102953&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java
Sat May 14 04:52:08 2011
@@ -18,16 +18,22 @@ package org.apache.logging.log4j.core.ap
 
 import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.core.LogEvent;
-import org.apache.logging.log4j.core.appender.CompressionType;
 import org.apache.logging.log4j.core.appender.FileManager;
 import org.apache.logging.log4j.core.appender.ManagerFactory;
+import org.apache.logging.log4j.core.appender.rolling.helper.Action;
+import org.apache.logging.log4j.core.appender.rolling.helper.ActionBase;
 import org.apache.logging.log4j.internal.StatusLogger;
 
 import java.io.BufferedOutputStream;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.OutputStream;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
 
 /**
  *
@@ -39,49 +45,35 @@ public class RollingFileManager extends 
      */
     protected static final Logger logger = StatusLogger.getLogger();
 
-    private CompressionType type = null;
     private long size;
     private long initialTime;
     private PatternProcessor processor;
+    private ArrayBlockingQueue<RollingFileManager> queue = new ArrayBlockingQueue<RollingFileManager>(1);
+    private static int count = 0;
 
     private static ManagerFactory factory = new RollingFileManagerFactory();
 
-    public static RollingFileManager getFileManager(String fileName, String pattern, boolean
append, boolean bufferedIO,
-                                                    CompressionType type) {
+    public static RollingFileManager getFileManager(String fileName, String pattern, boolean
append,
+                                                    boolean bufferedIO) {
 
-        return (RollingFileManager) getManager(fileName, factory, new FactoryData(fileName,
pattern, append, bufferedIO,
-                                               type));
+        return (RollingFileManager) getManager(fileName, factory, new FactoryData(fileName,
pattern, append,
+            bufferedIO));
     }
 
-    public RollingFileManager(String fileName, String pattern, OutputStream os, boolean append,
long size,
-                              CompressionType type, long time) {
+    public RollingFileManager(String fileName, String pattern, OutputStream os, boolean append,
long size, long time) {
         super(fileName, os, append, false);
         this.size = size;
-        this.type = type;
         this.initialTime = time;
         processor = new PatternProcessor(pattern);
+        queue.add(this);
     }
 
-    @Override
-    public void release() {
-        super.release();
-        if (!isOpen() && type != null) {
-            doCompress();
-        }
-    }
 
-    protected synchronized void write(byte[] bytes, int offset, int length)  {
+    protected synchronized void write(byte[] bytes, int offset, int length) {
         size += length;
         super.write(bytes, offset, length);
     }
 
-    public CompressionType getCompressionType() {
-        return this.type;
-    }
-
-    private void doCompress() {
-
-    }
     public long getFileSize() {
         return size;
     }
@@ -92,15 +84,15 @@ public class RollingFileManager extends 
 
     public synchronized void checkRollover(LogEvent event, TriggeringPolicy policy, RolloverStrategy
strategy) {
         if (policy.isTriggeringEvent(event)) {
-            close();
-            strategy.rollover(this);
-            try {
-                size = 0;
-                initialTime = System.currentTimeMillis();
-                OutputStream os = new FileOutputStream(getFileName(), isAppend());
-                setOutputStream(os);
-            } catch (FileNotFoundException ex) {
-                logger.error("FileManager (" + getFileName() + ") " + ex);
+            if (rollover(strategy)) {
+                try {
+                    size = 0;
+                    initialTime = System.currentTimeMillis();
+                    OutputStream os = new FileOutputStream(getFileName(), isAppend());
+                    setOutputStream(os);
+                } catch (FileNotFoundException ex) {
+                    logger.error("FileManager (" + getFileName() + ") " + ex);
+                }
             }
         }
     }
@@ -109,19 +101,110 @@ public class RollingFileManager extends 
         return processor;
     }
 
+    private boolean rollover(RolloverStrategy strategy) {
+
+        boolean success = false;
+
+        try {
+            /* Block until the asynchronous operation is completed. If it takes too long
then
+               don't roll over.
+             */
+            if (queue.poll(50, TimeUnit.MILLISECONDS) == null) {
+                logger.error("Unable to acquire lock for rollover");
+                return success;
+            }
+        } catch (InterruptedException ie) {
+            logger.error("Thread interrupted while attempting to check rollover", ie);
+            return success;
+        }
+
+        RolloverDescription descriptor = strategy.rollover(this);
+
+        if (descriptor != null) {
+
+            close();
+
+            try {
+
+                if (descriptor.getSynchronous() != null) {
+
+                    try {
+                        success = descriptor.getSynchronous().execute();
+                    } catch (Exception ex) {
+                        logger.error("Error in synchronous task", ex);
+                    }
+                }
+
+                if (success) {
+                    Action async = new AsyncAction(descriptor.getAsynchronous(), this);
+                    if (async != null) {
+                        new Thread(async).start();
+                    }
+                }
+            } finally {
+                if (!success && queue.size() == 0) {
+                    queue.add(this);
+                }
+            }
+            return success;
+        }
+        return false;
+    }
+
+    private static class AsyncAction extends ActionBase {
+
+        private final Action action;
+        private final RollingFileManager manager;
+
+        public AsyncAction(Action act, RollingFileManager manager) {
+            this.action = act;
+            this.manager = manager;
+        }
+
+        /**
+         * Perform an action.
+         *
+         * @return true if action was successful.  A return value of false will cause
+         *         the rollover to be aborted if possible.
+         * @throws java.io.IOException if IO error, a thrown exception will cause the rollover
+         *                             to be aborted if possible.
+         */
+        public boolean execute() throws IOException {
+            try {
+                return action.execute();
+            } finally {
+                this.manager.queue.add(manager);
+            }
+        }
+
+        /**
+         * Cancels the action if not already initialized or waits till completion.
+         */
+        public void close() {
+            action.close();
+        }
+
+        /**
+         * Determines if action has been completed.
+         *
+         * @return true if action is complete.
+         */
+        public boolean isComplete() {
+            return action.isComplete();
+        }
+    }
+
     private static class FactoryData {
         String fileName;
         String pattern;
         boolean append;
         boolean bufferedIO;
-        CompressionType type;
 
-        public FactoryData(String fileName, String pattern, boolean append, boolean bufferedIO,
CompressionType type) {
+        public FactoryData(String fileName, String pattern, boolean append, boolean bufferedIO)
{
             this.fileName = fileName;
             this.pattern = pattern;
             this.append = append;
             this.bufferedIO = bufferedIO;
-            this.type = type;
         }
     }
 
@@ -142,7 +225,7 @@ public class RollingFileManager extends 
                 if (data.bufferedIO) {
                     os = new BufferedOutputStream(os);
                 }
-                return new RollingFileManager(data.fileName, data.pattern, os, data.append,
size, data.type, time);
+                return new RollingFileManager(data.fileName, data.pattern, os, data.append,
size, time);
             } catch (FileNotFoundException ex) {
                 logger.error("FileManager (" + data.fileName + ") " + ex);
             }

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/SizeBasedTriggeringPolicy.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/SizeBasedTriggeringPolicy.java?rev=1102953&r1=1102952&r2=1102953&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/SizeBasedTriggeringPolicy.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/SizeBasedTriggeringPolicy.java
Sat May 14 04:52:08 2011
@@ -16,24 +16,43 @@
  */
 package org.apache.logging.log4j.core.appender.rolling;
 
+import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.core.LogEvent;
-import org.apache.logging.log4j.core.appender.FileManager;
 import org.apache.logging.log4j.core.config.plugins.Plugin;
 import org.apache.logging.log4j.core.config.plugins.PluginAttr;
-import org.apache.logging.log4j.core.config.plugins.PluginElement;
 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
+import org.apache.logging.log4j.internal.StatusLogger;
+
+import java.text.NumberFormat;
+import java.text.ParseException;
+import java.util.Locale;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /**
  *
  */
-@Plugin(name = "Size", type = "Core", printObject = true)
+@Plugin(name = "SizeBasedTriggeringPolicy", type = "Core", printObject = true)
 public class SizeBasedTriggeringPolicy implements TriggeringPolicy {
 
+    protected static final Logger logger = StatusLogger.getLogger();
+
+    private static final long KB = 1024;
+    private static final long MB = KB * KB;
+    private static final long GB = KB * MB;
+
     /**
      * Rollover threshold size in bytes.
      */
     private static final long MAX_FILE_SIZE = 10 * 1024 * 1024; // let 10 MB the default
max size
 
+
+    /**
+     * Pattern for string parsing.
+     */
+    private static final Pattern valuePattern =
+        Pattern.compile("([0-9]+([\\.,][0-9]+)?)\\s*(|K|M|G)B?", Pattern.CASE_INSENSITIVE);
+
     private final long maxFileSize;
 
     private RollingFileManager manager;
@@ -64,7 +83,51 @@ public class SizeBasedTriggeringPolicy i
 
     @PluginFactory
     public static SizeBasedTriggeringPolicy createPolicy(@PluginAttr("size") String size)
{
-        long maxSize = size == null ? MAX_FILE_SIZE : Long.parseLong(size);
+
+        long maxSize = size == null ? MAX_FILE_SIZE : valueOf(size);
         return new SizeBasedTriggeringPolicy(maxSize);
     }
+
+    /**
+     * Converts a string to a number of bytes. Strings consist of a floating point value
followed by
+     * K, M, or G for kilobytes, megabytes, gigabytes, respectively. The
+     * abbreviations KB, MB, and GB are also accepted. Matching is case insensitive.
+     *
+     * @param string The string to convert
+     * @return The Bytes value for the string
+     */
+    private static long valueOf(final String string){
+        final Matcher matcher = valuePattern.matcher(string);
+
+        // Valid input?
+        if (matcher.matches()) {
+            try {
+                // Get double precision value
+                final long value = NumberFormat.getNumberInstance(Locale.getDefault()).parse(
+                    matcher.group(1)).longValue();
+
+                // Get units specified
+                final String units = matcher.group(3);
+
+                if (units.equalsIgnoreCase("")) {
+                    return value;
+                } else if (units.equalsIgnoreCase("K")) {
+                    return value * KB;
+                } else if (units.equalsIgnoreCase("M")) {
+                    return value * MB;
+                } else if (units.equalsIgnoreCase("G")) {
+                    return value * GB;
+                } else {
+                    logger.error("Units not recognized: " + string);
+                    return MAX_FILE_SIZE;
+                }
+            } catch (ParseException e) {
+                logger.error("Unable to parse numeric part: " + string, e);
+                return MAX_FILE_SIZE;
+            }
+        } else {
+            logger.error("Unable to parse bytes: " + string);
+            return MAX_FILE_SIZE;
+        }
+    }
 }

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/helper/FileRenameAction.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/helper/FileRenameAction.java?rev=1102953&r1=1102952&r2=1102953&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/helper/FileRenameAction.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/helper/FileRenameAction.java
Sat May 14 04:52:08 2011
@@ -76,12 +76,16 @@ public final class FileRenameAction exte
     public static boolean execute(final File source, final File destination, boolean renameEmptyFiles)
{
         if (renameEmptyFiles || (source.length() > 0)) {
             try {
-                return source.renameTo(destination);
+
+                boolean result = source.renameTo(destination);
+                //System.out.println("Rename of " + source.getName() + " to " + destination.getName()
+ ": " + result);
+                return result;
             } catch (Exception ex) {
                 try {
                     copyFile(source, destination);
                     return source.delete();
                 } catch (IOException iex) {
+                    iex.printStackTrace();
                 }
             }
         }
@@ -96,18 +100,28 @@ public final class FileRenameAction exte
 
         FileChannel srcChannel = null;
         FileChannel destChannel = null;
+        FileInputStream srcStream = null;
+        FileOutputStream destStream = null;
         try {
-            srcChannel = new FileInputStream(source).getChannel();
-            destChannel = new FileOutputStream(destination).getChannel();
+            srcStream = new FileInputStream(source);
+            destStream = new FileOutputStream(destination);
+            srcChannel = srcStream.getChannel();
+            destChannel = destStream.getChannel();
             destChannel.transferFrom(srcChannel, 0, srcChannel.size());
         }
         finally {
             if(srcChannel != null) {
                 srcChannel.close();
             }
+            if (srcStream != null) {
+                srcStream.close();
+            }
             if (destChannel != null) {
                 destChannel.close();
             }
+            if (destStream != null) {
+                destStream.close();
+            }
         }
     }
 }

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/helper/GZCompressAction.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/helper/GZCompressAction.java?rev=1102953&r1=1102952&r2=1102953&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/helper/GZCompressAction.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/helper/GZCompressAction.java
Sat May 14 04:52:08 2011
@@ -105,8 +105,10 @@ public final class GZCompressAction exte
             os.close();
             fis.close();
 
-            if (deleteSource && !source.delete()) {
-                logger.warn("Unable to delete " + source.toString() + ".");
+            if (deleteSource) {
+                if (!source.delete()) {
+                    logger.warn("Unable to delete " + source.toString() + ".");
+                }
             }
 
             return true;

Added: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderSizeTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderSizeTest.java?rev=1102953&view=auto
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderSizeTest.java
(added)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderSizeTest.java
Sat May 14 04:52:08 2011
@@ -0,0 +1,109 @@
+/*
+ * 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.logging.log4j.core.appender.rolling;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.ThreadContext;
+import org.apache.logging.log4j.core.Layout;
+import org.apache.logging.log4j.core.Log4jLogEvent;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.appender.OutputStreamManager;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.XMLConfigurationFactory;
+import org.apache.logging.log4j.core.layout.PatternLayout;
+import org.apache.logging.log4j.internal.StatusLogger;
+import org.apache.logging.log4j.message.SimpleMessage;
+import org.junit.After;
+import org.junit.Test;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedReader;
+import java.io.DataInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+/**
+ *
+ */
+public class RollingAppenderSizeTest {
+
+    private static final String CONFIG = "log4j-rolling1.xml";
+    private static final String DIR = "target/rolling1";
+
+    org.apache.logging.log4j.Logger logger = LogManager.getLogger(RollingAppenderSizeTest.class.getName());
+
+    @BeforeClass
+    public static void setupClass() {
+        deleteDir();
+        System.setProperty(XMLConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
+        LoggerContext ctx = (LoggerContext) LogManager.getContext();
+        Configuration config = ctx.getConfiguration();
+    }
+
+    @AfterClass
+    public static void cleanupClass() {
+        deleteDir();
+        System.clearProperty(XMLConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
+        LoggerContext ctx = (LoggerContext) LogManager.getContext();
+        ctx.reconfigure();
+        StatusLogger.getLogger().reset();
+    }
+
+    @Test
+    public void testAppender() throws Exception {
+        for (int i=0; i < 100; ++i) {
+            logger.debug("This is test message number " + i);
+        }
+        File dir = new File(DIR);
+        assertTrue("Directory not created", dir.exists() && dir.listFiles().length
> 0);
+        File[] files = dir.listFiles();
+        assertTrue("No files created", files.length > 0);
+        boolean found = false;
+        for (File file : files) {
+            if (file.getName().endsWith(".gz")) {
+                found = true;
+            }
+        }
+        assertTrue("No compressed files found", found);
+    }
+
+    private static void deleteDir() {
+        File dir = new File(DIR);
+        if (dir.exists()) {
+            File[] files = dir.listFiles();
+            for (File file : files) {
+                file.delete();
+            }
+            dir.delete();
+        }
+    }
+}

Added: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/resources/log4j-rolling1.xml
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/resources/log4j-rolling1.xml?rev=1102953&view=auto
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/resources/log4j-rolling1.xml
(added)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/resources/log4j-rolling1.xml
Sat May 14 04:52:08 2011
@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<configuration packages="" status="debug" name="XMLConfigTest">
+  <properties>
+    <property name="filename">target/rolling1/rollingtest.log</property>
+  </properties>
+  <filters>
+    <Threshold level="debug"/>
+  </filters>
+
+  <appenders>
+    <Console name="STDOUT">
+      <PatternLayout pattern="%m%n"/>
+    </Console>
+    <RollingFile name="RollingFile" fileName="${filename}" filePattern="target/rolling1/test1-%i.log.gz">
+      <PatternLayout>
+        <pattern>%d %p %C{1.} [%t] %m%n</pattern>
+      </PatternLayout>
+      <SizeBasedTriggeringPolicy size="500" />
+      <FixedWindowRolloverStrategy />
+    </RollingFile>
+    <List name="List">
+      <filters>
+        <Threshold level="error"/>
+      </filters>
+    </List>
+  </appenders>
+
+  <loggers>
+    <logger name="org.apache.logging.log4j.test1" level="debug" additivity="false">
+      <filters>
+        <ThreadContextMap>
+          <KeyValuePair key="test" value="123"/>
+        </ThreadContextMap>
+      </filters>
+      <appender-ref ref="STDOUT"/>
+    </logger>>
+
+    <logger name="org.apache.logging.log4j.core.appender.rolling" level="debug" additivity="false">
+      <appender-ref ref="RollingFile"/>
+    </logger>>
+
+    <root level="error">
+      <appender-ref ref="STDOUT"/>
+    </root>
+  </loggers>
+
+</configuration>
\ No newline at end of file



---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org


Mime
View raw message