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]);
}
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]);
}
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);
}
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);
}
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;
}
Aggregations