-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a DateTimeFormatter in AlfJsonLayout instead of SimpleDateFormat …
…to be thread-safe.
- Loading branch information
1 parent
7c5516c
commit 8685c62
Showing
3 changed files
with
55 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
alf-log4j2/src/test/java/io/axway/alf/log4j2/layout/AlfJsonLayoutTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package io.axway.alf.log4j2.layout; | ||
|
||
import java.text.SimpleDateFormat; | ||
import java.util.*; | ||
import org.apache.logging.log4j.core.impl.Log4jLogEvent; | ||
import org.apache.logging.log4j.core.time.MutableInstant; | ||
import org.apache.logging.log4j.message.SimpleMessage; | ||
import org.testng.annotations.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class AlfJsonLayoutTest { | ||
public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss,SSS"; | ||
|
||
@Test | ||
public void shouldFormatTime() { | ||
long now = System.currentTimeMillis(); | ||
|
||
// Given a AlfJsonLayout | ||
AlfJsonLayout layout = AlfJsonLayout.newBuilder() | ||
.withDateFormat(DATE_FORMAT) | ||
.withThreadPrinting(false) | ||
.withLevelPrinting(false) | ||
.withLoggerPrinting(false) | ||
.build(); | ||
|
||
// And a log event | ||
MutableInstant mutableInstant = new MutableInstant(); | ||
mutableInstant.initFromEpochMilli(now, 0); | ||
Log4jLogEvent event = Log4jLogEvent.newBuilder().setMessage(new SimpleMessage("Just testing")).setInstant(mutableInstant).build(); | ||
|
||
// When formatting the event | ||
String output = layout.toSerializable(event); | ||
|
||
// Then it should be the expected one (using SimpleDateFormat to ensure compatibility) | ||
String expected = "{\"time\": \"" + new SimpleDateFormat(DATE_FORMAT).format(new Date(now)) + "\", \"message\": \"Just testing\"}"; | ||
assertThat(output).isEqualToIgnoringNewLines(expected); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
alf-log4j2/src/test/java/io/axway/alf/log4j2/layout/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
@ParametersAreNonnullByDefault | ||
package io.axway.alf.log4j2.layout; | ||
|
||
import javax.annotation.*; |