Search in sources :

Example 6 with DummyNanoClock

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

the class RingBufferLogEventTest method testSerializationDeserialization.

@SuppressWarnings("BanSerializableRead")
@Test
public void testSerializationDeserialization() throws IOException, ClassNotFoundException {
    final RingBufferLogEvent evt = new RingBufferLogEvent();
    final String loggerName = "logger.name";
    final Marker marker = null;
    final String fqcn = "f.q.c.n";
    final Level level = Level.TRACE;
    final Message data = new SimpleMessage("message");
    final Throwable t = new InternalError("not a real error");
    final ContextStack contextStack = null;
    final String threadName = "main";
    final StackTraceElement location = null;
    evt.setValues(null, loggerName, marker, fqcn, level, data, t, (StringMap) evt.getContextData(), contextStack, -1, threadName, -1, location, new FixedPreciseClock(12345, 678), new DummyNanoClock(1));
    ((StringMap) evt.getContextData()).putValue("key", "value");
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream out = new ObjectOutputStream(baos);
    out.writeObject(evt);
    final ObjectInputStream in = new FilteredObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
    final RingBufferLogEvent other = (RingBufferLogEvent) in.readObject();
    assertEquals(loggerName, other.getLoggerName());
    assertEquals(marker, other.getMarker());
    assertEquals(fqcn, other.getLoggerFqcn());
    assertEquals(level, other.getLevel());
    assertEquals(data, other.getMessage());
    assertNull("null after serialization", other.getThrown());
    assertEquals(new ThrowableProxy(t), other.getThrownProxy());
    assertEquals(evt.getContextData(), other.getContextData());
    assertEquals(contextStack, other.getContextStack());
    assertEquals(threadName, other.getThreadName());
    assertEquals(location, other.getSource());
    assertEquals(12345, other.getTimeMillis());
    assertEquals(678, other.getInstant().getNanoOfMillisecond());
}
Also used : StringMap(org.apache.logging.log4j.util.StringMap) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Message(org.apache.logging.log4j.message.Message) FilteredObjectInputStream(org.apache.logging.log4j.util.FilteredObjectInputStream) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Marker(org.apache.logging.log4j.Marker) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MutableThreadContextStack(org.apache.logging.log4j.spi.MutableThreadContextStack) ContextStack(org.apache.logging.log4j.ThreadContext.ContextStack) ObjectOutputStream(java.io.ObjectOutputStream) ThrowableProxy(org.apache.logging.log4j.core.impl.ThrowableProxy) ByteArrayInputStream(java.io.ByteArrayInputStream) FixedPreciseClock(org.apache.logging.log4j.core.time.internal.FixedPreciseClock) DummyNanoClock(org.apache.logging.log4j.core.time.internal.DummyNanoClock) Level(org.apache.logging.log4j.Level) ObjectInputStream(java.io.ObjectInputStream) FilteredObjectInputStream(org.apache.logging.log4j.util.FilteredObjectInputStream) Test(org.junit.Test)

Example 7 with DummyNanoClock

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

the class RingBufferLogEventTest method testCreateMementoReturnsCopy.

@SuppressWarnings("deprecation")
@Test
public void testCreateMementoReturnsCopy() {
    final RingBufferLogEvent evt = new RingBufferLogEvent();
    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 Message data = new SimpleMessage("message");
    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, data, t, (StringMap) evt.getContextData(), contextStack, -1, threadName, -1, location, new FixedPreciseClock(12345, 678), new DummyNanoClock(1));
    ((StringMap) evt.getContextData()).putValue("key", "value");
    final LogEvent actual = evt.createMemento();
    assertEquals(evt.getLoggerName(), actual.getLoggerName());
    assertEquals(evt.getMarker(), actual.getMarker());
    assertEquals(evt.getLoggerFqcn(), actual.getLoggerFqcn());
    assertEquals(evt.getLevel(), actual.getLevel());
    assertEquals(evt.getMessage(), actual.getMessage());
    assertEquals(evt.getThrown(), actual.getThrown());
    assertEquals(evt.getContextData(), actual.getContextData());
    assertEquals(evt.getContextStack(), actual.getContextStack());
    assertEquals(evt.getThreadName(), actual.getThreadName());
    assertEquals(evt.getTimeMillis(), actual.getTimeMillis());
    assertEquals(evt.getInstant().getNanoOfMillisecond(), actual.getInstant().getNanoOfMillisecond());
    assertEquals(evt.getSource(), actual.getSource());
    assertEquals(evt.getThrownProxy(), actual.getThrownProxy());
}
Also used : StringMap(org.apache.logging.log4j.util.StringMap) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Message(org.apache.logging.log4j.message.Message) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) 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) 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 8 with DummyNanoClock

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

