Search in sources :

Example 1 with SubstituteLogger

use of org.slf4j.helpers.SubstituteLogger in project core-java by SpineEventEngine.

the class CommandHandlerShould method log_errors.

@Test
public void log_errors() {
    final CommandEnvelope commandEnvelope = givenCommandEnvelope();
    // Since we're in the tests mode `Environment` returns `SubstituteLogger` instance.
    final SubstituteLogger log = (SubstituteLogger) handler.log();
    // Restrict the queue size only to the number of calls we want to make.
    final Queue<SubstituteLoggingEvent> queue = Queues.newArrayBlockingQueue(1);
    log.setDelegate(new EventRecodingLogger(log, queue));
    SubstituteLoggingEvent loggingEvent;
    final RuntimeException exception = new RuntimeException("log_errors");
    handler.onError(commandEnvelope, exception);
    loggingEvent = queue.poll();
    assertEquals(Level.ERROR, loggingEvent.getLevel());
    assertEquals(commandEnvelope, handler.getLastErrorEnvelope());
    assertEquals(exception, handler.getLastException());
}
Also used : SubstituteLogger(org.slf4j.helpers.SubstituteLogger) SubstituteLoggingEvent(org.slf4j.event.SubstituteLoggingEvent) EventRecodingLogger(org.slf4j.event.EventRecodingLogger) CommandEnvelope(io.spine.core.CommandEnvelope) Test(org.junit.Test)

Example 2 with SubstituteLogger

use of org.slf4j.helpers.SubstituteLogger in project core-java by SpineEventEngine.

the class LoggingObserverShould method assertAtLevel.

private void assertAtLevel(Level level) {
    final LoggingObserver<Object> observer = getObserver(level);
    assertNotNull(observer);
    // Since we're in the tests mode `Environment` returns `SubstituteLogger` instance.
    final SubstituteLogger log = (SubstituteLogger) observer.log();
    // Restrict the queue size only to the number of calls we want to make.
    final Queue<SubstituteLoggingEvent> queue = Queues.newArrayBlockingQueue(3);
    log.setDelegate(new EventRecodingLogger(log, queue));
    SubstituteLoggingEvent loggingEvent;
    final String value = newUuid();
    observer.onNext(value);
    loggingEvent = queue.poll();
    assertNotNull(loggingEvent);
    assertLogLevel(loggingEvent, level);
    assertContains(loggingEvent, value);
    assertContains(loggingEvent, "onNext");
    final Timestamp currentTime = Time.getCurrentTime();
    final String timeStr = Timestamps.toString(currentTime);
    observer.onNext(currentTime);
    loggingEvent = queue.poll();
    assertNotNull(loggingEvent);
    assertLogLevel(loggingEvent, level);
    assertContains(loggingEvent, timeStr);
    observer.onCompleted();
    loggingEvent = queue.poll();
    assertNotNull(loggingEvent);
    assertLogLevel(loggingEvent, level);
    assertContains(loggingEvent, "onCompleted");
}
Also used : SubstituteLogger(org.slf4j.helpers.SubstituteLogger) SubstituteLoggingEvent(org.slf4j.event.SubstituteLoggingEvent) EventRecodingLogger(org.slf4j.event.EventRecodingLogger) Timestamp(com.google.protobuf.Timestamp)

Aggregations

EventRecodingLogger (org.slf4j.event.EventRecodingLogger)2 SubstituteLoggingEvent (org.slf4j.event.SubstituteLoggingEvent)2 SubstituteLogger (org.slf4j.helpers.SubstituteLogger)2 Timestamp (com.google.protobuf.Timestamp)1 CommandEnvelope (io.spine.core.CommandEnvelope)1 Test (org.junit.Test)1