Search in sources :

Example 41 with ILoggingEvent

use of ch.qos.logback.classic.spi.ILoggingEvent in project cdap by caskdata.

the class KafkaOffsetResolverTest method findExpectedOffsetByTime.

/**
 * Finds the smallest next offset with corresponding timestamp equal to targetTime,
 * or the largest next offset with corresponding timestamp smaller than (targetTime - EVENT_DELAY_MILLIS) if
 * no event has timestamp equal to targetTime, or the smallest offset 0 if no event has timestamp smaller
 * than (targetTime - EVENT_DELAY_MILLIS)
 */
private long findExpectedOffsetByTime(List<ILoggingEvent> events, long targetTime) {
    long offset = 0;
    // Use smallest offset 0 as default value in case no event has timestamp smaller
    long latestOffsetBeforeMinTime = 0;
    long minTime = targetTime - EVENT_DELAY_MILLIS;
    for (ILoggingEvent event : events) {
        long time = event.getTimeStamp();
        if (time == targetTime) {
            // Increment the offset to get the next offset
            return offset + 1;
        }
        if (time < minTime) {
            // Increment the offset to get the next offset
            latestOffsetBeforeMinTime = offset + 1;
        }
        offset++;
    }
    // No event contains timestamp equal to targetTime, return latestOffsetBeforeMinTime
    return latestOffsetBeforeMinTime;
}
Also used : ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent)

Example 42 with ILoggingEvent

use of ch.qos.logback.classic.spi.ILoggingEvent in project cdap by caskdata.

the class KafkaOffsetResolverTest method testInOrderEvents.

@Test
public void testInOrderEvents() throws InterruptedException, IOException {
    String topic = "testInOrderEvents";
    KafkaPipelineConfig config = new KafkaPipelineConfig(topic, Collections.singleton(0), 1024L, EVENT_DELAY_MILLIS, 1048576, 200L);
    KAFKA_TESTER.createTopic(topic, 1);
    // Publish log messages to Kafka and wait for all messages to be published
    long baseTime = System.currentTimeMillis() - EVENT_DELAY_MILLIS;
    List<ILoggingEvent> inOrderEvents = new ArrayList<>();
    for (int i = 0; i < 20; i++) {
        inOrderEvents.add(createLoggingEvent("test.logger", Level.INFO, Integer.toString(i), baseTime + i));
    }
    publishLog(topic, inOrderEvents);
    waitForAllLogsPublished(topic, inOrderEvents.size());
    KafkaOffsetResolver offsetResolver = new KafkaOffsetResolver(KAFKA_TESTER.getBrokerService(), config);
    // Use every event's timestamp as target time and assert that found offset is the next offset of the current offset
    for (int i = 0; i < inOrderEvents.size(); i++) {
        long targetTime = inOrderEvents.get(i).getTimeStamp();
        long offset = offsetResolver.getStartOffset(new Checkpoint(Long.MAX_VALUE, targetTime, 0), 0);
        Assert.assertEquals("Failed to find the expected event with the target time: " + targetTime, i + 1, offset);
    }
}
Also used : Checkpoint(co.cask.cdap.logging.meta.Checkpoint) ArrayList(java.util.ArrayList) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) Checkpoint(co.cask.cdap.logging.meta.Checkpoint) Test(org.junit.Test)

Example 43 with ILoggingEvent

use of ch.qos.logback.classic.spi.ILoggingEvent in project cdap by caskdata.

the class LoggingEventTest method testEmptySerialize.

@Test
public void testEmptySerialize() throws Exception {
    Logger logger = LoggerFactory.getLogger(LoggingEventTest.class);
    ch.qos.logback.classic.spi.LoggingEvent iLoggingEvent = new ch.qos.logback.classic.spi.LoggingEvent(getClass().getName(), (ch.qos.logback.classic.Logger) logger, Level.ERROR, null, null, null);
    LoggingEventSerializer serializer = new LoggingEventSerializer();
    byte[] encoded = serializer.toBytes(new LogMessage(iLoggingEvent, LoggingContextAccessor.getLoggingContext()));
    ILoggingEvent decodedEvent = serializer.fromBytes(ByteBuffer.wrap(encoded));
    LoggingEventSerializerTest.assertLoggingEventEquals(iLoggingEvent, decodedEvent);
}
Also used : ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LogMessage(co.cask.cdap.logging.appender.LogMessage) Logger(org.slf4j.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) Test(org.junit.Test)

