use of org.apache.logging.log4j.core.time.MutableInstant in project logging-log4j2 by apache.
the class JsonTemplateLayoutTest method test_timestamp_epoch_resolvers.
@Test
@SuppressWarnings("FloatingPointLiteralPrecision")
void test_timestamp_epoch_resolvers() {
final List<Map<String, Object>> testCases = Arrays.asList(asMap("epochSecs", new BigDecimal("1581082727.982123456"), "epochSecsRounded", 1581082727, "epochSecsNanos", 982123456, "epochMillis", new BigDecimal("1581082727982.123456"), "epochMillisRounded", 1581082727982L, "epochMillisNanos", 123456, "epochNanos", 1581082727982123456L), asMap("epochSecs", new BigDecimal("1591177590.005000001"), "epochSecsRounded", 1591177590, "epochSecsNanos", 5000001, "epochMillis", new BigDecimal("1591177590005.000001"), "epochMillisRounded", 1591177590005L, "epochMillisNanos", 1, "epochNanos", 1591177590005000001L));
// Create the event template.
final String eventTemplate = writeJson(asMap("epochSecs", asMap("$resolver", "timestamp", "epoch", asMap("unit", "secs")), "epochSecsRounded", asMap("$resolver", "timestamp", "epoch", asMap("unit", "secs", "rounded", true)), "epochSecsNanos", asMap("$resolver", "timestamp", "epoch", asMap("unit", "secs.nanos")), "epochMillis", asMap("$resolver", "timestamp", "epoch", asMap("unit", "millis")), "epochMillisRounded", asMap("$resolver", "timestamp", "epoch", asMap("unit", "millis", "rounded", true)), "epochMillisNanos", asMap("$resolver", "timestamp", "epoch", asMap("unit", "millis.nanos")), "epochNanos", asMap("$resolver", "timestamp", "epoch", asMap("unit", "nanos"))));
// Create the layout.
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
testCases.forEach(testCase -> {
// Create the log event.
final SimpleMessage message = new SimpleMessage("Hello, World!");
final Level level = Level.ERROR;
final MutableInstant instant = new MutableInstant();
final Object instantSecsObject = testCase.get("epochSecsRounded");
final long instantSecs = instantSecsObject instanceof Long ? (long) instantSecsObject : (int) instantSecsObject;
final int instantSecsNanos = (int) testCase.get("epochSecsNanos");
instant.initFromEpochSecond(instantSecs, instantSecsNanos);
final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(LOGGER_NAME).setLevel(level).setMessage(message).setInstant(instant).build();
// Verify the test case.
usingSerializedLogEventAccessor(layout, logEvent, accessor -> testCase.forEach((key, expectedValue) -> Assertions.assertThat(accessor.getObject(key)).describedAs("key=%s", key).isEqualTo(expectedValue)));
});
}
use of org.apache.logging.log4j.core.time.MutableInstant in project logging-log4j2 by apache.
the class InstantFormatterTest method nanoseconds_should_be_formatted.
@Test
void nanoseconds_should_be_formatted() {
final InstantFormatter formatter = InstantFormatter.newBuilder().setPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'").setTimeZone(TimeZone.getTimeZone("UTC")).build();
MutableInstant instant = new MutableInstant();
instant.initFromEpochSecond(0, 123_456_789);
Assertions.assertThat(formatter.format(instant)).isEqualTo("1970-01-01T00:00:00.123456789Z");
}
use of org.apache.logging.log4j.core.time.MutableInstant in project logging-log4j2 by apache.
the class DateTimeFormatBenchmark method dateTimeFormatter.
@Benchmark
public void dateTimeFormatter(final Blackhole blackhole) {
for (final MutableInstant instant : INSTANTS) {
stringBuilder.setLength(0);
DATE_TIME_FORMATTER.formatTo(instant, stringBuilder);
blackhole.consume(stringBuilder.length());
}
}
use of org.apache.logging.log4j.core.time.MutableInstant in project logging-log4j2 by apache.
the class DateTimeFormatBenchmark method fixedDateFormat.
@Benchmark
public void fixedDateFormat(final Blackhole blackhole) {
for (final MutableInstant instant : INSTANTS) {
final int length = FIXED_DATE_FORMAT.formatInstant(instant, charBuffer, 0);
blackhole.consume(length);
}
}
use of org.apache.logging.log4j.core.time.MutableInstant in project logging-log4j2 by apache.
the class LogEventWrapper method getInstant.
@Override
public Instant getInstant() {
MutableInstant mutable = new MutableInstant();
mutable.initFromEpochMilli(event.getTimeStamp(), 0);
return mutable;
}
Aggregations