the class RingBufferLogEventTest method testGetMessageReturnsNonNullMessage.

@Test
public void testGetMessageReturnsNonNullMessage() {
    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));
    assertNotNull(evt.getMessage());
}
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 9 with DummyNanoClock

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

the class Log4jLogEventNanoTimeTest method testLog4jLogEventUsesNanoTimeClock.

@Test
public void testLog4jLogEventUsesNanoTimeClock() throws Exception {
    final File file = new File("target", "NanoTimeToFileTest.log");
    // System.out.println(f.getAbsolutePath());
    file.delete();
    final Logger log = LogManager.getLogger("com.foo.Bar");
    final long before = System.nanoTime();
    log.info("Use actual System.nanoTime()");
    assertTrue(Log4jLogEvent.getNanoClock() instanceof SystemNanoClock, "using SystemNanoClock");
    final long DUMMYNANOTIME = 123;
    Log4jLogEvent.setNanoClock(new DummyNanoClock(DUMMYNANOTIME));
    log.info("Use dummy nano clock");
    assertTrue(Log4jLogEvent.getNanoClock() instanceof DummyNanoClock, "using SystemNanoClock");
    // stop async thread
    CoreLoggerContexts.stopLoggerContext(file);
    String line1;
    String line2;
    try (final BufferedReader reader = new BufferedReader(new FileReader(file))) {
        line1 = reader.readLine();
        line2 = reader.readLine();
    // System.out.println(line1);
    // System.out.println(line2);
    }
    file.delete();
    assertNotNull(line1, "line1");
    assertNotNull(line2, "line2");
    final String[] line1Parts = line1.split(" AND ");
    assertEquals("Use actual System.nanoTime()", line1Parts[2]);
    assertEquals(line1Parts[0], line1Parts[1]);
    final long loggedNanoTime = Long.parseLong(line1Parts[0]);
    assertTrue(loggedNanoTime - before < TimeUnit.SECONDS.toNanos(1), "used system nano time");
    final String[] line2Parts = line2.split(" AND ");
    assertEquals("Use dummy nano clock", line2Parts[2]);
    assertEquals(String.valueOf(DUMMYNANOTIME), line2Parts[0]);
    assertEquals(String.valueOf(DUMMYNANOTIME), line2Parts[1]);
}
Also used : SystemNanoClock(org.apache.logging.log4j.core.time.SystemNanoClock) BufferedReader(java.io.BufferedReader) DummyNanoClock(org.apache.logging.log4j.core.time.internal.DummyNanoClock) FileReader(java.io.FileReader) Logger(org.apache.logging.log4j.Logger) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 10 with DummyNanoClock

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

the class Log4jLogEventTest method testNanoTimeGeneratedByNanoClock.

@Test
public void testNanoTimeGeneratedByNanoClock() {
    Log4jLogEvent.setNanoClock(new DummyNanoClock(123));
    verifyNanoTimeWithAllConstructors(123);
    Log4jLogEvent.setNanoClock(new DummyNanoClock(87654));
    verifyNanoTimeWithAllConstructors(87654);
}
Also used : DummyNanoClock(org.apache.logging.log4j.core.time.internal.DummyNanoClock) ClockFactoryTest(org.apache.logging.log4j.core.time.ClockFactoryTest) Test(org.junit.jupiter.api.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