Search in sources :

Example 1 with MutableInstant

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)));
    });
}
Also used : ParameterizedMessageFactory(org.apache.logging.log4j.message.ParameterizedMessageFactory) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Arrays(java.util.Arrays) PluginFactory(org.apache.logging.log4j.plugins.PluginFactory) EventResolverContext(org.apache.logging.log4j.layout.template.json.resolver.EventResolverContext) MappingIterator(com.fasterxml.jackson.databind.MappingIterator) EventResolver(org.apache.logging.log4j.layout.template.json.resolver.EventResolver) ReusableMessageFactory(org.apache.logging.log4j.message.ReusableMessageFactory) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Level(org.apache.logging.log4j.Level) LogEvent(org.apache.logging.log4j.core.LogEvent) ByteBuffer(java.nio.ByteBuffer) Configuration(org.apache.logging.log4j.core.config.Configuration) ServerSocket(java.net.ServerSocket) BigDecimal(java.math.BigDecimal) ConfigurationBuilderFactory(org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) StringMap(org.apache.logging.log4j.util.StringMap) Assertions(org.assertj.core.api.Assertions) JsonNode(com.fasterxml.jackson.databind.JsonNode) TemplateResolver(org.apache.logging.log4j.layout.template.json.resolver.TemplateResolver) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) JsonWriter(org.apache.logging.log4j.layout.template.json.util.JsonWriter) BlockingQueue(java.util.concurrent.BlockingQueue) Instant(java.time.Instant) EOFException(java.io.EOFException) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) TemplateResolverConfig(org.apache.logging.log4j.layout.template.json.resolver.TemplateResolverConfig) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) MarkerManager(org.apache.logging.log4j.MarkerManager) List(java.util.List) Strings(org.apache.logging.log4j.util.Strings) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IntStream(java.util.stream.IntStream) ChronoField(java.time.temporal.ChronoField) TemplateResolverFactory(org.apache.logging.log4j.layout.template.json.resolver.TemplateResolverFactory) Socket(java.net.Socket) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) Severity(org.apache.logging.log4j.core.net.Severity) TemporalAccessor(java.time.temporal.TemporalAccessor) ArrayList(java.util.ArrayList) EventTemplateAdditionalField(org.apache.logging.log4j.layout.template.json.JsonTemplateLayout.EventTemplateAdditionalField) Message(org.apache.logging.log4j.message.Message) Charset(java.nio.charset.Charset) SocketAppender(org.apache.logging.log4j.core.appender.SocketAppender) MutableInstant(org.apache.logging.log4j.core.time.MutableInstant) ByteBufferDestination(org.apache.logging.log4j.core.layout.ByteBufferDestination) Marker(org.apache.logging.log4j.Marker) TestHelpers(org.apache.logging.log4j.layout.template.json.TestHelpers) PrintStream(java.io.PrintStream) MessageFactory(org.apache.logging.log4j.message.MessageFactory) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) EventResolverFactory(org.apache.logging.log4j.layout.template.json.resolver.EventResolverFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) MainMapLookup(org.apache.logging.log4j.core.lookup.MainMapLookup) TimeUnit(java.util.concurrent.TimeUnit) AvailablePortFinder(org.apache.logging.log4j.core.test.AvailablePortFinder) Plugin(org.apache.logging.log4j.plugins.Plugin) Collections(java.util.Collections) InputStream(java.io.InputStream) 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) BigDecimal(java.math.BigDecimal) Level(org.apache.logging.log4j.Level) Map(java.util.Map) StringMap(org.apache.logging.log4j.util.StringMap) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) Test(org.junit.jupiter.api.Test)

Example 2 with MutableInstant

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");
}
Also used : MutableInstant(org.apache.logging.log4j.core.time.MutableInstant) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with MutableInstant

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

Example 4 with MutableInstant

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

Example 5 with MutableInstant

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

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