Search in sources :

Example 11 with Filter

use of io.cdap.cdap.logging.filter.Filter in project cdap by caskdata.

the class FileLogReader method getLog.

@Override
public CloseableIterator<LogEvent> getLog(LoggingContext loggingContext, final long fromTimeMs, final long toTimeMs, Filter filter) {
    try {
        final Filter logFilter = new AndFilter(ImmutableList.of(LoggingContextHelper.createFilter(loggingContext), filter));
        LOG.trace("Using fromTimeMs={}, toTimeMs={}", fromTimeMs, toTimeMs);
        List<LogLocation> sortedFilesInRange = fileMetadataReader.listFiles(LoggingContextHelper.getLogPathIdentifier(loggingContext), fromTimeMs, toTimeMs);
        if (sortedFilesInRange.isEmpty()) {
            // return empty iterator
            return new AbstractCloseableIterator<LogEvent>() {

                @Override
                protected LogEvent computeNext() {
                    return endOfData();
                }

                @Override
                public void close() {
                // no-op
                }
            };
        }
        final Iterator<LogLocation> filesIter = sortedFilesInRange.iterator();
        CloseableIterator<CloseableIterator<LogEvent>> closeableIterator = new CloseableIterator<CloseableIterator<LogEvent>>() {

            private CloseableIterator<LogEvent> curr = null;

            @Override
            public void close() {
                if (curr != null) {
                    curr.close();
                }
            }

            @Override
            public boolean hasNext() {
                return filesIter.hasNext();
            }

            @Override
            public CloseableIterator<LogEvent> next() {
                if (curr != null) {
                    curr.close();
                }
                LogLocation file = filesIter.next();
                LOG.trace("Reading file {}", file);
                curr = file.readLog(logFilter, fromTimeMs, toTimeMs, Integer.MAX_VALUE);
                return curr;
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException("Remove not supported");
            }
        };
        return concat(closeableIterator);
    } catch (Throwable e) {
        LOG.error("Got exception: ", e);
        throw Throwables.propagate(e);
    }
}
Also used : AndFilter(io.cdap.cdap.logging.filter.AndFilter) CloseableIterator(io.cdap.cdap.api.dataset.lib.CloseableIterator) AbstractCloseableIterator(io.cdap.cdap.api.dataset.lib.AbstractCloseableIterator) AbstractCloseableIterator(io.cdap.cdap.api.dataset.lib.AbstractCloseableIterator) Filter(io.cdap.cdap.logging.filter.Filter) AndFilter(io.cdap.cdap.logging.filter.AndFilter) LogLocation(io.cdap.cdap.logging.write.LogLocation)

Example 12 with Filter

use of io.cdap.cdap.logging.filter.Filter in project cdap by caskdata.

the class FileLogReader method getLogNext.

@Override
public void getLogNext(final LoggingContext loggingContext, final ReadRange readRange, final int maxEvents, final Filter filter, final Callback callback) {
    if (readRange == ReadRange.LATEST) {
        getLogPrev(loggingContext, readRange, maxEvents, filter, callback);
        return;
    }
    callback.init();
    try {
        Filter logFilter = new AndFilter(ImmutableList.of(LoggingContextHelper.createFilter(loggingContext), filter));
        long fromTimeMs = readRange.getFromMillis() + 1;
        LOG.trace("Using fromTimeMs={}, readRange={}", fromTimeMs, readRange);
        List<LogLocation> sortedFilesInRange = fileMetadataReader.listFiles(LoggingContextHelper.getLogPathIdentifier(loggingContext), readRange.getFromMillis(), readRange.getToMillis());
        if (sortedFilesInRange.isEmpty()) {
            return;
        }
        for (LogLocation file : sortedFilesInRange) {
            LOG.trace("Reading file {}", file);
            file.readLog(logFilter, fromTimeMs, Long.MAX_VALUE, maxEvents - callback.getCount(), callback);
            if (callback.getCount() >= maxEvents) {
                break;
            }
        }
    } catch (Throwable e) {
        LOG.error("Got exception: ", e);
        throw Throwables.propagate(e);
    }
}
Also used : AndFilter(io.cdap.cdap.logging.filter.AndFilter) Filter(io.cdap.cdap.logging.filter.Filter) AndFilter(io.cdap.cdap.logging.filter.AndFilter) LogLocation(io.cdap.cdap.logging.write.LogLocation)

Aggregations

Filter (io.cdap.cdap.logging.filter.Filter)12 AndFilter (io.cdap.cdap.logging.filter.AndFilter)6 ReadRange (io.cdap.cdap.logging.read.ReadRange)4 LogEvent (io.cdap.cdap.logging.read.LogEvent)3 LogLocation (io.cdap.cdap.logging.write.LogLocation)3 IOException (java.io.IOException)3 LoggingContext (io.cdap.cdap.common.logging.LoggingContext)2 KafkaConsumer (io.cdap.cdap.logging.kafka.KafkaConsumer)2 Callback (io.cdap.cdap.logging.read.Callback)2 LogOffset (io.cdap.cdap.logging.read.LogOffset)2 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)1 ImmutableList (com.google.common.collect.ImmutableList)1 AbstractCloseableIterator (io.cdap.cdap.api.dataset.lib.AbstractCloseableIterator)1 CloseableIterator (io.cdap.cdap.api.dataset.lib.CloseableIterator)1 ComponentLoggingContext (io.cdap.cdap.common.logging.ComponentLoggingContext)1 NamespaceLoggingContext (io.cdap.cdap.common.logging.NamespaceLoggingContext)1 ServiceLoggingContext (io.cdap.cdap.common.logging.ServiceLoggingContext)1 LogAppenderInitializer (io.cdap.cdap.logging.appender.LogAppenderInitializer)1 LoggingTester (io.cdap.cdap.logging.appender.LoggingTester)1 LogPartitionType (io.cdap.cdap.logging.appender.kafka.LogPartitionType)1