Skip to content

Commit

Permalink
fixed feedback - date format in ISO 8601.
Browse files Browse the repository at this point in the history
  • Loading branch information
praveenag committed Sep 4, 2017
1 parent 48ed114 commit a939d76
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 27 deletions.
Expand Up @@ -22,6 +22,8 @@


import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;


Expand All @@ -45,8 +47,9 @@ public BoltMessageLog( FileSystemAbstraction fileSystem, File logFile, Executor
{ {
RotatingFileOutputStreamSupplier outputStreamSupplier = new RotatingFileOutputStreamSupplier( fileSystem, RotatingFileOutputStreamSupplier outputStreamSupplier = new RotatingFileOutputStreamSupplier( fileSystem,
logFile, ROTATION_THRESHOLD_BYTES, ROTATION_DELAY_MS, MAX_ARCHIVES, executor ); logFile, ROTATION_THRESHOLD_BYTES, ROTATION_DELAY_MS, MAX_ARCHIVES, executor );

DateFormat iso8601DateFormat = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSSZ" );
FormattedLog formattedLog = FormattedLog.withUTCTimeZone().toOutputStream( outputStreamSupplier ); FormattedLog formattedLog = FormattedLog.withUTCTimeZone().withDateFormat( iso8601DateFormat )
.toOutputStream( outputStreamSupplier );
formattedLog.setLevel( Level.DEBUG ); formattedLog.setLevel( Level.DEBUG );


this.inner = formattedLog; this.inner = formattedLog;
Expand Down
44 changes: 31 additions & 13 deletions community/logging/src/main/java/org/neo4j/logging/FormattedLog.java
Expand Up @@ -45,6 +45,7 @@ public class FormattedLog extends AbstractLog
static final Function<OutputStream, PrintWriter> OUTPUT_STREAM_CONVERTER = static final Function<OutputStream, PrintWriter> OUTPUT_STREAM_CONVERTER =
outputStream -> new PrintWriter( new OutputStreamWriter( outputStream, StandardCharsets.UTF_8 ) ); outputStream -> new PrintWriter( new OutputStreamWriter( outputStream, StandardCharsets.UTF_8 ) );
static final TimeZone UTC = TimeZone.getTimeZone( "UTC" ); 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} * A Builder for a {@link FormattedLog}
Expand All @@ -56,6 +57,7 @@ public static class Builder
private String category; private String category;
private Level level = Level.INFO; private Level level = Level.INFO;
private boolean autoFlush = true; private boolean autoFlush = true;
private DateFormat dateFormat = SIMPLE_DATE_FORMAT;


private Builder() private Builder()
{ {
Expand Down Expand Up @@ -83,6 +85,18 @@ public Builder withTimeZone( TimeZone timezone )
return this; 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. * Use the specified object to synchronize on.
* *
Expand Down Expand Up @@ -184,13 +198,15 @@ public FormattedLog toPrintWriter( PrintWriter writer )
*/ */
public FormattedLog toPrintWriter( Supplier<PrintWriter> writerSupplier ) public FormattedLog toPrintWriter( Supplier<PrintWriter> 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<Date> currentDateSupplier; private final Supplier<Date> currentDateSupplier;
private final Supplier<PrintWriter> writerSupplier; private final Supplier<PrintWriter> writerSupplier;
private final TimeZone timezone; private final TimeZone timezone;
private final DateFormat dateFormat;
private final Object lock; private final Object lock;
private final String category; private final String category;
private final AtomicReference<Level> levelRef; private final AtomicReference<Level> levelRef;
Expand Down Expand Up @@ -328,6 +344,7 @@ protected FormattedLog(
Supplier<Date> currentDateSupplier, Supplier<Date> currentDateSupplier,
Supplier<PrintWriter> writerSupplier, Supplier<PrintWriter> writerSupplier,
TimeZone timezone, TimeZone timezone,
DateFormat dateFormat,
Object maybeLock, Object maybeLock,
String category, String category,
Level level, Level level,
Expand All @@ -336,6 +353,7 @@ protected FormattedLog(
this.currentDateSupplier = currentDateSupplier; this.currentDateSupplier = currentDateSupplier;
this.writerSupplier = writerSupplier; this.writerSupplier = writerSupplier;
this.timezone = timezone; this.timezone = timezone;
this.dateFormat = dateFormat;
this.lock = ( maybeLock != null ) ? maybeLock : this; this.lock = ( maybeLock != null ) ? maybeLock : this;
this.category = category; this.category = category;
this.levelRef = new AtomicReference<>( level ); this.levelRef = new AtomicReference<>( level );
Expand All @@ -346,10 +364,10 @@ protected FormattedLog(
String warnPrefix = ( category != null && !category.isEmpty() ) ? "WARN [" + category + "]" : "WARN "; String warnPrefix = ( category != null && !category.isEmpty() ) ? "WARN [" + category + "]" : "WARN ";
String errorPrefix = ( category != null && !category.isEmpty() ) ? "ERROR [" + category + "]" : "ERROR"; String errorPrefix = ( category != null && !category.isEmpty() ) ? "ERROR [" + category + "]" : "ERROR";


this.debugLogger = new FormattedLogger( writerSupplier, debugPrefix ); this.debugLogger = new FormattedLogger( writerSupplier, debugPrefix, dateFormat );
this.infoLogger = new FormattedLogger( writerSupplier, infoPrefix ); this.infoLogger = new FormattedLogger( writerSupplier, infoPrefix, dateFormat );
this.warnLogger = new FormattedLogger( writerSupplier, warnPrefix ); this.warnLogger = new FormattedLogger( writerSupplier, warnPrefix, dateFormat );
this.errorLogger = new FormattedLogger( writerSupplier, errorPrefix ); this.errorLogger = new FormattedLogger( writerSupplier, errorPrefix, dateFormat );
} }


/** /**
Expand Down Expand Up @@ -438,8 +456,8 @@ public void bulk( @Nonnull Consumer<Log> consumer )
synchronized ( lock ) synchronized ( lock )
{ {
writer = writerSupplier.get(); writer = writerSupplier.get();
consumer.accept( new FormattedLog( currentDateSupplier, Suppliers.singleton( writer ), timezone, lock, consumer.accept( new FormattedLog( currentDateSupplier, Suppliers.singleton( writer ), timezone,
category, levelRef.get(), false ) ); dateFormat, lock, category, levelRef.get(), false ) );
} }
if ( autoFlush ) if ( autoFlush )
{ {
Expand All @@ -450,14 +468,14 @@ public void bulk( @Nonnull Consumer<Log> consumer )
private class FormattedLogger extends AbstractPrintWriterLogger private class FormattedLogger extends AbstractPrintWriterLogger
{ {
private final String prefix; private final String prefix;
private final DateFormat format; private final DateFormat dateFormat;


FormattedLogger( @Nonnull Supplier<PrintWriter> writerSupplier, @Nonnull String prefix ) FormattedLogger( @Nonnull Supplier<PrintWriter> writerSupplier, @Nonnull String prefix, DateFormat dateFormat )
{ {
super( writerSupplier, lock, autoFlush ); super( writerSupplier, lock, autoFlush );
this.prefix = prefix; this.prefix = prefix;
format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss.SSSZ" ); this.dateFormat = dateFormat;
format.setTimeZone( timezone ); this.dateFormat.setTimeZone( timezone );
} }


@Override @Override
Expand Down Expand Up @@ -485,7 +503,7 @@ protected void writeLog( @Nonnull PrintWriter out, @Nonnull String message, @Non
@Override @Override
protected Logger getBulkLogger( @Nonnull PrintWriter out, @Nonnull Object lock ) 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 ) private void lineStart( PrintWriter out )
Expand All @@ -498,7 +516,7 @@ private void lineStart( PrintWriter out )


private String time() private String time()
{ {
return format.format( currentDateSupplier.get() ); return dateFormat.format( currentDateSupplier.get() );
} }
} }
} }
Expand Up @@ -22,6 +22,7 @@
import java.io.OutputStream; import java.io.OutputStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.Writer; import java.io.Writer;
import java.text.DateFormat;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
Expand All @@ -33,6 +34,7 @@


import static org.neo4j.logging.FormattedLog.DEFAULT_CURRENT_DATE_SUPPLIER; 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.OUTPUT_STREAM_CONVERTER;
import static org.neo4j.logging.FormattedLog.SIMPLE_DATE_FORMAT;
import static org.neo4j.logging.FormattedLog.UTC; import static org.neo4j.logging.FormattedLog.UTC;


/** /**
Expand All @@ -52,6 +54,7 @@ public static class Builder
private Map<String, Level> levels = new HashMap<>(); private Map<String, Level> levels = new HashMap<>();
private Level defaultLevel = Level.INFO; private Level defaultLevel = Level.INFO;
private boolean autoFlush = true; private boolean autoFlush = true;
private DateFormat dateFormat = SIMPLE_DATE_FORMAT;


private Builder() private Builder()
{ {
Expand Down Expand Up @@ -90,6 +93,17 @@ public Builder withTimeZone( TimeZone timezone )
return this; 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. * Use the specified log {@link Level} for all {@link Log}s by default.
* *
Expand Down Expand Up @@ -195,8 +209,8 @@ public FormattedLogProvider toPrintWriter( PrintWriter writer )
*/ */
public FormattedLogProvider toPrintWriter( Supplier<PrintWriter> writerSupplier ) public FormattedLogProvider toPrintWriter( Supplier<PrintWriter> writerSupplier )
{ {
return new FormattedLogProvider( DEFAULT_CURRENT_DATE_SUPPLIER, writerSupplier, timezone, renderContext, return new FormattedLogProvider( DEFAULT_CURRENT_DATE_SUPPLIER, writerSupplier, timezone, dateFormat,
levels, defaultLevel, autoFlush ); renderContext, levels, defaultLevel, autoFlush );
} }
} }


Expand All @@ -207,6 +221,7 @@ public FormattedLogProvider toPrintWriter( Supplier<PrintWriter> writerSupplier
private final Map<String, Level> levels; private final Map<String, Level> levels;
private final Level defaultLevel; private final Level defaultLevel;
private final boolean autoFlush; 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. * Start creating a {@link FormattedLogProvider} which will not render the context (the class name or log name) in each output line.
Expand Down Expand Up @@ -320,12 +335,14 @@ public static FormattedLogProvider toPrintWriter( Supplier<PrintWriter> writerSu
return new Builder().toPrintWriter( writerSupplier ); return new Builder().toPrintWriter( writerSupplier );
} }


FormattedLogProvider( Supplier<Date> currentDateSupplier, Supplier<PrintWriter> writerSupplier, TimeZone timezone,boolean renderContext, FormattedLogProvider( Supplier<Date> currentDateSupplier, Supplier<PrintWriter> writerSupplier,
Map<String, Level> levels, Level defaultLevel, boolean autoFlush ) TimeZone timezone, DateFormat dateFormat, boolean renderContext,
Map<String, Level> levels, Level defaultLevel, boolean autoFlush )
{ {
this.currentDateSupplier = currentDateSupplier; this.currentDateSupplier = currentDateSupplier;
this.writerSupplier = writerSupplier; this.writerSupplier = writerSupplier;
this.timezone = timezone; this.timezone = timezone;
this.dateFormat = dateFormat;
this.renderContext = renderContext; this.renderContext = renderContext;
this.levels = new HashMap<>( levels ); this.levels = new HashMap<>( levels );
this.defaultLevel = defaultLevel; this.defaultLevel = defaultLevel;
Expand All @@ -348,7 +365,8 @@ protected FormattedLog buildLog( String name )


private FormattedLog buildLog( String context, Level level ) 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 ) private Level levelForContext( String context )
Expand Down
Expand Up @@ -19,22 +19,26 @@
*/ */
package org.neo4j.logging; package org.neo4j.logging;


import org.junit.Test;
import org.neo4j.function.Suppliers;

import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.Map; import java.util.Map;


import org.junit.Test;

import org.neo4j.function.Suppliers;

import static java.lang.String.format; import static java.lang.String.format;

import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.sameInstance; import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is; import static org.hamcrest.core.Is.is;


import static org.neo4j.logging.FormattedLog.SIMPLE_DATE_FORMAT;

public class FormattedLogProviderTest public class FormattedLogProviderTest
{ {
private static final Date FIXED_DATE = new Date( 467612604343L ); private static final Date FIXED_DATE = new Date( 467612604343L );
Expand Down Expand Up @@ -132,6 +136,6 @@ private static FormattedLogProvider newFormattedLogProvider( StringWriter writer
{ {
return new FormattedLogProvider( return new FormattedLogProvider(
Suppliers.singleton( FIXED_DATE ), Suppliers.singleton( new PrintWriter( writer ) ), 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 );
} }
} }
Expand Up @@ -19,16 +19,17 @@
*/ */
package org.neo4j.logging; package org.neo4j.logging;


import org.junit.Test;

import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.Date; import java.util.Date;
import java.util.IllegalFormatException; import java.util.IllegalFormatException;


import org.junit.Test;

import org.neo4j.function.Suppliers; import org.neo4j.function.Suppliers;


import static java.lang.String.format; import static java.lang.String.format;

import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
Expand Down Expand Up @@ -192,7 +193,7 @@ private static FormattedLog newFormattedLog( StringWriter writer, Level level )
{ {
return new FormattedLog( return new FormattedLog(
Suppliers.singleton( FIXED_DATE ), Suppliers.singleton( new PrintWriter( writer ) ), 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 ) private static Throwable newThrowable( final String message, final String stackTrace )
Expand Down

0 comments on commit a939d76

Please sign in to comment.