Search in sources :

Example 1 with LoggingEvent

use of org.slf4j.event.LoggingEvent in project bookkeeper by apache.

the class LoggerOutput method apply.

@Override
public Statement apply(final Statement base, Description description) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            LoggerContext lc = (LoggerContext) LogManager.getContext(false);
            logAppender = spy(NullAppender.createAppender(UUID.randomUUID().toString()));
            logAppender.start();
            lc.getConfiguration().addAppender(logAppender);
            lc.getRootLogger().addAppender(lc.getConfiguration().getAppender(logAppender.getName()));
            lc.updateLoggers();
            try {
                base.evaluate();
                if (!logEventExpectations.isEmpty()) {
                    verify(logAppender, atLeastOnce()).append(logEventCaptor.capture());
                    List<LoggingEvent> logEvents = logEventCaptor.getAllValues().stream().map(LoggerOutput::toSlf4j).collect(Collectors.toList());
                    for (Consumer<List<LoggingEvent>> expectation : logEventExpectations) {
                        expectation.accept(logEvents);
                    }
                }
            } finally {
                lc.getRootLogger().removeAppender(lc.getConfiguration().getAppender(logAppender.getName()));
                lc.updateLoggers();
                logEventExpectations.clear();
                logEventCaptor = null;
            }
        }
    };
}
Also used : LoggingEvent(org.slf4j.event.LoggingEvent) Statement(org.junit.runners.model.Statement) ArrayList(java.util.ArrayList) List(java.util.List) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 2 with LoggingEvent

use of org.slf4j.event.LoggingEvent in project opentelemetry-java by open-telemetry.

the class OtlpHttpLogExporterTest method testServerError.

@Test
@SuppressLogger(OkHttpExporter.class)
void testServerError() {
    server.enqueue(buildResponse(HttpStatus.INTERNAL_SERVER_ERROR, Status.newBuilder().setMessage("Server error!").build()));
    OtlpHttpLogExporter exporter = builder.build();
    exportAndAssertResult(exporter, /* expectedResult= */
    false);
    LoggingEvent log = logs.assertContains("Failed to export logs. Server responded with HTTP status code 500. Error message: Server error!");
    assertThat(log.getLevel()).isEqualTo(Level.WARN);
}
Also used : LoggingEvent(org.slf4j.event.LoggingEvent) SuppressLogger(io.opentelemetry.internal.testing.slf4j.SuppressLogger) Test(org.junit.jupiter.api.Test)

Example 3 with LoggingEvent

use of org.slf4j.event.LoggingEvent in project opentelemetry-java by open-telemetry.

the class OtlpHttpMetricExporterTest method testServerError.

@Test
void testServerError() {
    server.enqueue(buildResponse(HttpStatus.INTERNAL_SERVER_ERROR, Status.newBuilder().setMessage("Server error!").build()));
    OtlpHttpMetricExporter exporter = builder.build();
    exportAndAssertResult(exporter, /* expectedResult= */
    false);
    LoggingEvent log = logs.assertContains("Failed to export metrics. Server responded with HTTP status code 500. Error message: Server error!");
    assertThat(log.getLevel()).isEqualTo(Level.WARN);
}
Also used : LoggingEvent(org.slf4j.event.LoggingEvent) Test(org.junit.jupiter.api.Test)

Example 4 with LoggingEvent

use of org.slf4j.event.LoggingEvent in project opentelemetry-java by open-telemetry.

the class OtlpHttpMetricExporterTest method testServerErrorParseError.

@Test
void testServerErrorParseError() {
    server.enqueue(HttpResponse.of(HttpStatus.INTERNAL_SERVER_ERROR, APPLICATION_PROTOBUF, "Server error!"));
    OtlpHttpMetricExporter exporter = builder.build();
    exportAndAssertResult(exporter, /* expectedResult= */
    false);
    LoggingEvent log = logs.assertContains("Failed to export metrics. Server responded with HTTP status code 500. Error message: Unable to parse response body, HTTP status message:");
    assertThat(log.getLevel()).isEqualTo(Level.WARN);
}
Also used : LoggingEvent(org.slf4j.event.LoggingEvent) Test(org.junit.jupiter.api.Test)

Example 5 with LoggingEvent

use of org.slf4j.event.LoggingEvent in project opentelemetry-java by open-telemetry.

the class ContextTest method closingScopeWhenNotActiveIsLogged.

@Test
public void closingScopeWhenNotActiveIsLogged() {
    Context initial = Context.current();
    Context context = initial.with(ANIMAL, "cat");
    try (Scope scope = context.makeCurrent()) {
        Context context2 = context.with(ANIMAL, "dog");
        try (Scope ignored = context2.makeCurrent()) {
            assertThat(Context.current().get(ANIMAL)).isEqualTo("dog");
            scope.close();
        }
    }
    assertThat(Context.current()).isEqualTo(initial);
    LoggingEvent log = logs.assertContains("Context in storage not the expected context");
    assertThat(log.getLevel()).isEqualTo(Level.DEBUG);
}
Also used : LoggingEvent(org.slf4j.event.LoggingEvent) Test(org.junit.jupiter.api.Test)

Aggregations

LoggingEvent (org.slf4j.event.LoggingEvent)21 Test (org.junit.jupiter.api.Test)20 SuppressLogger (io.opentelemetry.internal.testing.slf4j.SuppressLogger)11 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 ArrayList (java.util.ArrayList)1 List (java.util.List)1 LoggerContext (org.apache.logging.log4j.core.LoggerContext)1 Statement (org.junit.runners.model.Statement)1 SetSystemProperty (org.junitpioneer.jupiter.SetSystemProperty)1