Search in sources :

Example 1 with DummyNanoClock

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

the class AsyncLoggerTest method testAsyncLogWritesToLog.

@Test
public void testAsyncLogWritesToLog() throws Exception {
    final File file = new File("target", "AsyncLoggerTest.log");
    // System.out.println(f.getAbsolutePath());
    file.delete();
    final AsyncLogger log = (AsyncLogger) LogManager.getLogger("com.foo.Bar");
    assertTrue(log.getNanoClock() instanceof DummyNanoClock);
    final String msg = "Async logger msg";
    log.info(msg, new InternalError("this is not a real error"));
    // stop async thread
    CoreLoggerContexts.stopLoggerContext(false, file);
    final BufferedReader reader = new BufferedReader(new FileReader(file));
    final String line1 = reader.readLine();
    reader.close();
    file.delete();
    assertNotNull("line1", line1);
    assertTrue("line1 correct", line1.contains(msg));
    final String location = "testAsyncLogWritesToLog";
    assertFalse("no location", line1.contains(location));
    assertTrue(LogManager.getFactory().isClassLoaderDependent());
}
Also used : BufferedReader(java.io.BufferedReader) DummyNanoClock(org.apache.logging.log4j.core.time.internal.DummyNanoClock) FileReader(java.io.FileReader) File(java.io.File) Test(org.junit.Test)

Example 2 with DummyNanoClock

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

the class RingBufferLogEventTest method testCreateMementoRetainsParametersAndFormat.

@Test
public void testCreateMementoRetainsParametersAndFormat() {
    final RingBufferLogEvent evt = new RingBufferLogEvent();
    // Initialize the event with parameters
    evt.swapParameters(new Object[10]);
    final String loggerName = "logger.name";
    final Marker marker = MarkerManager.getMarker("marked man");
    final String fqcn = "f.q.c.n";
    final Level level = Level.TRACE;
    final ReusableMessageFactory factory = new ReusableMessageFactory();
    final Message message = factory.newMessage("Hello {}!", "World");
    try {
        final Throwable t = new InternalError("not a real error");
        final ContextStack contextStack = new MutableThreadContextStack(Arrays.asList("a", "b"));
        final String threadName = "main";
        final StackTraceElement location = null;
        evt.setValues(null, loggerName, marker, fqcn, level, message, t, (StringMap) evt.getContextData(), contextStack, -1, threadName, -1, location, new FixedPreciseClock(12345, 678), new DummyNanoClock(1));
        ((StringMap) evt.getContextData()).putValue("key", "value");
        final Message actual = evt.createMemento().getMessage();
        assertEquals("Hello {}!", actual.getFormat());
        assertArrayEquals(new String[] { "World" }, actual.getParameters());
        assertEquals("Hello World!", actual.getFormattedMessage());
    } finally {
        ReusableMessageFactory.release(message);
    }
}
Also used : StringMap(org.apache.logging.log4j.util.StringMap) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Message(org.apache.logging.log4j.message.Message) Marker(org.apache.logging.log4j.Marker) MutableThreadContextStack(org.apache.logging.log4j.spi.MutableThreadContextStack) MutableThreadContextStack(org.apache.logging.log4j.spi.MutableThreadContextStack) ContextStack(org.apache.logging.log4j.ThreadContext.ContextStack) ReusableMessageFactory(org.apache.logging.log4j.message.ReusableMessageFactory) FixedPreciseClock(org.apache.logging.log4j.core.time.internal.FixedPreciseClock) DummyNanoClock(org.apache.logging.log4j.core.time.internal.DummyNanoClock) Level(org.apache.logging.log4j.Level) Test(org.junit.Test)

Example 3 with DummyNanoClock

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

the class RingBufferLogEventTest method testIsPopulated.

/**
 * @see <a href="https://issues.apache.org/jira/browse/LOG4J2-2816">LOG4J2-2816</a>
 */
