21
21
import static org .apache .logging .log4j .layout .template .json .TestHelpers .usingSerializedLogEventAccessor ;
22
22
import static org .assertj .core .api .Assertions .assertThat ;
23
23
24
- import java .time .Instant ;
25
- import java .time .ZoneId ;
26
- import java .time .ZonedDateTime ;
27
- import java .time .format .DateTimeFormatter ;
28
- import java .util .Locale ;
29
24
import org .apache .logging .log4j .Level ;
30
25
import org .apache .logging .log4j .core .LogEvent ;
31
26
import org .apache .logging .log4j .core .impl .ContextDataFactory ;
@@ -44,9 +39,6 @@ class GcpLayoutTest {
44
39
45
40
private static final int LOG_EVENT_COUNT = 1_000 ;
46
41
47
- private static final DateTimeFormatter DATE_TIME_FORMATTER =
48
- DateTimeFormatter .ofPattern ("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" , Locale .US );
49
-
50
42
@ Test
51
43
void test_lite_log_events () {
52
44
LogEventFixture .createLiteLogEvents (LOG_EVENT_COUNT ).forEach (GcpLayoutTest ::verifySerialization );
@@ -84,8 +76,9 @@ private static void verifySerialization(final LogEvent logEvent) {
84
76
usingSerializedLogEventAccessor (LAYOUT , logEvent , accessor -> {
85
77
86
78
// Verify timestamp.
87
- final String expectedTimestamp = formatLogEventInstant (logEvent );
88
- assertThat (accessor .getString ("timestamp" )).isEqualTo (expectedTimestamp );
79
+ final org .apache .logging .log4j .core .time .Instant instant = logEvent .getInstant ();
80
+ assertThat (accessor .getInteger ("timestampSeconds" )).isEqualTo (instant .getEpochSecond ());
81
+ assertThat (accessor .getInteger ("timestampNanos" )).isEqualTo (instant .getNanoOfSecond ());
89
82
90
83
// Verify severity.
91
84
final Level level = logEvent .getLevel ();
@@ -169,11 +162,4 @@ private static void verifySerialization(final LogEvent logEvent) {
169
162
assertThat (accessor .getString ("logger" )).isEqualTo (logEvent .getLoggerName ());
170
163
});
171
164
}
172
-
173
- private static String formatLogEventInstant (final LogEvent logEvent ) {
174
- final org .apache .logging .log4j .core .time .Instant instant = logEvent .getInstant ();
175
- final ZonedDateTime dateTime = Instant .ofEpochSecond (instant .getEpochSecond (), instant .getNanoOfSecond ())
176
- .atZone (ZoneId .of ("UTC" ));
177
- return DATE_TIME_FORMATTER .format (dateTime );
178
- }
179
165
}
0 commit comments