From a939d7635cf061e5319166a084a825ca0df3732b Mon Sep 17 00:00:00 2001 From: Praveena G Date: Mon, 4 Sep 2017 14:45:37 +0100 Subject: [PATCH] fixed feedback - date format in ISO 8601. --- .../neo4j/bolt/logging/BoltMessageLog.java | 7 ++- .../java/org/neo4j/logging/FormattedLog.java | 44 +++++++++++++------ .../neo4j/logging/FormattedLogProvider.java | 28 +++++++++--- .../logging/FormattedLogProviderTest.java | 12 +++-- .../org/neo4j/logging/FormattedLogTest.java | 7 +-- 5 files changed, 71 insertions(+), 27 deletions(-) diff --git a/community/bolt/src/main/java/org/neo4j/bolt/logging/BoltMessageLog.java b/community/bolt/src/main/java/org/neo4j/bolt/logging/BoltMessageLog.java index c735b37ed9a3..b73c0d0ac8ae 100644 --- a/community/bolt/src/main/java/org/neo4j/bolt/logging/BoltMessageLog.java +++ b/community/bolt/src/main/java/org/neo4j/bolt/logging/BoltMessageLog.java @@ -22,6 +22,8 @@ import java.io.File; import java.io.IOException; +import java.text.DateFormat; +import java.text.SimpleDateFormat; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; @@ -45,8 +47,9 @@ public BoltMessageLog( FileSystemAbstraction fileSystem, File logFile, Executor { RotatingFileOutputStreamSupplier outputStreamSupplier = new RotatingFileOutputStreamSupplier( fileSystem, logFile, ROTATION_THRESHOLD_BYTES, ROTATION_DELAY_MS, MAX_ARCHIVES, executor ); - - FormattedLog formattedLog = FormattedLog.withUTCTimeZone().toOutputStream( outputStreamSupplier ); + DateFormat iso8601DateFormat = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSSZ" ); + FormattedLog formattedLog = FormattedLog.withUTCTimeZone().withDateFormat( iso8601DateFormat ) + .toOutputStream( outputStreamSupplier ); formattedLog.setLevel( Level.DEBUG ); this.inner = formattedLog; diff --git a/community/logging/src/main/java/org/neo4j/logging/FormattedLog.java b/community/logging/src/main/java/org/neo4j/logging/FormattedLog.java index 13ef237f49a5..6c245e9c7cf3 100644 --- a/community/logging/src/main/java/org/neo4j/logging/FormattedLog.java +++ b/community/logging/src/main/java/org/neo4j/logging/FormattedLog.java @@ -45,6 +45,7 @@ public class FormattedLog extends AbstractLog static final Function OUTPUT_STREAM_CONVERTER = outputStream -> new PrintWriter( new OutputStreamWriter( outputStream, StandardCharsets.UTF_8 ) ); static final TimeZone UTC = TimeZone.getTimeZone( "UTC" ); + public static final DateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss.SSSZ" ); /** * A Builder for a {@link FormattedLog} @@ -56,6 +57,7 @@ public static class Builder private String category; private Level level = Level.INFO; private boolean autoFlush = true; + private DateFormat dateFormat = SIMPLE_DATE_FORMAT; private Builder() { @@ -83,6 +85,18 @@ public Builder withTimeZone( TimeZone timezone ) return this; } + /** + * Set the dateFormat for datestamps in the log + * + * @param dateFormat the dateFormat to use for datestamps + * @return this builder + */ + public Builder withDateFormat( DateFormat dateFormat ) + { + this.dateFormat = dateFormat; + return this; + } + /** * Use the specified object to synchronize on. * @@ -184,13 +198,15 @@ public FormattedLog toPrintWriter( PrintWriter writer ) */ public FormattedLog toPrintWriter( Supplier writerSupplier ) { - return new FormattedLog( DEFAULT_CURRENT_DATE_SUPPLIER, writerSupplier, timezone, lock, category, level, autoFlush ); + return new FormattedLog( DEFAULT_CURRENT_DATE_SUPPLIER, writerSupplier, + timezone, dateFormat, lock, category, level, autoFlush ); } } private final Supplier currentDateSupplier; private final Supplier writerSupplier; private final TimeZone timezone; + private final DateFormat dateFormat; private final Object lock; private final String category; private final AtomicReference levelRef; @@ -328,6 +344,7 @@ protected FormattedLog( Supplier currentDateSupplier, Supplier writerSupplier, TimeZone timezone, + DateFormat dateFormat, Object maybeLock, String category, Level level, @@ -336,6 +353,7 @@ protected FormattedLog( this.currentDateSupplier = currentDateSupplier; this.writerSupplier = writerSupplier; this.timezone = timezone; + this.dateFormat = dateFormat; this.lock = ( maybeLock != null ) ? maybeLock : this; this.category = category; this.levelRef = new AtomicReference<>( level ); @@ -346,10 +364,10 @@ protected FormattedLog( String warnPrefix = ( category != null && !category.isEmpty() ) ? "WARN [" + category + "]" : "WARN "; String errorPrefix = ( category != null && !category.isEmpty() ) ? "ERROR [" + category + "]" : "ERROR"; - this.debugLogger = new FormattedLogger( writerSupplier, debugPrefix ); - this.infoLogger = new FormattedLogger( writerSupplier, infoPrefix ); - this.warnLogger = new FormattedLogger( writerSupplier, warnPrefix ); - this.errorLogger = new FormattedLogger( writerSupplier, errorPrefix ); + this.debugLogger = new FormattedLogger( writerSupplier, debugPrefix, dateFormat ); + this.infoLogger = new FormattedLogger( writerSupplier, infoPrefix, dateFormat ); + this.warnLogger = new FormattedLogger( writerSupplier, warnPrefix, dateFormat ); + this.errorLogger = new FormattedLogger( writerSupplier, errorPrefix, dateFormat ); } /** @@ -438,8 +456,8 @@ public void bulk( @Nonnull Consumer consumer ) synchronized ( lock ) { writer = writerSupplier.get(); - consumer.accept( new FormattedLog( currentDateSupplier, Suppliers.singleton( writer ), timezone, lock, - category, levelRef.get(), false ) ); + consumer.accept( new FormattedLog( currentDateSupplier, Suppliers.singleton( writer ), timezone, + dateFormat, lock, category, levelRef.get(), false ) ); } if ( autoFlush ) { @@ -450,14 +468,14 @@ public void bulk( @Nonnull Consumer consumer ) private class FormattedLogger extends AbstractPrintWriterLogger { private final String prefix; - private final DateFormat format; + private final DateFormat dateFormat; - FormattedLogger( @Nonnull Supplier writerSupplier, @Nonnull String prefix ) + FormattedLogger( @Nonnull Supplier writerSupplier, @Nonnull String prefix, DateFormat dateFormat ) { super( writerSupplier, lock, autoFlush ); this.prefix = prefix; - format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss.SSSZ" ); - format.setTimeZone( timezone ); + this.dateFormat = dateFormat; + this.dateFormat.setTimeZone( timezone ); } @Override @@ -485,7 +503,7 @@ protected void writeLog( @Nonnull PrintWriter out, @Nonnull String message, @Non @Override protected Logger getBulkLogger( @Nonnull PrintWriter out, @Nonnull Object lock ) { - return new FormattedLogger( Suppliers.singleton( out ), prefix ); + return new FormattedLogger( Suppliers.singleton( out ), prefix, SIMPLE_DATE_FORMAT ); } private void lineStart( PrintWriter out ) @@ -498,7 +516,7 @@ private void lineStart( PrintWriter out ) private String time() { - return format.format( currentDateSupplier.get() ); + return dateFormat.format( currentDateSupplier.get() ); } } } diff --git a/community/logging/src/main/java/org/neo4j/logging/FormattedLogProvider.java b/community/logging/src/main/java/org/neo4j/logging/FormattedLogProvider.java index 621b69d6b820..4222c171c78d 100644 --- a/community/logging/src/main/java/org/neo4j/logging/FormattedLogProvider.java +++ b/community/logging/src/main/java/org/neo4j/logging/FormattedLogProvider.java @@ -22,6 +22,7 @@ import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; +import java.text.DateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -33,6 +34,7 @@ import static org.neo4j.logging.FormattedLog.DEFAULT_CURRENT_DATE_SUPPLIER; import static org.neo4j.logging.FormattedLog.OUTPUT_STREAM_CONVERTER; +import static org.neo4j.logging.FormattedLog.SIMPLE_DATE_FORMAT; import static org.neo4j.logging.FormattedLog.UTC; /** @@ -52,6 +54,7 @@ public static class Builder private Map levels = new HashMap<>(); private Level defaultLevel = Level.INFO; private boolean autoFlush = true; + private DateFormat dateFormat = SIMPLE_DATE_FORMAT; private Builder() { @@ -90,6 +93,17 @@ public Builder withTimeZone( TimeZone timezone ) return this; } + /** + * Set the dateFormat for datestamps in the log + * + * @param dateFormat the dateFormat to use for datestamps + * @return this builder + */ + public Builder withDateFormat( DateFormat dateFormat ) + { + this.dateFormat = dateFormat; + return this; + } /** * Use the specified log {@link Level} for all {@link Log}s by default. * @@ -195,8 +209,8 @@ public FormattedLogProvider toPrintWriter( PrintWriter writer ) */ public FormattedLogProvider toPrintWriter( Supplier writerSupplier ) { - return new FormattedLogProvider( DEFAULT_CURRENT_DATE_SUPPLIER, writerSupplier, timezone, renderContext, - levels, defaultLevel, autoFlush ); + return new FormattedLogProvider( DEFAULT_CURRENT_DATE_SUPPLIER, writerSupplier, timezone, dateFormat, + renderContext, levels, defaultLevel, autoFlush ); } } @@ -207,6 +221,7 @@ public FormattedLogProvider toPrintWriter( Supplier writerSupplier private final Map levels; private final Level defaultLevel; private final boolean autoFlush; + private final DateFormat dateFormat; /** * Start creating a {@link FormattedLogProvider} which will not render the context (the class name or log name) in each output line. @@ -320,12 +335,14 @@ public static FormattedLogProvider toPrintWriter( Supplier writerSu return new Builder().toPrintWriter( writerSupplier ); } - FormattedLogProvider( Supplier currentDateSupplier, Supplier writerSupplier, TimeZone timezone,boolean renderContext, - Map levels, Level defaultLevel, boolean autoFlush ) + FormattedLogProvider( Supplier currentDateSupplier, Supplier writerSupplier, + TimeZone timezone, DateFormat dateFormat, boolean renderContext, + Map levels, Level defaultLevel, boolean autoFlush ) { this.currentDateSupplier = currentDateSupplier; this.writerSupplier = writerSupplier; this.timezone = timezone; + this.dateFormat = dateFormat; this.renderContext = renderContext; this.levels = new HashMap<>( levels ); this.defaultLevel = defaultLevel; @@ -348,7 +365,8 @@ protected FormattedLog buildLog( String name ) private FormattedLog buildLog( String context, Level level ) { - return new FormattedLog( currentDateSupplier, writerSupplier, timezone, this, renderContext ? context : null, level, autoFlush ); + return new FormattedLog( currentDateSupplier, writerSupplier, timezone, dateFormat, + this, renderContext ? context : null, level, autoFlush ); } private Level levelForContext( String context ) diff --git a/community/logging/src/test/java/org/neo4j/logging/FormattedLogProviderTest.java b/community/logging/src/test/java/org/neo4j/logging/FormattedLogProviderTest.java index 6b215d7f84f5..2d5e3102203e 100644 --- a/community/logging/src/test/java/org/neo4j/logging/FormattedLogProviderTest.java +++ b/community/logging/src/test/java/org/neo4j/logging/FormattedLogProviderTest.java @@ -19,9 +19,6 @@ */ package org.neo4j.logging; -import org.junit.Test; -import org.neo4j.function.Suppliers; - import java.io.ByteArrayOutputStream; import java.io.PrintWriter; import java.io.StringWriter; @@ -29,12 +26,19 @@ import java.util.Date; import java.util.Map; +import org.junit.Test; + +import org.neo4j.function.Suppliers; + import static java.lang.String.format; + import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.sameInstance; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; +import static org.neo4j.logging.FormattedLog.SIMPLE_DATE_FORMAT; + public class FormattedLogProviderTest { private static final Date FIXED_DATE = new Date( 467612604343L ); @@ -132,6 +136,6 @@ private static FormattedLogProvider newFormattedLogProvider( StringWriter writer { return new FormattedLogProvider( Suppliers.singleton( FIXED_DATE ), Suppliers.singleton( new PrintWriter( writer ) ), - FormattedLog.UTC, true, levels, Level.INFO, true ); + FormattedLog.UTC, SIMPLE_DATE_FORMAT, true, levels, Level.INFO, true ); } } diff --git a/community/logging/src/test/java/org/neo4j/logging/FormattedLogTest.java b/community/logging/src/test/java/org/neo4j/logging/FormattedLogTest.java index f3b306b37b62..41705335d684 100644 --- a/community/logging/src/test/java/org/neo4j/logging/FormattedLogTest.java +++ b/community/logging/src/test/java/org/neo4j/logging/FormattedLogTest.java @@ -19,16 +19,17 @@ */ package org.neo4j.logging; -import org.junit.Test; - import java.io.PrintWriter; import java.io.StringWriter; import java.util.Date; import java.util.IllegalFormatException; +import org.junit.Test; + import org.neo4j.function.Suppliers; import static java.lang.String.format; + import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.fail; @@ -192,7 +193,7 @@ private static FormattedLog newFormattedLog( StringWriter writer, Level level ) { return new FormattedLog( Suppliers.singleton( FIXED_DATE ), Suppliers.singleton( new PrintWriter( writer ) ), - FormattedLog.UTC, null, "test", level, true ); + FormattedLog.UTC, FormattedLog.SIMPLE_DATE_FORMAT, null, "test", level, true ); } private static Throwable newThrowable( final String message, final String stackTrace )