@Test
public void testIsPopulated() {
    final RingBufferLogEvent evt = new RingBufferLogEvent();
    assertFalse(evt.isPopulated());
    final String loggerName = null;
    final Marker marker = null;
    final String fqcn = null;
    final Level level = null;
    final Message data = null;
    final Throwable t = null;
    final ContextStack contextStack = null;
    final String threadName = null;
    final StackTraceElement location = null;
    evt.setValues(null, loggerName, marker, fqcn, level, data, t, (StringMap) evt.getContextData(), contextStack, -1, threadName, -1, location, new FixedPreciseClock(), new DummyNanoClock(1));
    assertTrue(evt.isPopulated());
    evt.clear();
    assertFalse(evt.isPopulated());
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Message(org.apache.logging.log4j.message.Message) FixedPreciseClock(org.apache.logging.log4j.core.time.internal.FixedPreciseClock) DummyNanoClock(org.apache.logging.log4j.core.time.internal.DummyNanoClock) Level(org.apache.logging.log4j.Level) Marker(org.apache.logging.log4j.Marker) MutableThreadContextStack(org.apache.logging.log4j.spi.MutableThreadContextStack) ContextStack(org.apache.logging.log4j.ThreadContext.ContextStack) Test(org.junit.Test)

Example 4 with DummyNanoClock

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

the class RingBufferLogEventTest method testGetLevelReturnsOffIfNullLevelSet.

@Test
public void testGetLevelReturnsOffIfNullLevelSet() {
    final RingBufferLogEvent evt = new RingBufferLogEvent();
    final String loggerName = null;
    final Marker marker = null;
    final String fqcn = null;
    final Level level = null;
    final Message data = null;
    final Throwable t = null;
    final ContextStack contextStack = null;
    final String threadName = null;
    final StackTraceElement location = null;
    evt.setValues(null, loggerName, marker, fqcn, level, data, t, (StringMap) evt.getContextData(), contextStack, -1, threadName, -1, location, new FixedPreciseClock(), new DummyNanoClock(1));
    assertEquals(Level.OFF, evt.getLevel());
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Message(org.apache.logging.log4j.message.Message) FixedPreciseClock(org.apache.logging.log4j.core.time.internal.FixedPreciseClock) DummyNanoClock(org.apache.logging.log4j.core.time.internal.DummyNanoClock) Level(org.apache.logging.log4j.Level) Marker(org.apache.logging.log4j.Marker) MutableThreadContextStack(org.apache.logging.log4j.spi.MutableThreadContextStack) ContextStack(org.apache.logging.log4j.ThreadContext.ContextStack) Test(org.junit.Test)

Example 5 with DummyNanoClock

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

the class RingBufferLogEventTest method testMementoReuse.

@Test
public void testMementoReuse() {
    final RingBufferLogEvent evt = new RingBufferLogEvent();
    // Initialize the event with parameters
    evt.swapParameters(new Object[10]);
    final String loggerName = "logger.name";
    final Marker marker = MarkerManager.getMarker("marked man");
    final String fqcn = "f.q.c.n";
    final Level level = Level.TRACE;
    final ReusableMessageFactory factory = new ReusableMessageFactory();
    final Message message = factory.newMessage("Hello {}!", "World");
    try {
        final Throwable t = new InternalError("not a real error");
        final ContextStack contextStack = new MutableThreadContextStack(Arrays.asList("a", "b"));
        final String threadName = "main";
        final StackTraceElement location = null;
        evt.setValues(null, loggerName, marker, fqcn, level, message, t, (StringMap) evt.getContextData(), contextStack, -1, threadName, -1, location, new FixedPreciseClock(12345, 678), new DummyNanoClock(1));
        ((StringMap) evt.getContextData()).putValue("key", "value");
        final Message memento1 = evt.memento();
        final Message memento2 = evt.memento();
        assertThat(memento1, sameInstance(memento2));
    } finally {
        ReusableMessageFactory.release(message);
    }
}
Also used : StringMap(org.apache.logging.log4j.util.StringMap) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Message(org.apache.logging.log4j.message.Message) Marker(org.apache.logging.log4j.Marker) MutableThreadContextStack(org.apache.logging.log4j.spi.MutableThreadContextStack) MutableThreadContextStack(org.apache.logging.log4j.spi.MutableThreadContextStack) ContextStack(org.apache.logging.log4j.ThreadContext.ContextStack) ReusableMessageFactory(org.apache.logging.log4j.message.ReusableMessageFactory) FixedPreciseClock(org.apache.logging.log4j.core.time.internal.FixedPreciseClock) DummyNanoClock(org.apache.logging.log4j.core.time.internal.DummyNanoClock) Level(org.apache.logging.log4j.Level) Test(org.junit.Test)

Aggregations

DummyNanoClock (org.apache.logging.log4j.core.time.internal.DummyNanoClock)16 Test (org.junit.Test)11 Message (org.apache.logging.log4j.message.Message)9 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)9 Level (org.apache.logging.log4j.Level)8 Marker (org.apache.logging.log4j.Marker)8 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)8 FixedPreciseClock (org.apache.logging.log4j.core.time.internal.FixedPreciseClock)8 MutableThreadContextStack (org.apache.logging.log4j.spi.MutableThreadContextStack)8 Test (org.junit.jupiter.api.Test)5 SystemNanoClock (org.apache.logging.log4j.core.time.SystemNanoClock)4 StringMap (org.apache.logging.log4j.util.StringMap)4 BufferedReader (java.io.BufferedReader)3 File (java.io.File)3 FileReader (java.io.FileReader)3 RingBufferLogEvent (org.apache.logging.log4j.core.async.RingBufferLogEvent)2 Configuration (org.apache.logging.log4j.core.config.Configuration)2 NullConfiguration (org.apache.logging.log4j.core.config.NullConfiguration)2 ReusableMessageFactory (org.apache.logging.log4j.message.ReusableMessageFactory)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1