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.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;

Expand All @@ -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;
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 =
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}
Expand All @@ -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()
{
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -184,13 +198,15 @@ public FormattedLog toPrintWriter( PrintWriter writer )
*/
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<PrintWriter> writerSupplier;
private final TimeZone timezone;
private final DateFormat dateFormat;
private final Object lock;
private final String category;
private final AtomicReference<Level> levelRef;
Expand Down Expand Up @@ -328,6 +344,7 @@ protected FormattedLog(
Supplier<Date> currentDateSupplier,
Supplier<PrintWriter> writerSupplier,
TimeZone timezone,
DateFormat dateFormat,
Object maybeLock,
String category,
Level level,
Expand All @@ -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 );
Expand All @@ -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 );
}

/**
Expand Down Expand Up @@ -438,8 +456,8 @@ public void bulk( @Nonnull Consumer<Log> 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 )
{
Expand All @@ -450,14 +468,14 @@ public void bulk( @Nonnull Consumer<Log> consumer )
private class FormattedLogger extends AbstractPrintWriterLogger
{
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 );
this.prefix = prefix;
format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss.SSSZ" );
format.setTimeZone( timezone );
this.dateFormat = dateFormat;
this.dateFormat.setTimeZone( timezone );
}

@Override
Expand Down Expand Up @@ -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 )
Expand All @@ -498,7 +516,7 @@ private void lineStart( PrintWriter out )

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.PrintWriter;
import java.io.Writer;
import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
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.OUTPUT_STREAM_CONVERTER;
import static org.neo4j.logging.FormattedLog.SIMPLE_DATE_FORMAT;
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 Level defaultLevel = Level.INFO;
private boolean autoFlush = true;
private DateFormat dateFormat = SIMPLE_DATE_FORMAT;

private Builder()
{
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -195,8 +209,8 @@ public FormattedLogProvider toPrintWriter( PrintWriter writer )
*/
public FormattedLogProvider toPrintWriter( Supplier<PrintWriter> 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 );
}
}

Expand All @@ -207,6 +221,7 @@ public FormattedLogProvider toPrintWriter( Supplier<PrintWriter> writerSupplier
private final Map<String, Level> 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.
Expand Down Expand Up @@ -320,12 +335,14 @@ public static FormattedLogProvider toPrintWriter( Supplier<PrintWriter> writerSu
return new Builder().toPrintWriter( writerSupplier );
}

FormattedLogProvider( Supplier<Date> currentDateSupplier, Supplier<PrintWriter> writerSupplier, TimeZone timezone,boolean renderContext,
Map<String, Level> levels, Level defaultLevel, boolean autoFlush )
FormattedLogProvider( Supplier<Date> currentDateSupplier, Supplier<PrintWriter> writerSupplier,
TimeZone timezone, DateFormat dateFormat, boolean renderContext,
Map<String, Level> 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;
Expand All @@ -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 )
Expand Down
Expand Up @@ -19,22 +19,26 @@
*/
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;
import java.util.Collections;
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 );
Expand Down Expand Up @@ -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 );
}
}
Expand Up @@ -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;
Expand Down Expand Up @@ -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 )
Expand Down

0 comments on commit a939d76

Please sign in to comment.