Search in sources :

Example 1 with SystemNanoClock

use of org.apache.logging.log4j.core.time.SystemNanoClock 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 2 with SystemNanoClock

use of org.apache.logging.log4j.core.time.SystemNanoClock 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 3 with SystemNanoClock

use of org.apache.logging.log4j.core.time.SystemNanoClock 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 4 with SystemNanoClock

use of org.apache.logging.log4j.core.time.SystemNanoClock 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)

Example 5 with SystemNanoClock

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

the class PatternParser method parse.

public List<PatternFormatter> parse(final String pattern, final boolean alwaysWriteExceptions, final boolean disableAnsi, final boolean noConsoleNoAnsi) {
    final List<PatternFormatter> list = new ArrayList<>();
    final List<PatternConverter> converters = new ArrayList<>();
    final List<FormattingInfo> fields = new ArrayList<>();
    parse(pattern, converters, fields, disableAnsi, noConsoleNoAnsi, true);
    final Iterator<FormattingInfo> fieldIter = fields.iterator();
    boolean handlesThrowable = false;
    for (final PatternConverter converter : converters) {
        if (converter instanceof NanoTimePatternConverter) {
            // LOG4J2-1248 set config nanoclock
            if (config != null) {
                config.setNanoClock(new SystemNanoClock());
            }
        }
        final LogEventPatternConverter pc;
        if (converter instanceof LogEventPatternConverter) {
            pc = (LogEventPatternConverter) converter;
            handlesThrowable |= pc.handlesThrowable();
        } else {
            pc = SimpleLiteralPatternConverter.of(Strings.EMPTY);
        }
        final FormattingInfo field;
        if (fieldIter.hasNext()) {
            field = fieldIter.next();
        } else {
            field = FormattingInfo.getDefault();
        }
        list.add(new PatternFormatter(pc, field));
    }
    if (alwaysWriteExceptions && !handlesThrowable) {
        final LogEventPatternConverter pc = ExtendedThrowablePatternConverter.newInstance(config, null);
        list.add(new PatternFormatter(pc, FormattingInfo.getDefault()));
    }
    return list;
}
Also used : SystemNanoClock(org.apache.logging.log4j.core.time.SystemNanoClock) ArrayList(java.util.ArrayList)

Aggregations

SystemNanoClock (org.apache.logging.log4j.core.time.SystemNanoClock)5 DummyNanoClock (org.apache.logging.log4j.core.time.internal.DummyNanoClock)4 Test (org.junit.jupiter.api.Test)3 BufferedReader (java.io.BufferedReader)2 File (java.io.File)2 FileReader (java.io.FileReader)2 Configuration (org.apache.logging.log4j.core.config.Configuration)2 NullConfiguration (org.apache.logging.log4j.core.config.NullConfiguration)2 ArrayList (java.util.ArrayList)1 Logger (org.apache.logging.log4j.Logger)1 Test (org.junit.Test)1