Search in sources :

Example 11 with MutableInstant

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);
}
Also used : MutableInstant(org.apache.logging.log4j.core.time.MutableInstant) DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 12 with MutableInstant

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);
    }
}
Also used : MutableInstant(org.apache.logging.log4j.core.time.MutableInstant) Instant(java.time.Instant)

Example 13 with MutableInstant

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);
}
Also used : MutableInstant(org.apache.logging.log4j.core.time.MutableInstant)

Example 14 with MutableInstant

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);
}
Also used : MutableInstant(org.apache.logging.log4j.core.time.MutableInstant)

Example 15 with MutableInstant

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);
    });
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) MutableInstant(org.apache.logging.log4j.core.time.MutableInstant) Test(org.junit.jupiter.api.Test)

Aggregations

MutableInstant (org.apache.logging.log4j.core.time.MutableInstant)16 LogEvent (org.apache.logging.log4j.core.LogEvent)3 Test (org.junit.jupiter.api.Test)3 Instant (java.time.Instant)2 TemporalAccessor (java.time.temporal.TemporalAccessor)2 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)2 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)2 Test (org.junit.Test)2 Benchmark (org.openjdk.jmh.annotations.Benchmark)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 MappingIterator (com.fasterxml.jackson.databind.MappingIterator)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 PrintStream (java.io.PrintStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 BigDecimal (java.math.BigDecimal)1 ServerSocket (java.net.ServerSocket)1