You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2015/09/27 04:32:57 UTC
tomee git commit: TOMEE-1631 LocalFileHandler
Repository: tomee
Updated Branches:
refs/heads/master 8652068e9 -> af5755444
TOMEE-1631 LocalFileHandler
Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/af575544
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/af575544
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/af575544
Branch: refs/heads/master
Commit: af575544450425cb3f4b1060ae7d4ef12862eba5
Parents: 8652068
Author: Romain Manni-Bucau <rm...@gmail.com>
Authored: Sat Sep 26 19:32:50 2015 -0700
Committer: Romain Manni-Bucau <rm...@gmail.com>
Committed: Sat Sep 26 19:32:50 2015 -0700
----------------------------------------------------------------------
tomee/tomee-juli/pom.xml | 27 +
.../handler/rotating/BackgroundTaskRunner.java | 91 +++
.../tomee/jul/handler/rotating/Duration.java | 178 ++++++
.../jul/handler/rotating/LocalFileHandler.java | 553 +++++++++++++++++++
.../apache/tomee/jul/handler/rotating/Size.java | 343 ++++++++++++
.../jul/handler/rotating/ArchivingTest.java | 215 +++++++
.../handler/rotating/LocalFileHandlerTest.java | 128 +++++
.../tomee/jul/handler/rotating/PerfRunner.java | 109 ++++
8 files changed, 1644 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tomee/blob/af575544/tomee/tomee-juli/pom.xml
----------------------------------------------------------------------
diff --git a/tomee/tomee-juli/pom.xml b/tomee/tomee-juli/pom.xml
index eb4147e..885f55d 100644
--- a/tomee/tomee-juli/pom.xml
+++ b/tomee/tomee-juli/pom.xml
@@ -46,8 +46,35 @@
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.openjdk.jmh</groupId>
+ <artifactId>jmh-core</artifactId>
+ <version>${jmh.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.openjdk.jmh</groupId>
+ <artifactId>jmh-generator-annprocess</artifactId>
+ <version>${jmh.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
+ <properties>
+ <jmh.version>1.10.5</jmh.version>
+ </properties>
+
<build>
<plugins>
<plugin>
http://git-wip-us.apache.org/repos/asf/tomee/blob/af575544/tomee/tomee-juli/src/main/java/org/apache/tomee/jul/handler/rotating/BackgroundTaskRunner.java
----------------------------------------------------------------------
diff --git a/tomee/tomee-juli/src/main/java/org/apache/tomee/jul/handler/rotating/BackgroundTaskRunner.java b/tomee/tomee-juli/src/main/java/org/apache/tomee/jul/handler/rotating/BackgroundTaskRunner.java
new file mode 100644
index 0000000..5b88b5f
--- /dev/null
+++ b/tomee/tomee-juli/src/main/java/org/apache/tomee/jul/handler/rotating/BackgroundTaskRunner.java
@@ -0,0 +1,91 @@
+/*
+ * 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.tomee.jul.handler.rotating;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.logging.LogManager;
+
+// Note: don't touch this class while not needed to avoid to trigger the executor service init
+// mainly there to avoid all handlers to have their own threads
+class BackgroundTaskRunner {
+ private static final ExecutorService EXECUTOR_SERVICE;
+ private static final boolean SYNCHRONOUS;
+
+ static {
+ final LogManager logManager = LogManager.getLogManager();
+ SYNCHRONOUS = Boolean.parseBoolean(getProperty(logManager, BackgroundTaskRunner.class.getName() + ".synchronous"));
+ if (SYNCHRONOUS) {
+ EXECUTOR_SERVICE = null;
+ } else {
+
+ final String threadCount = getProperty(logManager, BackgroundTaskRunner.class.getName() + ".threads");
+ final String shutdownTimeoutStr = getProperty(logManager, BackgroundTaskRunner.class.getName() + ".shutdownTimeout");
+ final Duration shutdownTimeout = new Duration(shutdownTimeoutStr == null ? "30 seconds" : shutdownTimeoutStr);
+ EXECUTOR_SERVICE = Executors.newFixedThreadPool(Integer.parseInt(threadCount == null ? "2" : threadCount), new ThreadFactory() {
+ private final ThreadGroup group;
+ private final AtomicInteger threadNumber = new AtomicInteger(1);
+ private final String namePrefix = "com.tomitribe.logging.jul.handler.BackgroundTaskThread-";
+
+ {
+ SecurityManager s = System.getSecurityManager();
+ group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
+ }
+
+ @Override
+ public Thread newThread(final Runnable r) {
+ final Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0);
+ if (!t.isDaemon()) {
+ t.setDaemon(true);
+ }
+ if (t.getPriority() != Thread.NORM_PRIORITY) {
+ t.setPriority(Thread.NORM_PRIORITY);
+ }
+ return t;
+ }
+ });
+
+ Runtime.getRuntime().addShutdownHook(new Thread() {
+ @Override
+ public void run() {
+ EXECUTOR_SERVICE.shutdown();
+ try {
+ EXECUTOR_SERVICE.awaitTermination(shutdownTimeout.asMillis(), TimeUnit.MILLISECONDS);
+ } catch (final InterruptedException e) {
+ Thread.interrupted();
+ }
+ }
+ });
+ }
+ }
+
+ private static String getProperty(final LogManager logManager, final String key) {
+ final String val = logManager.getProperty(key);
+ return val != null ? val : System.getProperty(key);
+ }
+
+ static void push(final Runnable runnable) {
+ if (SYNCHRONOUS) {
+ runnable.run();
+ } else {
+ EXECUTOR_SERVICE.submit(runnable);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/tomee/blob/af575544/tomee/tomee-juli/src/main/java/org/apache/tomee/jul/handler/rotating/Duration.java
----------------------------------------------------------------------
diff --git a/tomee/tomee-juli/src/main/java/org/apache/tomee/jul/handler/rotating/Duration.java b/tomee/tomee-juli/src/main/java/org/apache/tomee/jul/handler/rotating/Duration.java
new file mode 100644
index 0000000..005f43a
--- /dev/null
+++ b/tomee/tomee-juli/src/main/java/org/apache/tomee/jul/handler/rotating/Duration.java
@@ -0,0 +1,178 @@
+/*
+ * 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.tomee.jul.handler.rotating;
+
+import java.util.concurrent.TimeUnit;
+
+class Duration {
+ private long time;
+ private TimeUnit unit = TimeUnit.MILLISECONDS;
+
+ private Duration() {
+ // no-op
+ }
+
+ private Duration(final long time, final TimeUnit unit) {
+ this.time = time;
+ this.unit = unit;
+ }
+
+ Duration(final String string) {
+ this(string, null);
+ }
+
+ private Duration(final String string, final TimeUnit defaultUnit) {
+ final String[] strings = string.split(",| and ");
+
+ Duration total = new Duration();
+
+ for (final String value : strings) {
+ final Duration part = new Duration();
+ final String s = value.trim();
+
+ final StringBuilder t = new StringBuilder();
+ final StringBuilder u = new StringBuilder();
+
+ int i = 0;
+
+ // get the number
+ for (; i < s.length(); i++) {
+ final char c = s.charAt(i);
+ if (Character.isDigit(c) || i == 0 && c == '-') {
+ t.append(c);
+ } else {
+ break;
+ }
+ }
+
+ if (t.length() == 0) {
+ invalidFormat(s);
+ }
+
+ // skip whitespace
+ for (; i < s.length(); i++) {
+ final char c = s.charAt(i);
+ if (!Character.isWhitespace(c)) {
+ break;
+ }
+ }
+
+ // get time unit text part
+ for (; i < s.length(); i++) {
+ final char c = s.charAt(i);
+ if (Character.isLetter(c)) {
+ u.append(c);
+ } else {
+ invalidFormat(s);
+ }
+ }
+
+ part.time = Long.parseLong(t.toString());
+
+ part.unit = parseUnit(u.toString());
+
+ if (part.unit == null) {
+ part.unit = defaultUnit;
+ }
+
+ total = total.add(part);
+ }
+
+ this.time = total.time;
+ this.unit = total.unit;
+ }
+
+ public long asMillis() {
+ return unit.toMillis(time);
+ }
+
+ private static class Normalize {
+ private long a;
+ private long b;
+ private TimeUnit base;
+
+ private Normalize(final Duration a, final Duration b) {
+ this.base = lowest(a, b);
+ this.a = a.unit == null ? a.time : base.convert(a.time, a.unit);
+ this.b = b.unit == null ? b.time : base.convert(b.time, b.unit);
+ }
+
+ private static TimeUnit lowest(final Duration a, final Duration b) {
+ if (a.unit == null) return b.unit;
+ if (b.unit == null) return a.unit;
+ if (a.time == 0) return b.unit;
+ if (b.time == 0) return a.unit;
+ return TimeUnit.values()[Math.min(a.unit.ordinal(), b.unit.ordinal())];
+ }
+ }
+
+ public Duration add(final Duration that) {
+ final Normalize n = new Normalize(this, that);
+ return new Duration(n.a + n.b, n.base);
+ }
+
+ private static void invalidFormat(final String text) {
+ throw new IllegalArgumentException("Illegal duration format: '" + text +
+ "'. Valid examples are '10s' or '10 seconds'.");
+ }
+
+ private static TimeUnit parseUnit(final String u) {
+ if (u.length() == 0) {
+ return null;
+ }
+
+ if ("NANOSECONDS".equalsIgnoreCase(u)) return TimeUnit.NANOSECONDS;
+ if ("NANOSECOND".equalsIgnoreCase(u)) return TimeUnit.NANOSECONDS;
+ if ("NANOS".equalsIgnoreCase(u)) return TimeUnit.NANOSECONDS;
+ if ("NANO".equalsIgnoreCase(u)) return TimeUnit.NANOSECONDS;
+ if ("NS".equalsIgnoreCase(u)) return TimeUnit.NANOSECONDS;
+
+ if ("MICROSECONDS".equalsIgnoreCase(u)) return TimeUnit.MICROSECONDS;
+ if ("MICROSECOND".equalsIgnoreCase(u)) return TimeUnit.MICROSECONDS;
+ if ("MICROS".equalsIgnoreCase(u)) return TimeUnit.MICROSECONDS;
+ if ("MICRO".equalsIgnoreCase(u)) return TimeUnit.MICROSECONDS;
+
+ if ("MILLISECONDS".equalsIgnoreCase(u)) return TimeUnit.MILLISECONDS;
+ if ("MILLISECOND".equalsIgnoreCase(u)) return TimeUnit.MILLISECONDS;
+ if ("MILLIS".equalsIgnoreCase(u)) return TimeUnit.MILLISECONDS;
+ if ("MILLI".equalsIgnoreCase(u)) return TimeUnit.MILLISECONDS;
+ if ("MS".equalsIgnoreCase(u)) return TimeUnit.MILLISECONDS;
+
+ if ("SECONDS".equalsIgnoreCase(u)) return TimeUnit.SECONDS;
+ if ("SECOND".equalsIgnoreCase(u)) return TimeUnit.SECONDS;
+ if ("SEC".equalsIgnoreCase(u)) return TimeUnit.SECONDS;
+ if ("S".equalsIgnoreCase(u)) return TimeUnit.SECONDS;
+
+ if ("MINUTES".equalsIgnoreCase(u)) return TimeUnit.MINUTES;
+ if ("MINUTE".equalsIgnoreCase(u)) return TimeUnit.MINUTES;
+ if ("MIN".equalsIgnoreCase(u)) return TimeUnit.MINUTES;
+ if ("M".equalsIgnoreCase(u)) return TimeUnit.MINUTES;
+
+ if ("HOURS".equalsIgnoreCase(u)) return TimeUnit.HOURS;
+ if ("HOUR".equalsIgnoreCase(u)) return TimeUnit.HOURS;
+ if ("HRS".equalsIgnoreCase(u)) return TimeUnit.HOURS;
+ if ("HR".equalsIgnoreCase(u)) return TimeUnit.HOURS;
+ if ("H".equalsIgnoreCase(u)) return TimeUnit.HOURS;
+
+ if ("DAYS".equalsIgnoreCase(u)) return TimeUnit.DAYS;
+ if ("DAY".equalsIgnoreCase(u)) return TimeUnit.DAYS;
+ if ("D".equalsIgnoreCase(u)) return TimeUnit.DAYS;
+
+ throw new IllegalArgumentException("Unknown time unit '" + u + "'");
+ }
+}
+
http://git-wip-us.apache.org/repos/asf/tomee/blob/af575544/tomee/tomee-juli/src/main/java/org/apache/tomee/jul/handler/rotating/LocalFileHandler.java
----------------------------------------------------------------------
diff --git a/tomee/tomee-juli/src/main/java/org/apache/tomee/jul/handler/rotating/LocalFileHandler.java b/tomee/tomee-juli/src/main/java/org/apache/tomee/jul/handler/rotating/LocalFileHandler.java
new file mode 100644
index 0000000..7656063
--- /dev/null
+++ b/tomee/tomee-juli/src/main/java/org/apache/tomee/jul/handler/rotating/LocalFileHandler.java
@@ -0,0 +1,553 @@
+/*
+ * 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.tomee.jul.handler.rotating;
+
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.io.UnsupportedEncodingException;
+import java.nio.file.Files;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.sql.Timestamp;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import java.util.logging.ErrorManager;
+import java.util.logging.Filter;
+import java.util.logging.Formatter;
+import java.util.logging.Handler;
+import java.util.logging.Level;
+import java.util.logging.LogManager;
+import java.util.logging.LogRecord;
+import java.util.logging.SimpleFormatter;
+import java.util.regex.Pattern;
+import java.util.zip.Deflater;
+import java.util.zip.GZIPOutputStream;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+
+/**
+ * NOTE: for simplicity the prefix `org.apache.tomee.jul.handler.rotating.LocalFileHandler.` has been removed of name columns.
+ *
+ * |===
+ * | Name | Default Value | Description
+ * | filenamePattern | ${catalina.base}/logs/logs.%s.%03d.log | where log files are created, it uses String.format() and gives you the date and file number - in this order.
+ * | limit | 10 Megabytes | limit size indicating the file should be rotated
+ * | dateCheckInterval | 5 seconds | how often the date should be computed to rotate the file (don't do it each time for performances reason, means you can get few records of next day in a file name with current day)
+ * | bufferSize | -1 bytes | if positive the in memory buffer used to store data before flushing them to the disk
+ * | encoding | - | file encoding
+ * | level | ALL | level this handler accepts
+ * | filter | - | filter used to check if the message should be logged
+ * | formatter | java.util.logging.SimpleFormatter | formatter used to format messages
+ * | archiveDirectory | ${catalina.base}/logs/archives/ | where compressed logs are put.
+ * | archiveFormat | gzip | zip or gzip.
+ * | archiveOlderThan | -1 days | how many days files are kept before being compressed
+ * | purgeOlderThan | -1 days | how many days files are kept before being deleted, note: it applies on archives and not log files so 2 days of archiving and 3 days of purge makes it deleted after 5 days.
+ * | compressionLevel | -1 | In case of zip archiving the zip compression level (-1 for off or 0-9).
+ * |===
+ *
+ * NOTE: archiving and purging are done only when a file is rotated, it means it can be ignored during days if there is no logging activity.
+ *
+ * NOTE: archiving and purging is done in a background thread pool, you can configure the number of threads in thanks to
+ * `org.apache.tomee.jul.handler.rotating.BackgroundTaskRunner.threads` property in `conf/logging.properties`.
+ * Default is 2 which should be fine for most applications.
+ */
+/*
+ Open point/enhancements:
+ - date pattern/filename pattern instead of hardcoded String.format?
+ - write another async version? ensure it flushed well, use disruptor? -> bench seems to show it is useless
+ */
+public class LocalFileHandler extends Handler {
+ private static final int BUFFER_SIZE = 8102;
+
+ private long limit = 0;
+ private int bufferSize = -1;
+ private Pattern filenameRegex;
+ private Pattern archiveFilenameRegex;
+ private String filenamePattern = "${catalina.base}/logs/logs.%s.%03d.log";
+ private String archiveFormat = "gzip";
+ private long dateCheckInterval = TimeUnit.SECONDS.toMillis(5);
+ private long archiveExpiryDuration;
+ private int compressionLevel;
+ private long purgeExpiryDuration;
+ private File archiveDir;
+
+ private volatile int currentIndex;
+ private volatile long lastTimestamp;
+ private volatile String date;
+ private volatile PrintWriter writer;
+ private volatile int written;
+ private final ReadWriteLock writerLock = new ReentrantReadWriteLock();
+ private final Lock backgroundTaskLock = new ReentrantLock();
+ private volatile boolean closed;
+
+ public LocalFileHandler() {
+ configure();
+ }
+
+ private void configure() {
+ date = currentDate();
+
+ final String className = getClass().getName(); //allow classes to override
+
+ final ClassLoader cl = Thread.currentThread().getContextClassLoader();
+
+ dateCheckInterval = new Duration(getProperty(className + ".dateCheckInterval", String.valueOf(dateCheckInterval))).asMillis();
+ filenamePattern = replace(getProperty(className + ".filenamePattern", filenamePattern));
+ limit = new Size(getProperty(className + ".limit", String.valueOf("10 Mega"))).asBytes();
+
+ final int lastSep = Math.max(filenamePattern.lastIndexOf('/'), filenamePattern.lastIndexOf('\\'));
+ String fileNameReg = lastSep >= 0 ? filenamePattern.substring(lastSep + 1) : filenamePattern;
+ fileNameReg = fileNameReg.replace("%s", "\\d{4}\\-\\d{2}\\-\\d{2}"); // date.
+ { // file rotation index
+ final int indexIdxStart = fileNameReg.indexOf('%');
+ if (indexIdxStart >= 0) {
+ final int indexIdxEnd = fileNameReg.indexOf('d', indexIdxStart);
+ if (indexIdxEnd >= 0) {
+ fileNameReg = fileNameReg.substring(0, indexIdxStart) + "\\d*" + fileNameReg.substring(indexIdxEnd + 1, fileNameReg.length());
+ }
+ }
+ }
+ filenameRegex = Pattern.compile(fileNameReg);
+
+ compressionLevel = Integer.parseInt(getProperty(className + ".compressionLevel", String.valueOf(Deflater.DEFAULT_COMPRESSION)));
+ archiveExpiryDuration = new Duration(getProperty(className + ".archiveOlderThan", String.valueOf("-1 days"))).asMillis();
+ archiveDir = new File(replace(getProperty(className + ".archiveDirectory", "${catalina.base}/logs/archives/")));
+ archiveFormat = replace(getProperty(className + ".archiveFormat", archiveFormat));
+ archiveFilenameRegex = Pattern.compile(fileNameReg + "\\." + archiveFormat);
+
+ purgeExpiryDuration = new Duration(getProperty(className + ".purgeOlderThan", String.valueOf("-1 days"))).asMillis();
+
+ try {
+ bufferSize = (int) new Size(getProperty(className + ".bufferSize", "-1 b")).asBytes();
+ } catch (final NumberFormatException ignore) {
+ // no-op
+ }
+
+ final String encoding = getProperty(className + ".encoding", null);
+ if (encoding != null && encoding.length() > 0) {
+ try {
+ setEncoding(encoding);
+ } catch (final UnsupportedEncodingException ex) {
+ // no-op
+ }
+ }
+
+ setLevel(Level.parse(getProperty(className + ".level", "" + Level.ALL)));
+
+ final String filterName = getProperty(className + ".filter", null);
+ if (filterName != null) {
+ try {
+ setFilter(Filter.class.cast(cl.loadClass(filterName).newInstance()));
+ } catch (final Exception e) {
+ // Ignore
+ }
+ }
+
+ final String formatterName = getProperty(className + ".formatter", null);
+ if (formatterName != null) {
+ try {
+ setFormatter(Formatter.class.cast(cl.loadClass(formatterName).newInstance()));
+ } catch (final Exception e) {
+ setFormatter(new SimpleFormatter());
+ }
+ } else {
+ setFormatter(new SimpleFormatter());
+ }
+
+ setErrorManager(new ErrorManager());
+
+ lastTimestamp = System.currentTimeMillis();
+ }
+
+ protected String currentDate() {
+ return new Timestamp(System.currentTimeMillis()).toString().substring(0, 10);
+ }
+
+ @Override
+ public void publish(final LogRecord record) {
+ if (!isLoggable(record)) {
+ return;
+ }
+
+ final long now = System.currentTimeMillis();
+ final String tsDate;
+ // just do it once / sec if we have a lot of log, can make some log appearing in the wrong file but better than doing it each time
+ if (now - lastTimestamp > dateCheckInterval) { // using as much as possible volatile to avoid to lock too much
+ lastTimestamp = now;
+ tsDate = currentDate();
+ } else {
+ tsDate = null;
+ }
+
+ try {
+ writerLock.readLock().lock();
+ rotateIfNeeded(tsDate);
+
+ String result;
+ try {
+ result = getFormatter().format(record);
+ } catch (final Exception e) {
+ reportError(null, e, ErrorManager.FORMAT_FAILURE);
+ return;
+ }
+
+ try {
+ if (writer != null) {
+ writer.write(result);
+ if (bufferSize < 0) {
+ writer.flush();
+ }
+ } else {
+ reportError(getClass().getSimpleName() + " is closed or not yet initialized, unable to log [" + result + "]", null, ErrorManager.WRITE_FAILURE);
+ }
+ } catch (final Exception e) {
+ reportError(null, e, ErrorManager.WRITE_FAILURE);
+ }
+ } finally {
+ writerLock.readLock().unlock();
+ }
+ }
+
+ private void rotateIfNeeded(final String currentDate) {
+ if (!closed && writer == null) {
+ try {
+ writerLock.readLock().unlock();
+ writerLock.writeLock().lock();
+
+ if (!closed && writer == null) {
+ openWriter();
+ }
+ } finally {
+ writerLock.writeLock().unlock();
+ writerLock.readLock().lock();
+ }
+ } else if (shouldRotate(currentDate)) {
+ try {
+ writerLock.readLock().unlock();
+ writerLock.writeLock().lock();
+
+ if (shouldRotate(currentDate)) {
+ close();
+ if (currentDate != null && !date.equals(currentDate)) {
+ currentIndex = 0;
+ date = currentDate;
+ }
+ openWriter();
+ }
+ } finally {
+ writerLock.writeLock().unlock();
+ writerLock.readLock().lock();
+ }
+ }
+ }
+
+ private boolean shouldRotate(final String currentDate) { // new day, new file or limit exceeded
+ return (currentDate != null && !date.equals(currentDate)) || (limit > 0 && written >= limit);
+ }
+
+ @Override
+ public void close() {
+ closed = true;
+
+ writerLock.writeLock().lock();
+ try {
+ if (writer == null) {
+ return;
+ }
+ writer.write(getFormatter().getTail(this));
+ writer.flush();
+ writer.close();
+ writer = null;
+ } catch (final Exception e) {
+ reportError(null, e, ErrorManager.CLOSE_FAILURE);
+ } finally {
+ writerLock.writeLock().unlock();
+ }
+
+ // wait for bg tasks if running
+ backgroundTaskLock.lock();
+ backgroundTaskLock.unlock();
+ }
+
+ @Override
+ public void flush() {
+ writerLock.readLock().lock();
+ try {
+ writer.flush();
+ } catch (final Exception e) {
+ reportError(null, e, ErrorManager.FLUSH_FAILURE);
+ } finally {
+ writerLock.readLock().unlock();
+ }
+ }
+
+ protected void openWriter() {
+ final long beforeRotation = System.currentTimeMillis();
+
+ writerLock.writeLock().lock();
+ FileOutputStream fos = null;
+ OutputStream os = null;
+ try {
+ File pathname;
+ do {
+ pathname = new File(formatFilename(filenamePattern, date, currentIndex));
+ final File parent = pathname.getParentFile();
+ if (!parent.isDirectory() && !parent.mkdirs()) {
+ reportError("Unable to create [" + parent + "]", null, ErrorManager.OPEN_FAILURE);
+ writer = null;
+ return;
+ }
+ currentIndex++;
+ } while (pathname.isFile()); // loop to ensure we don't overwrite existing files
+
+ final String encoding = getEncoding();
+ fos = new FileOutputStream(pathname, true);
+ os = new CountingStream(bufferSize > 0 ? new BufferedOutputStream(fos, bufferSize) : fos);
+ writer = new PrintWriter((encoding != null) ? new OutputStreamWriter(os, encoding) : new OutputStreamWriter(os), false);
+ writer.write(getFormatter().getHead(this));
+ } catch (final Exception e) {
+ reportError(null, e, ErrorManager.OPEN_FAILURE);
+ writer = null;
+ if (fos != null) {
+ try {
+ fos.close();
+ } catch (final IOException e1) {
+ // no-op
+ }
+ }
+ if (os != null) {
+ try {
+ os.close();
+ } catch (final IOException e1) {
+ // no-op
+ }
+ }
+ } finally {
+ writerLock.writeLock().unlock();
+ }
+
+ BackgroundTaskRunner.push(new Runnable() {
+ @Override
+ public void run() {
+ backgroundTaskLock.lock();
+ try {
+ evict(beforeRotation);
+ } catch (final Exception e) {
+ reportError("Can't do the log eviction", e, ErrorManager.GENERIC_FAILURE);
+ } finally {
+ backgroundTaskLock.unlock();
+ }
+ }
+ });
+ }
+
+ private void evict(final long now) {
+ if (purgeExpiryDuration > 0) { // purging archives
+ final File[] archives = archiveDir.listFiles(new FilenameFilter() {
+ @Override
+ public boolean accept(final File dir, final String name) {
+ return archiveFilenameRegex.matcher(name).matches();
+ }
+ });
+
+ if (archives != null) {
+ for (final File archive : archives) {
+ try {
+ final BasicFileAttributes attr = Files.readAttributes(archive.toPath(), BasicFileAttributes.class);
+ if (now - attr.creationTime().toMillis() > purgeExpiryDuration) {
+ if (!Files.deleteIfExists(archive.toPath())) {
+ // dont try to delete on exit cause we will find it again
+ reportError("Can't delete " + archive.getAbsolutePath() + ".", null, ErrorManager.GENERIC_FAILURE);
+ }
+ }
+ } catch (final IOException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+ }
+ }
+ if (archiveExpiryDuration > 0) { // archiving log files
+ final File[] logs = new File(formatFilename(filenamePattern, "0000-00-00", 0)).getParentFile()
+ .listFiles(new FilenameFilter() {
+ @Override
+ public boolean accept(final File dir, final String name) {
+ return filenameRegex.matcher(name).matches();
+ }
+ });
+
+ if (logs != null) {
+ for (final File file : logs) {
+ try {
+ final BasicFileAttributes attr = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
+ if (attr.creationTime().toMillis() < now && now - attr.lastModifiedTime().toMillis() > archiveExpiryDuration) {
+ createArchive(file);
+ }
+ } catch (final IOException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+ }
+ }
+ }
+
+ private String formatFilename(final String pattern, final String date, final int index) {
+ return String.format(pattern, date, index);
+ }
+
+ private void createArchive(final File source) {
+ final File target = new File(archiveDir, source.getName() + "." + archiveFormat);
+ if (target.isFile()) {
+ return;
+ }
+
+ final File parentFile = target.getParentFile();
+ if (!parentFile.isDirectory() && !parentFile.mkdirs()) {
+ throw new IllegalStateException("Can't create " + parentFile.getAbsolutePath());
+ }
+
+ if (archiveFormat.equalsIgnoreCase("gzip")) {
+ try (final OutputStream outputStream = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(target)))) {
+ final byte[] buffer = new byte[BUFFER_SIZE];
+ try (final FileInputStream inputStream = new FileInputStream(source)) {
+ copyStream(inputStream, outputStream, buffer);
+ } catch (final IOException e) {
+ throw new IllegalStateException(e);
+ }
+ } catch (final IOException e) {
+ throw new IllegalStateException(e);
+ }
+ } else { // consider file defines a zip whatever extension it is
+ try (final ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(target))) {
+ outputStream.setLevel(compressionLevel);
+
+ final byte[] buffer = new byte[BUFFER_SIZE];
+ try (final FileInputStream inputStream = new FileInputStream(source)) {
+ final ZipEntry zipEntry = new ZipEntry(source.getName());
+ outputStream.putNextEntry(zipEntry);
+ copyStream(inputStream, outputStream, buffer);
+ } catch (final IOException e) {
+ throw new IllegalStateException(e);
+ }
+ } catch (final IOException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+ try {
+ if (!Files.deleteIfExists(source.toPath())) {
+ reportError("Can't delete " + source.getAbsolutePath() + ".", null, ErrorManager.GENERIC_FAILURE);
+ }
+ } catch (final IOException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+
+ private static void copyStream(final InputStream inputStream, final OutputStream outputStream, final byte[] buffer) throws IOException {
+ int n;
+ while ((n = inputStream.read(buffer)) != -1) {
+ outputStream.write(buffer, 0, n);
+ }
+ }
+
+ protected String getProperty(final String name, final String defaultValue) {
+ String value = LogManager.getLogManager().getProperty(name);
+ if (value == null) {
+ value = defaultValue;
+ } else {
+ value = value.trim();
+ }
+ return value;
+ }
+
+ protected static String replace(final String str) { // [lang3] would be good but no dep for these classes is better
+ String result = str;
+ int start = str.indexOf("${");
+ if (start >= 0) {
+ final StringBuilder builder = new StringBuilder();
+ int end = -1;
+ while (start >= 0) {
+ builder.append(str, end + 1, start);
+ end = str.indexOf('}', start + 2);
+ if (end < 0) {
+ end = start - 1;
+ break;
+ }
+
+ final String propName = str.substring(start + 2, end);
+ String replacement = !propName.isEmpty() ? System.getProperty(propName) : null;
+ if (replacement == null) {
+ replacement = System.getenv(propName);
+ }
+ if (replacement != null) {
+ builder.append(replacement);
+ } else {
+ builder.append(str, start, end + 1);
+ }
+ start = str.indexOf("${", end + 1);
+ }
+ builder.append(str, end + 1, str.length());
+ result = builder.toString();
+ }
+ return result;
+ }
+
+ private class CountingStream extends OutputStream {
+ private final OutputStream out;
+
+ private CountingStream(final OutputStream out) {
+ this.out = out;
+ written = 0;
+ }
+
+ @Override
+ public void write(final int b) throws IOException {
+ out.write(b);
+ written++;
+ }
+
+ @Override
+ public void write(final byte buff[]) throws IOException {
+ out.write(buff);
+ written += buff.length;
+ }
+
+ @Override
+ public void write(final byte buff[], final int off, final int len) throws IOException {
+ out.write(buff, off, len);
+ written += len;
+ }
+
+ @Override
+ public void flush() throws IOException {
+ out.flush();
+ }
+
+ @Override
+ public void close() throws IOException {
+ out.close();
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/tomee/blob/af575544/tomee/tomee-juli/src/main/java/org/apache/tomee/jul/handler/rotating/Size.java
----------------------------------------------------------------------
diff --git a/tomee/tomee-juli/src/main/java/org/apache/tomee/jul/handler/rotating/Size.java b/tomee/tomee-juli/src/main/java/org/apache/tomee/jul/handler/rotating/Size.java
new file mode 100644
index 0000000..be1b476
--- /dev/null
+++ b/tomee/tomee-juli/src/main/java/org/apache/tomee/jul/handler/rotating/Size.java
@@ -0,0 +1,343 @@
+/*
+ * 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.tomee.jul.handler.rotating;
+
+import static java.lang.Long.MAX_VALUE;
+
+class Size {
+ private long size;
+ private SizeUnit unit;
+
+ private Size() {
+ // no-op
+ }
+
+ private Size(final long size, final SizeUnit unit) {
+ this.size = size;
+ this.unit = unit;
+ }
+
+ Size(final String string) {
+ this(string, null);
+ }
+
+ private Size(final String string, final SizeUnit defaultUnit) {
+ final String[] strings = string.split(",| and ");
+
+ Size total = new Size();
+ for (String s : strings) {
+ final Size part = new Size();
+ s = s.trim();
+
+ final StringBuilder t = new StringBuilder();
+ final StringBuilder u = new StringBuilder();
+
+ int i = 0;
+
+ // get the number
+ for (; i < s.length(); i++) {
+ final char c = s.charAt(i);
+ if (Character.isDigit(c) || i == 0 && c == '-' || i > 0 && c == '.') {
+ t.append(c);
+ } else {
+ break;
+ }
+ }
+
+ if (t.length() == 0) {
+ invalidFormat(s);
+ }
+
+ // skip whitespace
+ for (; i < s.length(); i++) {
+ final char c = s.charAt(i);
+ if (!Character.isWhitespace(c)) {
+ break;
+ }
+ }
+
+ // get time unit text part
+ for (; i < s.length(); i++) {
+ final char c = s.charAt(i);
+ if (Character.isLetter(c)) {
+ u.append(c);
+ } else {
+ invalidFormat(s);
+ }
+ }
+
+
+ part.unit = parseUnit(u.toString());
+
+ if (part.unit == null) {
+ part.unit = defaultUnit;
+ }
+
+ final String size = t.toString();
+ if (size.contains(".")) {
+ if (part.unit == null) {
+ throw new IllegalArgumentException("unit must be specified with floating point numbers");
+ }
+ final double d = Double.parseDouble(size);
+ final long bytes = part.unit.toBytes(1);
+ part.size = (long) (bytes * d);
+ part.unit = SizeUnit.BYTES;
+ } else {
+ part.size = Integer.parseInt(size);
+ }
+
+ total = total.add(part);
+ }
+
+ this.size = total.size;
+ this.unit = total.unit;
+ }
+
+ public long asBytes() {
+ return unit.toBytes(size);
+ }
+
+ private static class Normalize {
+ private long a;
+ private long b;
+ private SizeUnit base;
+
+ private Normalize(final Size a, final Size b) {
+ this.base = lowest(a, b);
+ this.a = a.unit == null ? a.size : base.convert(a.size, a.unit);
+ this.b = b.unit == null ? b.size : base.convert(b.size, b.unit);
+ }
+
+ private static SizeUnit lowest(final Size a, final Size b) {
+ if (a.unit == null) return b.unit;
+ if (b.unit == null) return a.unit;
+ if (a.size == 0) return b.unit;
+ if (b.size == 0) return a.unit;
+ return SizeUnit.values()[Math.min(a.unit.ordinal(), b.unit.ordinal())];
+ }
+ }
+
+ public Size add(final Size that) {
+ final Normalize n = new Normalize(this, that);
+ return new Size(n.a + n.b, n.base);
+ }
+
+ private static void invalidFormat(final String text) {
+ throw new IllegalArgumentException("Illegal size format: '" + text + "'. Valid examples are '10kb' or '10 kilobytes'.");
+ }
+
+ private static SizeUnit parseUnit(final String u) {
+ if (u.length() == 0) return null;
+
+ if ("BYTES".equalsIgnoreCase(u)) return SizeUnit.BYTES;
+ if ("BYTE".equalsIgnoreCase(u)) return SizeUnit.BYTES;
+ if ("B".equalsIgnoreCase(u)) return SizeUnit.BYTES;
+
+ if ("KILOBYTES".equalsIgnoreCase(u)) return SizeUnit.KILOBYTES;
+ if ("KILOBYTE".equalsIgnoreCase(u)) return SizeUnit.KILOBYTES;
+ if ("KILO".equalsIgnoreCase(u)) return SizeUnit.KILOBYTES;
+ if ("KB".equalsIgnoreCase(u)) return SizeUnit.KILOBYTES;
+ if ("K".equalsIgnoreCase(u)) return SizeUnit.KILOBYTES;
+
+ if ("MEGABYTES".equalsIgnoreCase(u)) return SizeUnit.MEGABYTES;
+ if ("MEGABYTE".equalsIgnoreCase(u)) return SizeUnit.MEGABYTES;
+ if ("MEGA".equalsIgnoreCase(u)) return SizeUnit.MEGABYTES;
+ if ("MB".equalsIgnoreCase(u)) return SizeUnit.MEGABYTES;
+ if ("M".equalsIgnoreCase(u)) return SizeUnit.MEGABYTES;
+
+ if ("GIGABYTES".equalsIgnoreCase(u)) return SizeUnit.GIGABYTES;
+ if ("GIGABYTE".equalsIgnoreCase(u)) return SizeUnit.GIGABYTES;
+ if ("GIGA".equalsIgnoreCase(u)) return SizeUnit.GIGABYTES;
+ if ("GB".equalsIgnoreCase(u)) return SizeUnit.GIGABYTES;
+ if ("G".equalsIgnoreCase(u)) return SizeUnit.GIGABYTES;
+
+ throw new IllegalArgumentException("Unknown size unit '" + u + "'");
+ }
+
+ private enum SizeUnit {
+ BYTES {
+ public long toBytes(final long s) {
+ return s;
+ }
+
+ public long toKilobytes(final long s) {
+ return s / (B1 / B0);
+ }
+
+ public long toMegabytes(final long s) {
+ return s / (B2 / B0);
+ }
+
+ public long toGigabytes(final long s) {
+ return s / (B3 / B0);
+ }
+
+ public long toTerabytes(final long s) {
+ return s / (B4 / B0);
+ }
+
+ public long convert(final long s, final SizeUnit u) {
+ return u.toBytes(s);
+ }
+ },
+
+ KILOBYTES {
+ public long toBytes(final long s) {
+ return x(s, B1 / B0, MAX_VALUE / (B1 / B0));
+ }
+
+ public long toKilobytes(final long s) {
+ return s;
+ }
+
+ public long toMegabytes(final long s) {
+ return s / (B2 / B1);
+ }
+
+ public long toGigabytes(final long s) {
+ return s / (B3 / B1);
+ }
+
+ public long toTerabytes(final long s) {
+ return s / (B4 / B1);
+ }
+
+ public long convert(final long s, final SizeUnit u) {
+ return u.toKilobytes(s);
+ }
+ },
+
+ MEGABYTES {
+ public long toBytes(final long s) {
+ return x(s, B2 / B0, MAX_VALUE / (B2 / B0));
+ }
+
+ public long toKilobytes(final long s) {
+ return x(s, B2 / B1, MAX_VALUE / (B2 / B1));
+ }
+
+ public long toMegabytes(final long s) {
+ return s;
+ }
+
+ public long toGigabytes(final long s) {
+ return s / (B3 / B2);
+ }
+
+ public long toTerabytes(final long s) {
+ return s / (B4 / B2);
+ }
+
+ public long convert(final long s, final SizeUnit u) {
+ return u.toMegabytes(s);
+ }
+ },
+
+ GIGABYTES {
+ public long toBytes(final long s) {
+ return x(s, B3 / B0, MAX_VALUE / (B3 / B0));
+ }
+
+ public long toKilobytes(final long s) {
+ return x(s, B3 / B1, MAX_VALUE / (B3 / B1));
+ }
+
+ public long toMegabytes(final long s) {
+ return x(s, B3 / B2, MAX_VALUE / (B3 / B2));
+ }
+
+ public long toGigabytes(final long s) {
+ return s;
+ }
+
+ public long toTerabytes(final long s) {
+ return s / (B4 / B3);
+ }
+
+ public long convert(final long s, final SizeUnit u) {
+ return u.toGigabytes(s);
+ }
+ },
+
+ TERABYTES {
+ public long toBytes(final long s) {
+ return x(s, B4 / B0, MAX_VALUE / (B4 / B0));
+ }
+
+ public long toKilobytes(final long s) {
+ return x(s, B4 / B1, MAX_VALUE / (B4 / B1));
+ }
+
+ public long toMegabytes(final long s) {
+ return x(s, B4 / B2, MAX_VALUE / (B4 / B2));
+ }
+
+ public long toGigabytes(final long s) {
+ return x(s, B4 / B3, MAX_VALUE / (B4 / B3));
+ }
+
+ public long toTerabytes(final long s) {
+ return s;
+ }
+
+ public long convert(final long s, final SizeUnit u) {
+ return u.toTerabytes(s);
+ }
+ };
+
+ static final long B0 = 1L;
+ static final long B1 = B0 * 1024L;
+ static final long B2 = B1 * 1024L;
+ static final long B3 = B2 * 1024L;
+ static final long B4 = B3 * 1024L;
+
+
+ static long x(final long d, final long m, final long over) {
+ if (d > over) {
+ return MAX_VALUE;
+ }
+ if (d < -over) {
+ return Long.MIN_VALUE;
+ }
+ return d * m;
+ }
+
+ public long toBytes(final long size) {
+ throw new AbstractMethodError();
+ }
+
+ public long toKilobytes(final long size) {
+ throw new AbstractMethodError();
+ }
+
+ public long toMegabytes(final long size) {
+ throw new AbstractMethodError();
+ }
+
+ public long toGigabytes(final long size) {
+ throw new AbstractMethodError();
+ }
+
+ public long toTerabytes(final long size) {
+ throw new AbstractMethodError();
+ }
+
+ public long convert(final long sourceSize, final SizeUnit sourceUnit) {
+ throw new AbstractMethodError();
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/tomee/blob/af575544/tomee/tomee-juli/src/test/java/org/apache/tomee/jul/handler/rotating/ArchivingTest.java
----------------------------------------------------------------------
diff --git a/tomee/tomee-juli/src/test/java/org/apache/tomee/jul/handler/rotating/ArchivingTest.java b/tomee/tomee-juli/src/test/java/org/apache/tomee/jul/handler/rotating/ArchivingTest.java
new file mode 100644
index 0000000..77da90b
--- /dev/null
+++ b/tomee/tomee-juli/src/test/java/org/apache/tomee/jul/handler/rotating/ArchivingTest.java
@@ -0,0 +1,215 @@
+/*
+ * 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.tomee.jul.handler.rotating;
+
+import org.apache.commons.io.IOUtils;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import java.io.File;
+import java.io.FileFilter;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+import java.util.zip.GZIPInputStream;
+import java.util.zip.ZipInputStream;
+
+import static java.util.Arrays.asList;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(Parameterized.class)
+public class ArchivingTest {
+ @Parameterized.Parameters(name = "{0}")
+ public static String[][] formats() {
+ return new String[][] { { "zip" }, { "gzip" } };
+ }
+
+ @Parameterized.Parameter(0)
+ public String format;
+
+ @Test
+ public void logAndRotate() throws IOException, NoSuchMethodException {
+ clean("target/ArchivingTest-" + format + "/logs");
+
+ final AtomicReference<String> today = new AtomicReference<>();
+ final Map<String, String> config = new HashMap<>();
+
+ // initial config
+ today.set("2015-09-01");
+ config.put("filenamePattern", "target/ArchivingTest-" + format + "/logs/test.%s.%d.log");
+ config.put("archiveDirectory", "target/ArchivingTest-" + format + "/logs/archives");
+ config.put("archiveFormat", format);
+ config.put("archiveOlderThan", "1 s");
+ config.put("limit", "10 kilobytes");
+ config.put("level", "INFO");
+ config.put("dateCheckInterval", "1 second");
+
+ final LocalFileHandler handler = new LocalFileHandler() {
+ @Override
+ protected String currentDate() {
+ return today.get();
+ }
+
+ @Override
+ protected String getProperty(final String name, final String defaultValue) {
+ final String s = config.get(name.substring(name.lastIndexOf('.') + 1));
+ return s != null ? s : defaultValue;
+ }
+ };
+
+ final String string10chars = "abcdefghij";
+ final int iterations = 950;
+ for (int i = 0; i < iterations; i++) {
+ handler.publish(new LogRecord(Level.INFO, string10chars));
+ }
+
+ today.set("2015-09-02");
+ try { // ensure we test the date
+ Thread.sleep(1500);
+ } catch (final InterruptedException e) {
+ Thread.interrupted();
+ }
+ handler.publish(new LogRecord(Level.INFO, string10chars)); // will trigger the archiving
+ handler.close();
+
+ final File logGzip = new File("target/ArchivingTest-" + format + "/logs/archives/test.2015-09-01.0.log." + format);
+ assertTrue(logGzip.getAbsolutePath(), logGzip.isFile());
+
+ if ("gzip".equals(format)) {
+ try (final GZIPInputStream gis = new GZIPInputStream(new FileInputStream("target/ArchivingTest-gzip/logs/archives/test.2015-09-01.0.log.gzip"))) {
+ final String content = IOUtils.toString(gis);
+ assertTrue(content.contains("INFO: abcdefghij\n"));
+ assertEquals(10258, content.length());
+ }
+ } else {
+ try (final ZipInputStream zis = new ZipInputStream(new FileInputStream("target/ArchivingTest-zip/logs/archives/test.2015-09-01.0.log.zip"))) {
+ assertEquals("test.2015-09-01.0.log", zis.getNextEntry().getName());
+ final String content = IOUtils.toString(zis);
+ assertTrue(content.contains("INFO: abcdefghij\n"));
+ assertEquals(10258, content.length());
+ assertNull(zis.getNextEntry());
+ }
+ }
+ }
+
+ @Test
+ public void logAndRotateAndPurge() throws IOException, NoSuchMethodException {
+ clean("target/ArchivingTestPurge-" + format + "/logs");
+
+ final AtomicReference<String> today = new AtomicReference<>();
+ final Map<String, String> config = new HashMap<>();
+
+ // initial config
+ today.set("2015-09-01");
+ config.put("filenamePattern", "target/ArchivingTestPurge-" + format + "/logs/test.%s.%d.log");
+ config.put("archiveDirectory", "target/ArchivingTestPurge-" + format + "/logs/archives");
+ config.put("archiveFormat", format);
+ config.put("archiveOlderThan", "1 s");
+ config.put("purgeOlderThan", "2 s");
+ config.put("limit", "10 kilobytes");
+ config.put("level", "INFO");
+ config.put("dateCheckInterval", "1 second");
+
+ final LocalFileHandler handler = new LocalFileHandler() {
+ @Override
+ protected String currentDate() {
+ return today.get();
+ }
+
+ @Override
+ protected String getProperty(final String name, final String defaultValue) {
+ final String s = config.get(name.substring(name.lastIndexOf('.') + 1));
+ return s != null ? s : defaultValue;
+ }
+ };
+
+ final String string10chars = "abcdefghij";
+ final int iterations = 950;
+ for (int i = 0; i < iterations; i++) {
+ handler.publish(new LogRecord(Level.INFO, string10chars));
+ }
+
+ final File logArchive = new File("target/ArchivingTestPurge-" + format + "/logs/archives/test.2015-09-01.0.log." + format);
+
+ today.set("2015-09-02");
+ try {
+ Thread.sleep(1500);
+ } catch (final InterruptedException e) {
+ Thread.interrupted();
+ }
+ handler.publish(new LogRecord(Level.INFO, string10chars)); // will trigger the archiving
+ for (int i = 0; i < 120; i++) { // async so retry
+ if (logArchive.exists()) {
+ break;
+ }
+ try {
+ Thread.sleep(250);
+ } catch (final InterruptedException e) {
+ Thread.interrupted();
+ }
+ }
+ assertTrue(logArchive.getAbsolutePath() + " was archived", logArchive.exists());
+
+ today.set("2015-09-03");
+ try {
+ Thread.sleep(2500);
+ } catch (final InterruptedException e) {
+ Thread.interrupted();
+ }
+ handler.publish(new LogRecord(Level.INFO, string10chars)); // will trigger the purging
+ handler.close();
+
+ assertFalse(logArchive.getAbsolutePath() + " was purged", logArchive.exists());
+ }
+
+ private static void clean(final String base) {
+ {
+ final File out = new File(base);
+ if (out.exists()) {
+ for (final File file : asList(out.listFiles(new FileFilter() {
+ @Override
+ public boolean accept(final File pathname) {
+ return pathname.getName().startsWith("test");
+ }
+ }))) {
+ file.delete();
+ }
+ }
+ }
+ {
+ final File out = new File(base + "/archives");
+ if (out.exists()) {
+ for (final File file : asList(out.listFiles(new FileFilter() {
+ @Override
+ public boolean accept(final File pathname) {
+ return pathname.getName().startsWith("test");
+ }
+ }))) {
+ file.delete();
+ }
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/tomee/blob/af575544/tomee/tomee-juli/src/test/java/org/apache/tomee/jul/handler/rotating/LocalFileHandlerTest.java
----------------------------------------------------------------------
diff --git a/tomee/tomee-juli/src/test/java/org/apache/tomee/jul/handler/rotating/LocalFileHandlerTest.java b/tomee/tomee-juli/src/test/java/org/apache/tomee/jul/handler/rotating/LocalFileHandlerTest.java
new file mode 100644
index 0000000..e17740d
--- /dev/null
+++ b/tomee/tomee-juli/src/test/java/org/apache/tomee/jul/handler/rotating/LocalFileHandlerTest.java
@@ -0,0 +1,128 @@
+/*
+ * 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.tomee.jul.handler.rotating;
+
+import org.apache.commons.io.IOUtils;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.FileFilter;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.logging.Formatter;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+
+import static java.lang.System.lineSeparator;
+import static java.util.Arrays.asList;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class LocalFileHandlerTest {
+ @Test
+ public void logAndRotate() throws IOException {
+ final File out = new File("target/LocalFileHandlerTest/logs/");
+ if (out.exists()) {
+ for (final File file : asList(out.listFiles(new FileFilter() {
+ @Override
+ public boolean accept(final File pathname) {
+ return pathname.getName().startsWith("test");
+ }
+ }))) {
+ file.delete();
+ }
+ }
+
+ final AtomicReference<String> today = new AtomicReference<>();
+ final Map<String, String> config = new HashMap<>();
+
+ // initial config
+ today.set("day1");
+ config.put("filenamePattern", "target/LocalFileHandlerTest/logs/test.%s.%d.log");
+ config.put("limit", "10 kilobytes");
+ config.put("level", "INFO");
+ config.put("dateCheckInterval", "1 second");
+ config.put("formatter", MessageOnlyFormatter.class.getName());
+
+ final LocalFileHandler handler = new LocalFileHandler() {
+ @Override
+ protected String currentDate() {
+ return today.get();
+ }
+
+ @Override
+ protected String getProperty(final String name, final String defaultValue) {
+ final String s = config.get(name.substring(name.lastIndexOf('.') + 1));
+ return s != null ? s : defaultValue;
+ }
+ };
+
+ final String string10chars = "abcdefghij";
+ final int iterations = 950;
+ for (int i = 0; i < iterations; i++) {
+ handler.publish(new LogRecord(Level.INFO, string10chars));
+ }
+
+ final File[] logFiles = out.listFiles(new FileFilter() {
+ @Override
+ public boolean accept(final File pathname) {
+ return pathname.getName().startsWith("test");
+ }
+ });
+ final Set<String> logFilesNames = new HashSet<>();
+ for (final File f : logFiles) {
+ logFilesNames.add(f.getName());
+ }
+ assertEquals(2, logFiles.length);
+ assertEquals(new HashSet<>(asList("test.day1.0.log", "test.day1.1.log")), logFilesNames);
+
+ try (final InputStream is = new FileInputStream(new File(out, "test.day1.1.log"))) {
+ final List<String> lines = IOUtils.readLines(is);
+ assertEquals(19, lines.size());
+ assertEquals(string10chars, lines.iterator().next());
+ }
+
+ final long firstFileLen = new File(out, "test.day1.0.log").length();
+ assertTrue(firstFileLen >= 1024 * 10 && firstFileLen < 1024 * 10 + (1 + string10chars.getBytes().length));
+
+ // now change of day
+ today.set("day2");
+ try { // ensure we tets the date
+ Thread.sleep(1500);
+ } catch (final InterruptedException e) {
+ Thread.interrupted();
+ }
+ handler.publish(new LogRecord(Level.INFO, string10chars));
+ assertTrue(new File(out, "test.day2.0.log").exists());
+
+ handler.close();
+ }
+
+ public static class MessageOnlyFormatter extends Formatter {
+ @Override
+ public String format(final LogRecord record) {
+ return record.getMessage() + lineSeparator();
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/tomee/blob/af575544/tomee/tomee-juli/src/test/java/org/apache/tomee/jul/handler/rotating/PerfRunner.java
----------------------------------------------------------------------
diff --git a/tomee/tomee-juli/src/test/java/org/apache/tomee/jul/handler/rotating/PerfRunner.java b/tomee/tomee-juli/src/test/java/org/apache/tomee/jul/handler/rotating/PerfRunner.java
new file mode 100644
index 0000000..8ef6644
--- /dev/null
+++ b/tomee/tomee-juli/src/test/java/org/apache/tomee/jul/handler/rotating/PerfRunner.java
@@ -0,0 +1,109 @@
+package org.apache.tomee.jul.handler.rotating;
+
+import org.apache.juli.OneLineFormatter;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.Setup;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.TearDown;
+import org.openjdk.jmh.runner.Runner;
+import org.openjdk.jmh.runner.RunnerException;
+import org.openjdk.jmh.runner.options.OptionsBuilder;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.logging.Handler;
+import java.util.logging.Logger;
+
+/*
+#1 thread
+Benchmark Mode Cnt Score Error Units
+PerfRunner.bufferizedLogger thrpt 5 70875.992 ± 14974.425 ops/s
+PerfRunner.defaultLogger thrpt 5 54832.426 ± 11709.029 ops/s
+
+#30 threads
+Benchmark Mode Cnt Score Error Units
+PerfRunner.bufferizedLogger thrpt 5 123684.947 ± 103294.959 ops/s
+PerfRunner.defaultLogger thrpt 5 62014.127 ± 36682.710 ops/s
+ */
+@State(Scope.Benchmark)
+public class PerfRunner {
+ private Logger defaultLogger;
+ private Logger bufferizedLogger;
+
+ @Setup
+ public void setup() {
+ {
+ defaultLogger = Logger.getLogger("perf.logger.default");
+ cleanHandlers(defaultLogger);
+
+ final Map<String, String> config = new HashMap<>();
+
+ // initial config
+ config.put("filenamePattern", "target/PerfRunner/logs/performance.default.%s.%02d.log");
+ config.put("limit", "10 Mega");
+ config.put("formatter", OneLineFormatter.class.getName());
+ defaultLogger.addHandler(new LocalFileHandler() {
+ @Override
+ protected String getProperty(final String name, final String defaultValue) {
+ final String key = name.substring(name.lastIndexOf('.') + 1);
+ return config.containsKey(key) ? config.get(key) : defaultValue;
+ }
+ });
+ }
+ {
+ bufferizedLogger = Logger.getLogger("perf.logger.buffer");
+ cleanHandlers(bufferizedLogger);
+
+ final Map<String, String> config = new HashMap<>();
+
+ // initial config
+ config.put("filenamePattern", "target/PerfRunner/logs/performance.buffer.%s.%02d.log");
+ config.put("limit", "10 Mega");
+ config.put("bufferSize", "1 Mega");
+ config.put("formatter", OneLineFormatter.class.getName());
+ bufferizedLogger.addHandler(new LocalFileHandler() {
+ @Override
+ protected String getProperty(final String name, final String defaultValue) {
+ final String key = name.substring(name.lastIndexOf('.') + 1);
+ return config.containsKey(key) ? config.get(key) : defaultValue;
+ }
+ });
+ }
+ }
+
+ @TearDown
+ public void tearDown() {
+ defaultLogger.getHandlers()[0].close();
+ bufferizedLogger.getHandlers()[0].close();
+ }
+
+ private void cleanHandlers(final Logger logger) {
+ for (final Handler h : logger.getHandlers()) {
+ logger.removeHandler(h);
+ }
+ logger.setUseParentHandlers(false);
+ }
+
+ @Benchmark
+ public void defaultLogger() {
+ defaultLogger.info("something happens here and nowhere else so i need to write it down");
+ }
+
+
+ @Benchmark
+ public void bufferizedLogger() {
+ bufferizedLogger.info("something happens here and nowhere else so i need to write it down");
+ }
+
+ public static void main(final String[] args) throws RunnerException {
+ new Runner(new OptionsBuilder()
+ .include(PerfRunner.class.getSimpleName())
+ .forks(0)
+ .warmupIterations(5)
+ .measurementIterations(5)
+ .threads(1)
+ .build())
+ .run();
+ }
+}