use of io.cdap.cdap.logging.read.LogEvent in project cdap by caskdata.
the class AbstractLogHttpHandler method doGetLogs.
protected void doGetLogs(LogReader logReader, HttpResponder responder, LoggingContext loggingContext, long fromTimeSecsParam, long toTimeSecsParam, boolean escape, String filterStr, @Nullable RunRecordDetail runRecord, String format, List<String> fieldsToSuppress) {
try {
TimeRange timeRange = parseTime(fromTimeSecsParam, toTimeSecsParam, responder);
if (timeRange == null) {
return;
}
Filter filter = FilterParser.parse(filterStr);
ReadRange readRange = new ReadRange(timeRange.getFromMillis(), timeRange.getToMillis(), LogOffset.INVALID_KAFKA_OFFSET);
readRange = adjustReadRange(readRange, runRecord, fromTimeSecsParam != -1);
try {
// the iterator is closed by the BodyProducer passed to the HttpResponder
CloseableIterator<LogEvent> logIter = logReader.getLog(loggingContext, readRange.getFromMillis(), readRange.getToMillis(), filter);
AbstractChunkedLogProducer logsProducer = getFullLogsProducer(format, logIter, fieldsToSuppress, escape);
responder.sendContent(HttpResponseStatus.OK, logsProducer, logsProducer.getResponseHeaders());
} catch (Exception ex) {
LOG.debug("Exception while reading logs for logging context {}", loggingContext, ex);
responder.sendStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
}
} catch (SecurityException e) {
responder.sendStatus(HttpResponseStatus.UNAUTHORIZED);
} catch (IllegalArgumentException e) {
responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
}
}
use of io.cdap.cdap.logging.read.LogEvent in project cdap by cdapio.
the class TextChunkedLogProducer method writeLogEvents.
@Override
protected ByteBuf writeLogEvents(CloseableIterator<LogEvent> logEventIter) throws IOException {
ByteBuf buffer = Unpooled.buffer(BUFFER_BYTES);
while (logEventIter.hasNext() && buffer.readableBytes() < BUFFER_BYTES) {
LogEvent logEvent = logEventIter.next();
String logLine = patternLayout.doLayout(logEvent.getLoggingEvent());
logLine = escape ? StringEscapeUtils.escapeHtml(logLine) : logLine;
buffer.writeCharSequence(logLine, StandardCharsets.UTF_8);
}
return buffer;
}
use of io.cdap.cdap.logging.read.LogEvent in project cdap by cdapio.
the class CDAPLogAppenderTest method assertLogEventDetails.
private void assertLogEventDetails(LoggingEvent expectedLoggingEvent, LogLocation logLocation) throws IOException {
Assert.assertEquals(LogLocation.VERSION_1, logLocation.getFrameworkVersion());
Assert.assertTrue(logLocation.getLocation().exists());
CloseableIterator<LogEvent> logEventCloseableIterator = logLocation.readLog(Filter.EMPTY_FILTER, 0, Long.MAX_VALUE, Integer.MAX_VALUE);
int logCount = 0;
while (logEventCloseableIterator.hasNext()) {
logCount++;
LogEvent logEvent = logEventCloseableIterator.next();
Assert.assertEquals(expectedLoggingEvent.getMessage(), logEvent.getLoggingEvent().getMessage());
}
logEventCloseableIterator.close();
Assert.assertEquals(1, logCount);
}
use of io.cdap.cdap.logging.read.LogEvent in project cdap by cdapio.
the class TestDistributedLogReader method generateCheckpointTime.
private static void generateCheckpointTime(LoggingContext loggingContext, int numExpectedEvents, String kafkaTopic) throws IOException {
FileLogReader logReader = injector.getInstance(FileLogReader.class);
List<LogEvent> events = Lists.newArrayList(logReader.getLog(loggingContext, 0, Long.MAX_VALUE, Filter.EMPTY_FILTER));
Assert.assertEquals(numExpectedEvents, events.size());
// Save checkpoint (time of last event)
TransactionRunner transactionRunner = injector.getInstance(TransactionRunner.class);
CheckpointManager<KafkaOffset> checkpointManager = new KafkaCheckpointManager(transactionRunner, Constants.Logging.SYSTEM_PIPELINE_CHECKPOINT_PREFIX + kafkaTopic);
long checkpointTime = events.get(numExpectedEvents - 1).getLoggingEvent().getTimeStamp();
checkpointManager.saveCheckpoints(ImmutableMap.of(stringPartitioner.partition(loggingContext.getLogPartition(), -1), new Checkpoint<>(new KafkaOffset(numExpectedEvents, checkpointTime), checkpointTime)));
}
use of io.cdap.cdap.logging.read.LogEvent in project cdap by cdapio.
the class TestDistributedLogReader method testDistributedLogPrev.
private void testDistributedLogPrev(ReadRange readRange, LoggingContext loggingContext, int numCalls, int step, String assertMessage, int assertCount) throws Exception {
DistributedLogReader distributedLogReader = injector.getInstance(DistributedLogReader.class);
for (int i = 0; i < numCalls; ++i) {
LoggingTester.LogCallback callback = new LoggingTester.LogCallback();
distributedLogReader.getLogPrev(loggingContext, readRange, step, Filter.EMPTY_FILTER, callback);
List<LogEvent> events = callback.getEvents();
Assert.assertFalse(events.isEmpty());
readRange = ReadRange.createToRange(events.get(0).getOffset());
Collections.reverse(events);
for (LogEvent event : events) {
Assert.assertEquals(assertMessage + --assertCount, event.getLoggingEvent().getFormattedMessage());
// All logs generated by this test have value "system" in MDC property ".origin"
Assert.assertEquals("system", event.getLoggingEvent().getMDCPropertyMap().get(".origin"));
}
}
Assert.assertEquals(0, assertCount);
}
Aggregations