Search in sources :

Example 26 with LoggingContext

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());
}
Also used : LoggingTester(co.cask.cdap.logging.appender.LoggingTester) LoggingContext(co.cask.cdap.common.logging.LoggingContext) FlowletLoggingContext(co.cask.cdap.logging.context.FlowletLoggingContext) LogEvent(co.cask.cdap.logging.read.LogEvent) FlowletLoggingContext(co.cask.cdap.logging.context.FlowletLoggingContext) FileLogReader(co.cask.cdap.logging.read.FileLogReader) Test(org.junit.Test)

Example 27 with LoggingContext

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);
}
Also used : KafkaLogReader(co.cask.cdap.logging.read.KafkaLogReader) LoggingTester(co.cask.cdap.logging.appender.LoggingTester) LoggingContext(co.cask.cdap.common.logging.LoggingContext) FlowletLoggingContext(co.cask.cdap.logging.context.FlowletLoggingContext) FlowletLoggingContext(co.cask.cdap.logging.context.FlowletLoggingContext) Test(org.junit.Test)

Example 28 with 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);
}
Also used : KafkaLogReader(co.cask.cdap.logging.read.KafkaLogReader) LoggingTester(co.cask.cdap.logging.appender.LoggingTester) LoggingContext(co.cask.cdap.common.logging.LoggingContext) FlowletLoggingContext(co.cask.cdap.logging.context.FlowletLoggingContext) FlowletLoggingContext(co.cask.cdap.logging.context.FlowletLoggingContext) Test(org.junit.Test)

Example 29 with 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);
}
Also used : LoggingContext(co.cask.cdap.common.logging.LoggingContext) FlowletLoggingContext(co.cask.cdap.logging.context.FlowletLoggingContext) HashMap(java.util.HashMap) LoggingEventSerializer(co.cask.cdap.logging.serialize.LoggingEventSerializer) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent)

Example 30 with LoggingContext

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());
}
Also used : PrintStream(java.io.PrintStream) ApplicationLoggingContext(co.cask.cdap.common.logging.ApplicationLoggingContext) NamespaceLoggingContext(co.cask.cdap.common.logging.NamespaceLoggingContext) LoggingContext(co.cask.cdap.common.logging.LoggingContext)

Aggregations

LoggingContext (co.cask.cdap.common.logging.LoggingContext)30 GET (javax.ws.rs.GET)12 Path (javax.ws.rs.Path)12 Test (org.junit.Test)11 FlowletLoggingContext (co.cask.cdap.logging.context.FlowletLoggingContext)9 RunRecordMeta (co.cask.cdap.internal.app.store.RunRecordMeta)6 LoggingTester (co.cask.cdap.logging.appender.LoggingTester)5 FileLogReader (co.cask.cdap.logging.read.FileLogReader)4 LogEvent (co.cask.cdap.logging.read.LogEvent)4 CConfiguration (co.cask.cdap.common.conf.CConfiguration)3 ApplicationLoggingContext (co.cask.cdap.common.logging.ApplicationLoggingContext)3 LogPathIdentifier (co.cask.cdap.logging.appender.system.LogPathIdentifier)3 FileMetaDataReader (co.cask.cdap.logging.meta.FileMetaDataReader)3 LogLocation (co.cask.cdap.logging.write.LogLocation)3 ProgramType (co.cask.cdap.proto.ProgramType)3 Transactional (co.cask.cdap.api.Transactional)2 DatasetManager (co.cask.cdap.api.dataset.DatasetManager)2 NamespaceLoggingContext (co.cask.cdap.common.logging.NamespaceLoggingContext)2 ServiceLoggingContext (co.cask.cdap.common.logging.ServiceLoggingContext)2 SystemDatasetInstantiator (co.cask.cdap.data.dataset.SystemDatasetInstantiator)2