Search in sources :

Example 6 with MutableInstant

use of org.apache.logging.log4j.core.time.MutableInstant in project logging-log4j2 by apache.

the class DatePatternConverter method getMutableInstant.

private MutableInstant getMutableInstant() {
    if (Constants.ENABLE_THREADLOCALS) {
        MutableInstant result = threadLocalMutableInstant.get();
        if (result == null) {
            result = new MutableInstant();
            threadLocalMutableInstant.set(result);
        }
        return result;
    }
    return new MutableInstant();
}
Also used : MutableInstant(org.apache.logging.log4j.core.time.MutableInstant)

Example 7 with MutableInstant

use of org.apache.logging.log4j.core.time.MutableInstant in project logging-log4j2 by apache.

the class DatePatternConverterTest method testFormatDateStringBuilderIso8601WithPeriodMicroseconds.

@Test
public void testFormatDateStringBuilderIso8601WithPeriodMicroseconds() {
    final String[] pattern = { FixedDateFormat.FixedFormat.ISO8601_PERIOD_MICROS.name(), "Z" };
    final DatePatternConverter converter = DatePatternConverter.newInstance(pattern);
    final StringBuilder sb = new StringBuilder();
    MutableInstant instant = new MutableInstant();
    instant.initFromEpochMilli(1577225134559L, // One microsecond
    1000);
    converter.format(instant, sb);
    final String expected = "2019-12-24T22:05:34.559001";
    assertEquals(expected, sb.toString());
}
Also used : MutableInstant(org.apache.logging.log4j.core.time.MutableInstant) Test(org.junit.Test)

Example 8 with MutableInstant

use of org.apache.logging.log4j.core.time.MutableInstant in project logging-log4j2 by apache.

the class InstantAttributeConverterTest method testConvert01.

@Test
public void testConvert01() {
    final MutableInstant instant = new MutableInstant();
    instant.initFromEpochSecond(1234567, 89012);
    final String converted = this.converter.convertToDatabaseColumn(instant);
    assertNotNull("The converted value should not be null.", converted);
    assertEquals("The converted value is not correct.", "1234567,89012", converted);
    final Instant reversed = this.converter.convertToEntityAttribute(converted);
    assertNotNull("The reversed value should not be null.", reversed);
    assertEquals("epoch sec", 1234567, reversed.getEpochSecond());
    assertEquals("nanoOfSecond", 89012, reversed.getNanoOfSecond());
}
Also used : MutableInstant(org.apache.logging.log4j.core.time.MutableInstant) Instant(org.apache.logging.log4j.core.time.Instant) MutableInstant(org.apache.logging.log4j.core.time.MutableInstant) Test(org.junit.Test)

Example 9 with MutableInstant

use of org.apache.logging.log4j.core.time.MutableInstant in project logging-log4j2 by apache.

the class InstantAttributeConverter method convertToEntityAttribute.

@Override
public Instant convertToEntityAttribute(final String s) {
    if (Strings.isEmpty(s)) {
        return null;
    }
    int pos = s.indexOf(",");
    long epochSecond = Long.parseLong(s.substring(0, pos));
    int nanos = Integer.parseInt(s.substring(pos + 1, s.length()));
    MutableInstant result = new MutableInstant();
    result.initFromEpochSecond(epochSecond, nanos);
    return result;
}
Also used : MutableInstant(org.apache.logging.log4j.core.time.MutableInstant)

Example 10 with MutableInstant

use of org.apache.logging.log4j.core.time.MutableInstant in project logging-log4j2 by apache.

the class LogEventEntityTest method testToImmutable_AbstractLogEventWrapperEntity.

@Test
public void testToImmutable_AbstractLogEventWrapperEntity() {
    final LogEvent logEvent = new AbstractLogEventWrapperEntity() {

        private static final long serialVersionUID = 2L;

        @Override
        public ContextStack getContextStack() {
            return null;
        }

        @Override
        public Level getLevel() {
            return null;
        }

        @Override
        public String getLoggerFqcn() {
            return null;
        }

        @Override
        public String getLoggerName() {
            return null;
        }

        @Override
        public Marker getMarker() {
            return null;
        }

        @Override
        public Message getMessage() {
            return null;
        }

        @Override
        public Instant getInstant() {
            return new MutableInstant();
        }

        @Override
        public long getNanoTime() {
            return 0;
        }

        @Override
        public StackTraceElement getSource() {
            return null;
        }

        @Override
        public long getThreadId() {
            return 0;
        }

        @Override
        public String getThreadName() {
            return null;
        }

        @Override
        public int getThreadPriority() {
            return 0;
        }

        @Override
        public Throwable getThrown() {
            return null;
        }

        @Override
        public ThrowableProxy getThrownProxy() {
            return null;
        }

        @Override
        public long getTimeMillis() {
            return 0;
        }
    };
    Assert.assertNotSame(logEvent, logEvent.toImmutable());
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) MutableInstant(org.apache.logging.log4j.core.time.MutableInstant) Test(org.junit.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