Repository: tomee Updated Branches: refs/heads/tomee-1.7.x 23749f574 -> eb8a5afe6
PMD Project: http://git-wip-us.apache.org/repos/asf/tomee/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/eb8a5afe Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/eb8a5afe Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/eb8a5afe Branch: refs/heads/tomee-1.7.x Commit: eb8a5afe6fdb99e9f0729c943d701080dc580b85 Parents: 23749f5 Author: AndyGee <[email protected]> Authored: Mon Nov 2 19:22:40 2015 +0100 Committer: AndyGee <[email protected]> Committed: Mon Nov 2 19:22:40 2015 +0100 ---------------------------------------------------------------------- .../tomee/jul/handler/rotating/Duration.java | 159 +++++++++++++------ .../jul/handler/rotating/LocalFileHandler.java | 62 ++++---- .../apache/tomee/jul/handler/rotating/Size.java | 106 +++++++++---- 3 files changed, 221 insertions(+), 106 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/eb8a5afe/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 index 005f43a..5be6298 100644 --- 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 @@ -18,7 +18,8 @@ package org.apache.tomee.jul.handler.rotating; import java.util.concurrent.TimeUnit; -class Duration { +class + Duration { private long time; private TimeUnit unit = TimeUnit.MILLISECONDS; @@ -101,9 +102,9 @@ class Duration { } private static class Normalize { - private long a; - private long b; - private TimeUnit base; + private final long a; + private final long b; + private final TimeUnit base; private Normalize(final Duration a, final Duration b) { this.base = lowest(a, b); @@ -112,10 +113,18 @@ class Duration { } 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; + 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())]; } } @@ -127,7 +136,7 @@ class Duration { private static void invalidFormat(final String text) { throw new IllegalArgumentException("Illegal duration format: '" + text + - "'. Valid examples are '10s' or '10 seconds'."); + "'. Valid examples are '10s' or '10 seconds'."); } private static TimeUnit parseUnit(final String u) { @@ -135,42 +144,102 @@ class Duration { 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; + 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/eb8a5afe/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 index e4217ad..c8886a8 100644 --- 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 @@ -50,27 +50,27 @@ 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: for simplicity the prefix `org.apache.tomee.jul.handler.rotating.LocalFileHandler.` has been removed of name columns. + * <p/> + * |=== + * | 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). + * |=== + * <p/> + * 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. + * <p/> * 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. @@ -207,7 +207,7 @@ public class LocalFileHandler extends Handler { writerLock.readLock().lock(); rotateIfNeeded(tsDate); - String result; + final String result; try { result = getFormatter().format(record); } catch (final Exception e) { @@ -266,7 +266,7 @@ public class LocalFileHandler extends Handler { } private boolean shouldRotate(final String currentDate) { // new day, new file or limit exceeded - return (currentDate != null && !date.equals(currentDate)) || (limit > 0 && written >= limit); + return currentDate != null && !date.equals(currentDate) || limit > 0 && written >= limit; } @Override @@ -327,7 +327,7 @@ public class LocalFileHandler extends Handler { 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 = 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); @@ -380,7 +380,7 @@ public class LocalFileHandler extends Handler { 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 + // Do not try to delete on exit cause we will find it again reportError("Can't delete " + archive.getAbsolutePath() + ".", null, ErrorManager.GENERIC_FAILURE); } } @@ -392,12 +392,12 @@ public class LocalFileHandler extends Handler { } 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(); - } - }); + .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) { http://git-wip-us.apache.org/repos/asf/tomee/blob/eb8a5afe/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 index be1b476..d0de8f0 100644 --- 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 @@ -112,9 +112,9 @@ class Size { } private static class Normalize { - private long a; - private long b; - private SizeUnit base; + private final long a; + private final long b; + private final SizeUnit base; private Normalize(final Size a, final Size b) { this.base = lowest(a, b); @@ -123,10 +123,18 @@ class Size { } 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; + 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())]; } } @@ -141,29 +149,67 @@ class Size { } 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; + 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 + "'"); }
