use of org.apache.logging.log4j.core.time.MutableInstant in project logging-log4j2 by apache.
the class InstantFormatter method patternSupported.
/**
* Checks if the provided formatter output matches with the one generated by
* {@link DateTimeFormatter}.
*/
private static boolean patternSupported(final String pattern, final Locale locale, final TimeZone timeZone, final Formatter formatter) {
final DateTimeFormatter javaFormatter = DateTimeFormatter.ofPattern(pattern).withLocale(locale).withZone(timeZone.toZoneId());
final MutableInstant instant = new MutableInstant();
instant.initFromEpochSecond(// 2021-05-17 21:41:10
1621280470, // precision.
123_456_789);
final String expectedFormat = javaFormatter.format(instant);
final StringBuilder stringBuilder = new StringBuilder();
formatter.format(instant, stringBuilder);
final String actualFormat = stringBuilder.toString();
return expectedFormat.equals(actualFormat);
}
use of org.apache.logging.log4j.core.time.MutableInstant in project logging-log4j2 by apache.
the class SystemClock method init.
/**
* {@inheritDoc}
*/
@Override
public void init(final MutableInstant mutableInstant) {
if (USE_PRECISE_CLOCK) {
final Instant instant = java.time.Clock.systemUTC().instant();
mutableInstant.initFromEpochSecond(instant.getEpochSecond(), instant.getNano());
} else {
mutableInstant.initFromEpochMilli(System.currentTimeMillis(), 0);
}
}
use of org.apache.logging.log4j.core.time.MutableInstant in project logging-log4j2 by apache.
the class DatePatternConverter method format.
public void format(final long epochMilli, final StringBuilder output) {
final MutableInstant instant = getMutableInstant();
instant.initFromEpochMilli(epochMilli, 0);
format(instant, output);
}
use of org.apache.logging.log4j.core.time.MutableInstant in project logging-log4j2 by apache.
the class DatePatternConverter method fromEpochMillis.
private CachedTime fromEpochMillis(final long epochMillis) {
final MutableInstant temp = new MutableInstant();
temp.initFromEpochMilli(epochMillis, 0);
return new CachedTime(temp);
}
use of org.apache.logging.log4j.core.time.MutableInstant in project logging-log4j2 by apache.
the class JsonTemplateLayoutTest method test_inline_template.
@Test
void test_inline_template() {
// Create the log event.
final SimpleMessage message = new SimpleMessage("Hello, World");
final String formattedInstant = "2017-09-28T17:13:29.098Z";
final TemporalAccessor instantAccessor = Instant.parse(formattedInstant);
final long instantEpochSeconds = instantAccessor.getLong(ChronoField.INSTANT_SECONDS);
final int instantEpochSecondsNanos = instantAccessor.get(ChronoField.NANO_OF_SECOND);
final MutableInstant instant = new MutableInstant();
instant.initFromEpochSecond(instantEpochSeconds, instantEpochSecondsNanos);
final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(LOGGER_NAME).setLevel(Level.INFO).setMessage(message).setInstant(instant).build();
// Create the event template.
final String timestampFieldName = "@timestamp";
final String staticFieldName = "staticFieldName";
final String staticFieldValue = "staticFieldValue";
final String eventTemplate = writeJson(asMap(timestampFieldName, asMap("$resolver", "timestamp", "pattern", asMap("format", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "timeZone", "UTC")), staticFieldName, staticFieldValue));
// Create the layout.
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
// Check the serialized event.
usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
assertThat(accessor.getString(timestampFieldName)).isEqualTo(formattedInstant);
assertThat(accessor.getString(staticFieldName)).isEqualTo(staticFieldValue);
});
}
Aggregations