Example 44 with ILoggingEvent

use of ch.qos.logback.classic.spi.ILoggingEvent in project cdap by caskdata.

the class LoggingEventSerializerTest method testEmptySerialization.

@Test
public void testEmptySerialization() throws Exception {
    Logger logger = LoggerFactory.getLogger(LoggingEventSerializerTest.class);
    LoggingEventSerializer serializer = new LoggingEventSerializer();
    ch.qos.logback.classic.spi.LoggingEvent iLoggingEvent = new ch.qos.logback.classic.spi.LoggingEvent(getClass().getName(), (ch.qos.logback.classic.Logger) logger, Level.ERROR, "message", null, null);
    iLoggingEvent.setThreadName("thread-1");
    iLoggingEvent.setTimeStamp(10000000L);
    // Serialize
    ILoggingEvent event = new LogMessage(iLoggingEvent, LoggingContextAccessor.getLoggingContext());
    byte[] serializedBytes = serializer.toBytes(event);
    // De-serialize
    ILoggingEvent actualEvent = serializer.fromBytes(ByteBuffer.wrap(serializedBytes));
    assertLoggingEventEquals(iLoggingEvent, actualEvent);
}
Also used : ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LogMessage(co.cask.cdap.logging.appender.LogMessage) Logger(org.slf4j.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) Test(org.junit.Test)

Example 45 with ILoggingEvent

use of ch.qos.logback.classic.spi.ILoggingEvent in project cdap by caskdata.

the class LogLocation method readToEndSyncPosition.

/**
 *  Read current block in Avro file from current block sync marker to next block sync marker
 */
private List<LogEvent> readToEndSyncPosition(DataFileReader<GenericRecord> dataFileReader, Filter logFilter, long fromTimeMs, long endSyncPosition) throws IOException {
    List<LogEvent> logSegment = new ArrayList<>();
    long currentSyncPosition = dataFileReader.previousSync();
    // or read until endSyncPosition has been reached
    while (dataFileReader.hasNext() && (endSyncPosition == -1 || (currentSyncPosition < endSyncPosition))) {
        ILoggingEvent loggingEvent = new LoggingEvent(dataFileReader.next());
        loggingEvent.prepareForDeferredProcessing();
        // Stop when reached fromTimeMs
        if (loggingEvent.getTimeStamp() > fromTimeMs) {
            break;
        }
        if (logFilter.match(loggingEvent)) {
            logSegment.add(new LogEvent(loggingEvent, new LogOffset(LogOffset.INVALID_KAFKA_OFFSET, loggingEvent.getTimeStamp())));
        }
        currentSyncPosition = dataFileReader.previousSync();
    }
    return logSegment;
}
Also used : ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggingEvent(co.cask.cdap.logging.serialize.LoggingEvent) LogEvent(co.cask.cdap.logging.read.LogEvent) LogOffset(co.cask.cdap.logging.read.LogOffset) ArrayList(java.util.ArrayList) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent)

Aggregations

ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)300 Test (org.junit.Test)111 Logger (ch.qos.logback.classic.Logger)90 LoggerContext (ch.qos.logback.classic.LoggerContext)85 Appender (ch.qos.logback.core.Appender)48 Test (org.junit.jupiter.api.Test)47 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)44 FileAppender (ch.qos.logback.core.FileAppender)40 ConsoleAppender (ch.qos.logback.core.ConsoleAppender)35 RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)30 File (java.io.File)26 AsyncLoggingEventAppenderFactory (io.dropwizard.logging.async.AsyncLoggingEventAppenderFactory)22 DropwizardLayoutFactory (io.dropwizard.logging.layout.DropwizardLayoutFactory)21 Logger (org.slf4j.Logger)20 TimeBasedRollingPolicy (ch.qos.logback.core.rolling.TimeBasedRollingPolicy)17 AsyncAppender (ch.qos.logback.classic.AsyncAppender)16 ArrayList (java.util.ArrayList)16 ListAppender (ch.qos.logback.core.read.ListAppender)15 OnMarkerEvaluator (ch.qos.logback.classic.boolex.OnMarkerEvaluator)13 EvaluatorFilter (ch.qos.logback.core.filter.EvaluatorFilter)13