Search in sources :

Example 1 with SystemClock

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

the class ClockFactory method createClock.

private static Clock createClock() {
    final String userRequest = PropertiesUtil.getProperties().getStringProperty(PROPERTY_NAME);
    if (userRequest == null) {
        LOGGER.trace("Using default SystemClock for timestamps.");
        return logSupportedPrecision(new SystemClock());
    }
    final Supplier<Clock> specified = aliases().get(userRequest);
    if (specified != null) {
        LOGGER.trace("Using specified {} for timestamps.", userRequest);
        return logSupportedPrecision(specified.get());
    }
    try {
        final Clock result = Loader.newCheckedInstanceOf(userRequest, Clock.class);
        LOGGER.trace("Using {} for timestamps.", result.getClass().getName());
        return logSupportedPrecision(result);
    } catch (final Exception e) {
        final String fmt = "Could not create {}: {}, using default SystemClock for timestamps.";
        LOGGER.error(fmt, userRequest, e);
        return logSupportedPrecision(new SystemClock());
    }
}
Also used : SystemClock(org.apache.logging.log4j.core.time.internal.SystemClock) CachedClock(org.apache.logging.log4j.core.time.internal.CachedClock) SystemMillisClock(org.apache.logging.log4j.core.time.internal.SystemMillisClock) CoarseCachedClock(org.apache.logging.log4j.core.time.internal.CoarseCachedClock) SystemClock(org.apache.logging.log4j.core.time.internal.SystemClock)

Example 2 with SystemClock

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

the class JsonLayoutTest method testLayoutRingBufferEventReusableMessageWithCurlyBraces.

// Test for LOG4J2-2312 LOG4J2-2341
@Test
public void testLayoutRingBufferEventReusableMessageWithCurlyBraces() throws Exception {
    final boolean propertiesAsList = false;
    final AbstractJacksonLayout layout = JsonLayout.newBuilder().setLocationInfo(false).setProperties(false).setPropertiesAsList(propertiesAsList).setComplete(false).setCompact(true).setEventEol(false).setCharset(StandardCharsets.UTF_8).setIncludeStacktrace(true).build();
    Message message = ReusableMessageFactory.INSTANCE.newMessage("Testing {}", new TestObj());
    try {
        RingBufferLogEvent ringBufferEvent = new RingBufferLogEvent();
        ringBufferEvent.setValues(null, "a.B", null, "f.q.c.n", Level.DEBUG, message, null, new SortedArrayStringMap(), ThreadContext.EMPTY_STACK, 1L, "threadName", 1, null, new SystemClock(), new DummyNanoClock());
        final String str = layout.toSerializable(ringBufferEvent);
        final String expectedMessage = "Testing " + TestObj.TO_STRING_VALUE;
        assertThat(str, containsString("\"message\":\"" + expectedMessage + '"'));
        final Log4jLogEvent actual = new Log4jJsonObjectMapper(propertiesAsList, true, false, false).readValue(str, Log4jLogEvent.class);
        assertEquals(expectedMessage, actual.getMessage().getFormattedMessage());
    } finally {
        ReusableMessageFactory.release(message);
    }
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Message(org.apache.logging.log4j.message.Message) SystemClock(org.apache.logging.log4j.core.time.internal.SystemClock) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) AbstractJacksonLayout(org.apache.logging.log4j.jackson.AbstractJacksonLayout) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) DummyNanoClock(org.apache.logging.log4j.core.time.internal.DummyNanoClock) Log4jJsonObjectMapper(org.apache.logging.log4j.jackson.json.Log4jJsonObjectMapper) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) RingBufferLogEvent(org.apache.logging.log4j.core.async.RingBufferLogEvent) Test(org.junit.Test)

Aggregations

SystemClock (org.apache.logging.log4j.core.time.internal.SystemClock)2 RingBufferLogEvent (org.apache.logging.log4j.core.async.RingBufferLogEvent)1 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)1 CachedClock (org.apache.logging.log4j.core.time.internal.CachedClock)1 CoarseCachedClock (org.apache.logging.log4j.core.time.internal.CoarseCachedClock)1 DummyNanoClock (org.apache.logging.log4j.core.time.internal.DummyNanoClock)1 SystemMillisClock (org.apache.logging.log4j.core.time.internal.SystemMillisClock)1 AbstractJacksonLayout (org.apache.logging.log4j.jackson.AbstractJacksonLayout)1 Log4jJsonObjectMapper (org.apache.logging.log4j.jackson.json.Log4jJsonObjectMapper)1 Message (org.apache.logging.log4j.message.Message)1 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)1 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)1 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)1 SortedArrayStringMap (org.apache.logging.log4j.util.SortedArrayStringMap)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 Test (org.junit.Test)1