use of org.apache.logging.log4j.core.util.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";
assertTrue("no location", !line1.contains(location));
}
use of org.apache.logging.log4j.core.util.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]);
}
use of org.apache.logging.log4j.core.util.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);
}
use of org.apache.logging.log4j.core.util.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);
}
use of org.apache.logging.log4j.core.util.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("using SystemNanoClock", Log4jLogEvent.getNanoClock() instanceof SystemNanoClock);
final long DUMMYNANOTIME = 123;
Log4jLogEvent.setNanoClock(new DummyNanoClock(DUMMYNANOTIME));
log.info("Use dummy nano clock");
assertTrue("using SystemNanoClock", Log4jLogEvent.getNanoClock() instanceof DummyNanoClock);
// 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("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]);
}
Aggregations