use of co.cask.cdap.common.logging.LoggingContext in project cdap by caskdata.
the class TestFileLogging method testGetLog.
@Test
public void testGetLog() throws Exception {
// LogReader.getLog is tested in LogSaverTest for distributed mode
LoggingContext loggingContext = new FlowletLoggingContext("TFL_NS_1", "APP_1", "FLOW_1", "", "RUN1", "INSTANCE1");
FileLogReader logTail = injector.getInstance(FileLogReader.class);
LoggingTester.LogCallback logCallback1 = new LoggingTester.LogCallback();
logTail.getLogPrev(loggingContext, ReadRange.LATEST, 60, Filter.EMPTY_FILTER, logCallback1);
List<LogEvent> allEvents = logCallback1.getEvents();
Assert.assertEquals(60, allEvents.size());
List<LogEvent> events = Lists.newArrayList(logTail.getLog(loggingContext, allEvents.get(10).getLoggingEvent().getTimeStamp(), allEvents.get(15).getLoggingEvent().getTimeStamp(), Filter.EMPTY_FILTER));
Assert.assertEquals(5, events.size());
Assert.assertEquals(allEvents.get(10).getLoggingEvent().getFormattedMessage(), events.get(0).getLoggingEvent().getFormattedMessage());
Assert.assertEquals(allEvents.get(14).getLoggingEvent().getFormattedMessage(), events.get(4).getLoggingEvent().getFormattedMessage());
events = Lists.newArrayList(logTail.getLog(loggingContext, allEvents.get(0).getLoggingEvent().getTimeStamp(), allEvents.get(59).getLoggingEvent().getTimeStamp(), Filter.EMPTY_FILTER));
Assert.assertEquals(59, events.size());
Assert.assertEquals(allEvents.get(0).getLoggingEvent().getFormattedMessage(), events.get(0).getLoggingEvent().getFormattedMessage());
Assert.assertEquals(allEvents.get(58).getLoggingEvent().getFormattedMessage(), events.get(58).getLoggingEvent().getFormattedMessage());
events = Lists.newArrayList(logTail.getLog(loggingContext, allEvents.get(12).getLoggingEvent().getTimeStamp(), allEvents.get(41).getLoggingEvent().getTimeStamp(), Filter.EMPTY_FILTER));
Assert.assertEquals(29, events.size());
Assert.assertEquals(allEvents.get(12).getLoggingEvent().getFormattedMessage(), events.get(0).getLoggingEvent().getFormattedMessage());
Assert.assertEquals(allEvents.get(40).getLoggingEvent().getFormattedMessage(), events.get(28).getLoggingEvent().getFormattedMessage());
events = Lists.newArrayList(logTail.getLog(loggingContext, allEvents.get(22).getLoggingEvent().getTimeStamp(), allEvents.get(38).getLoggingEvent().getTimeStamp(), Filter.EMPTY_FILTER));
Assert.assertEquals(16, events.size());
Assert.assertEquals(allEvents.get(22).getLoggingEvent().getFormattedMessage(), events.get(0).getLoggingEvent().getFormattedMessage());
Assert.assertEquals(allEvents.get(37).getLoggingEvent().getFormattedMessage(), events.get(15).getLoggingEvent().getFormattedMessage());
events = Lists.newArrayList(logTail.getLog(loggingContext, allEvents.get(41).getLoggingEvent().getTimeStamp(), allEvents.get(59).getLoggingEvent().getTimeStamp(), Filter.EMPTY_FILTER));
Assert.assertEquals(18, events.size());
Assert.assertEquals(allEvents.get(41).getLoggingEvent().getFormattedMessage(), events.get(0).getLoggingEvent().getFormattedMessage());
Assert.assertEquals(allEvents.get(58).getLoggingEvent().getFormattedMessage(), events.get(17).getLoggingEvent().getFormattedMessage());
// Try with null run id, should get all logs for FLOW_1
LoggingContext loggingContext1 = new FlowletLoggingContext("TFL_NS_1", "APP_1", "FLOW_1", "", null, "INSTANCE1");
events = Lists.newArrayList(logTail.getLog(loggingContext1, 0, Long.MAX_VALUE, Filter.EMPTY_FILTER));
Assert.assertEquals(100, events.size());
}
use of co.cask.cdap.common.logging.LoggingContext in project cdap by caskdata.
the class TestKafkaLogging method testGetPrev.
@Test
public void testGetPrev() throws Exception {
LoggingContext loggingContext = new FlowletLoggingContext("TKL_NS_1", "APP_1", "FLOW_1", "", "RUN1", "INSTANCE1");
KafkaLogReader logReader = KAFKA_TESTER.getInjector().getInstance(KafkaLogReader.class);
LoggingTester tester = new LoggingTester();
tester.testGetPrev(logReader, loggingContext);
}
use of co.cask.cdap.common.logging.LoggingContext in project cdap by caskdata.
the class TestKafkaLogging method testGetNext.
@Test
public void testGetNext() throws Exception {
// Check with null runId and null instanceId
LoggingContext loggingContext = new FlowletLoggingContext("TKL_NS_1", "APP_1", "FLOW_1", "", "RUN1", "INSTANCE1");
KafkaLogReader logReader = KAFKA_TESTER.getInjector().getInstance(KafkaLogReader.class);
LoggingTester tester = new LoggingTester();
tester.testGetNext(logReader, loggingContext);
}
use of co.cask.cdap.common.logging.LoggingContext in project cdap by caskdata.
the class TestKafkaLogging method convertFetchedMessage.
private Map.Entry<Integer, String> convertFetchedMessage(FetchedMessage message) throws IOException {
LoggingEventSerializer serializer = new LoggingEventSerializer();
ILoggingEvent iLoggingEvent = serializer.fromBytes(message.getPayload());
LoggingContext loggingContext = LoggingContextHelper.getLoggingContext(iLoggingEvent.getMDCPropertyMap());
String key = loggingContext.getLogPartition();
// Temporary map for pretty format
Map<String, String> tempMap = new HashMap<>();
tempMap.put("Timestamp", Long.toString(iLoggingEvent.getTimeStamp()));
return Maps.immutableEntry(message.getTopicPartition().getPartition(), key);
}
use of co.cask.cdap.common.logging.LoggingContext in project cdap by caskdata.
the class LoggingTester method generateLogs.
public void generateLogs(Logger logger, LoggingContext loggingContextNs1) throws InterruptedException {
Exception e1 = new Exception("Test Exception1");
Exception e2 = new Exception("Test Exception2", e1);
LoggingContext loggingContextNs2 = replaceTag(loggingContextNs1, new Entry(NamespaceLoggingContext.TAG_NAMESPACE_ID, getNamespace2(loggingContextNs1)));
LoggingContextAccessor.setLoggingContext(loggingContextNs2);
for (int i = 0; i < 40; ++i) {
logger.warn("NS_2 Test log message {} {} {}", i, "arg1", "arg2", e2);
TimeUnit.MILLISECONDS.sleep(1);
}
LoggingContextAccessor.setLoggingContext(loggingContextNs1);
for (int i = 0; i < 20; ++i) {
logger.warn("Test log message {} {} {}", i, "arg1", "arg2", e2);
TimeUnit.MILLISECONDS.sleep(1);
}
LoggingContextAccessor.setLoggingContext(loggingContextNs2);
for (int i = 40; i < 80; ++i) {
logger.warn("NS_2 Test log message {} {} {}", i, "arg1", "arg2", e2);
TimeUnit.MILLISECONDS.sleep(1);
}
LoggingContextAccessor.setLoggingContext(loggingContextNs1);
for (int i = 20; i < 40; ++i) {
logger.warn("Test log message {} {} {}", i, "arg1", "arg2", e2);
TimeUnit.MILLISECONDS.sleep(1);
}
LoggingContextAccessor.setLoggingContext(loggingContextNs1);
for (int i = 40; i < 60; ++i) {
logger.warn("Test log message {} {} {}", i, "arg1", "arg2", e2);
TimeUnit.MILLISECONDS.sleep(1);
}
// Add logs with a different runid
LoggingContextAccessor.setLoggingContext(replaceTag(loggingContextNs1, new Entry(ApplicationLoggingContext.TAG_RUN_ID, "RUN2")));
for (int i = 40; i < 60; ++i) {
logger.warn("RUN2 Test log message {} {} {}", i, "arg1", "arg2", e2);
TimeUnit.MILLISECONDS.sleep(1);
}
// Add logs with null runid and null instanceid
LoggingContextAccessor.setLoggingContext(replaceTag(loggingContextNs1, new Entry(ApplicationLoggingContext.TAG_RUN_ID, null), new Entry(ApplicationLoggingContext.TAG_INSTANCE_ID, null)));
for (int i = 40; i < 60; ++i) {
logger.warn("NULL Test log message {} {} {}", i, "arg1", "arg2", e2);
TimeUnit.MILLISECONDS.sleep(1);
}
// Check with null runId and null instanceId
LoggingContextAccessor.setLoggingContext(replaceTag(loggingContextNs2, new Entry(ApplicationLoggingContext.TAG_RUN_ID, null), new Entry(ApplicationLoggingContext.TAG_INSTANCE_ID, null)));
for (int i = 80; i < 120; ++i) {
logger.warn("NS_2 Test log message {} {} {}", i, "arg1", "arg2", e2);
TimeUnit.MILLISECONDS.sleep(1);
}
StatusPrinter.setPrintStream(new PrintStream(System.out, true));
StatusPrinter.print((LoggerContext) LoggerFactory.getILoggerFactory());
}
Aggregations