Search in sources :

Example 11 with DummyNanoClock

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

the class SmtpManagerTest method testAdd_WhereRingBufferLogEvent.

// LOG4J2-3172
@Test
void testAdd_WhereRingBufferLogEvent() {
    RingBufferLogEvent event = new RingBufferLogEvent();
    event.setValues(null, null, null, null, null, getReusableMessage("test message"), null, null, null, 0, null, 0, null, ClockFactory.getClock(), new DummyNanoClock());
    testAdd(event);
}
Also used : DummyNanoClock(org.apache.logging.log4j.core.time.internal.DummyNanoClock) RingBufferLogEvent(org.apache.logging.log4j.core.async.RingBufferLogEvent) Test(org.junit.jupiter.api.Test)

Example 12 with DummyNanoClock

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

the class RingBufferLogEventTest method testGetMillisReturnsConstructorMillisForNormalMessage.

@Test
public void testGetMillisReturnsConstructorMillisForNormalMessage() {
    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(123, 456), new DummyNanoClock(1));
    assertEquals(123, evt.getTimeMillis());
    assertEquals(456, evt.getInstant().getNanoOfMillisecond());
}
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 13 with DummyNanoClock

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

the class AsyncLoggerTestNanoTime method testAsyncLogUsesNanoTimeClock.

@Test
public void testAsyncLogUsesNanoTimeClock() throws Exception {
    final File file = new File("target", "NanoTimeToFileTest.log");
    // System.out.println(f.getAbsolutePath());
    file.delete();
    final AsyncLogger log = (AsyncLogger) LogManager.getLogger("com.foo.Bar");
    final long before = System.nanoTime();
    log.info("Use actual System.nanoTime()");
    assertTrue("using SystemNanoClock", log.getNanoClock() instanceof SystemNanoClock);
    final long DUMMYNANOTIME = -53;
    log.getContext().getConfiguration().setNanoClock(new DummyNanoClock(DUMMYNANOTIME));
    log.updateConfiguration(log.getContext().getConfiguration());
    // trigger a new nano clock lookup
    log.updateConfiguration(log.getContext().getConfiguration());
    log.info("Use dummy nano clock");
    assertTrue("using SystemNanoClock", log.getNanoClock() instanceof DummyNanoClock);
    // stop async thread
    CoreLoggerContexts.stopLoggerContext(file);
    final BufferedReader reader = new BufferedReader(new FileReader(file));
    final String line1 = reader.readLine();
    final String line2 = reader.readLine();
    // System.out.println(line1);
    // System.out.println(line2);
    reader.close();
    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("used system nano time", loggedNanoTime - before < TimeUnit.SECONDS.toNanos(1));
    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) File(java.io.File) Test(org.junit.Test)

Example 14 with DummyNanoClock

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

the class PatternParserTest method testNanoPatternLongChangesNanoClockFactoryMode.

@Test
public void testNanoPatternLongChangesNanoClockFactoryMode() {
    final Configuration config = new NullConfiguration();
    assertTrue(config.getNanoClock() instanceof DummyNanoClock);
    final PatternParser pp = new PatternParser(config, KEY, null);
    assertTrue(config.getNanoClock() instanceof DummyNanoClock);
    pp.parse("%m");
    assertTrue(config.getNanoClock() instanceof DummyNanoClock);
    pp.parse("%N");
    assertTrue(config.getNanoClock() instanceof SystemNanoClock);
}
Also used : SystemNanoClock(org.apache.logging.log4j.core.time.SystemNanoClock) NullConfiguration(org.apache.logging.log4j.core.config.NullConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) DummyNanoClock(org.apache.logging.log4j.core.time.internal.DummyNanoClock) NullConfiguration(org.apache.logging.log4j.core.config.NullConfiguration) Test(org.junit.jupiter.api.Test)

Example 15 with DummyNanoClock

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

the class PatternParserTest method testNanoPatternShortChangesConfigurationNanoClock.

@Test
public void testNanoPatternShortChangesConfigurationNanoClock() {
    final Configuration config = new NullConfiguration();
    assertTrue(config.getNanoClock() instanceof DummyNanoClock);
    final PatternParser pp = new PatternParser(config, KEY, null);
    assertTrue(config.getNanoClock() instanceof DummyNanoClock);
    pp.parse("%m");
    assertTrue(config.getNanoClock() instanceof DummyNanoClock);
    // this changes the config clock
    pp.parse("%nano");
    assertTrue(config.getNanoClock() instanceof SystemNanoClock);
}
Also used : SystemNanoClock(org.apache.logging.log4j.core.time.SystemNanoClock) NullConfiguration(org.apache.logging.log4j.core.config.NullConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) DummyNanoClock(org.apache.logging.log4j.core.time.internal.DummyNanoClock) NullConfiguration(org.apache.logging.log4j.core.config.NullConfiguration) 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