Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add prefix monitor #3621

Merged
merged 14 commits into from
Nov 22, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*
* Contributors:
* Microsoft Corporation - initial API and implementation
* Mercedes-Benz Tech Innovation GmbH - prefix monitoring
*
*/

Expand Down Expand Up @@ -64,4 +65,17 @@ default String sanitizeMessage(Supplier<String> supplier) {
.orElse(null);
}

/**
* Creates a prefixed {@link Monitor} which will prepend the supplied prefix parameter to the actual log message
* in the format of: <strong>[LOGLEVEL] [TIMESTAMP] [[PREFIX]] [MESSAGE]</strong>
* <br>
* Example output: <strong>INFO 2023-11-08T15:53:39.989025 [JerseyExtension]: Registered Web API context alias: protocol</strong>
*
* @param prefix string value to be prefixed
* @return the prefixed monitor
*/
default Monitor withPrefix(String prefix) {
hamidonos marked this conversation as resolved.
Show resolved Hide resolved
return new PrefixMonitor(this, prefix);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2023 Mercedes-Benz Tech Innovation GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Mercedes-Benz Tech Innovation GmbH - initial implementation
*
*/

package org.eclipse.edc.spi.monitor;

import java.util.function.Supplier;

/**
* Monitor implementation which will prefix a supplied text to all log messages.
* Note that this monitor will delegate the actual log execution to the underlying monitor provided by the context.
*/
class PrefixMonitor implements Monitor {

private static final String MESSAGE_FORMAT = "[%s] %s";

private final Monitor monitor;
private final String prefix;

PrefixMonitor(Monitor monitor, String prefix) {
this.monitor = monitor;
this.prefix = prefix;
}

@Override
public void severe(Supplier<String> supplier, Throwable... errors) {
monitor.severe(() -> MESSAGE_FORMAT.formatted(prefix, supplier.get()), errors);
}

@Override
public void severe(String message, Throwable... errors) {
monitor.severe(MESSAGE_FORMAT.formatted(prefix, message), errors);
}

@Override
public void warning(Supplier<String> supplier, Throwable... errors) {
monitor.warning(() -> MESSAGE_FORMAT.formatted(prefix, supplier.get()), errors);
}

@Override
public void warning(String message, Throwable... errors) {
monitor.warning(MESSAGE_FORMAT.formatted(prefix, message), errors);
}

@Override
public void info(Supplier<String> supplier, Throwable... errors) {
monitor.info(() -> MESSAGE_FORMAT.formatted(prefix, supplier.get()), errors);
}

@Override
public void info(String message, Throwable... errors) {
monitor.info(MESSAGE_FORMAT.formatted(prefix, message), errors);
}

@Override
public void debug(Supplier<String> supplier, Throwable... errors) {
monitor.debug(() -> MESSAGE_FORMAT.formatted(prefix, supplier.get()), errors);
}

@Override
public void debug(String message, Throwable... errors) {
monitor.debug(MESSAGE_FORMAT.formatted(prefix, message), errors);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2023 Mercedes-Benz Tech Innovation GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Mercedes-Benz Tech Innovation GmbH - initial implementation
*
*/

package org.eclipse.edc.spi.monitor;

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.util.function.Supplier;

import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;

class PrefixMonitorTest {

private static final String TEST_PREFIX = "Test Prefix";
private static final String TEST_MESSAGE = "Test Message";
private static final String EXPECTED_MESSAGE = "[Test Prefix] Test Message";
private final Monitor mockMonitor = Mockito.mock();
private final Monitor prefixMonitor = new PrefixMonitor(mockMonitor, TEST_PREFIX);

@Test
void logSevere_byMessageSupplier_logIsDelegated() {
prefixMonitor.severe(() -> TEST_MESSAGE);
verify(mockMonitor).severe(argThat((Supplier<String> supplier) -> EXPECTED_MESSAGE.equals(supplier.get())));
}

@Test
void logSevere_byMessageString_logIsDelegated() {
prefixMonitor.severe(TEST_MESSAGE);
verify(mockMonitor).severe(eq(EXPECTED_MESSAGE));
}

@Test
void logWarning_byMessageSupplier_logIsDelegated() {
prefixMonitor.warning(() -> TEST_MESSAGE);
verify(mockMonitor).warning(argThat((Supplier<String> supplier) -> EXPECTED_MESSAGE.equals(supplier.get())));
}

@Test
void logWarning_byMessageString_logIsDelegated() {
prefixMonitor.warning(TEST_MESSAGE);
verify(mockMonitor).warning(eq(EXPECTED_MESSAGE));
}

@Test
void logInfo_byMessageSupplier_logIsDelegated() {
prefixMonitor.info(() -> TEST_MESSAGE);
verify(mockMonitor).info(argThat((Supplier<String> supplier) -> EXPECTED_MESSAGE.equals(supplier.get())));
}

@Test
void logInfo_byMessageString_logIsDelegated() {
prefixMonitor.info(TEST_MESSAGE);
verify(mockMonitor).info(eq(EXPECTED_MESSAGE));
}

@Test
void logDebug_byMessageSupplier_logIsDelegated() {
prefixMonitor.debug(() -> TEST_MESSAGE);
verify(mockMonitor).debug(argThat((Supplier<String> supplier) -> EXPECTED_MESSAGE.equals(supplier.get())));
}

@Test
void logDebug_byMessageString_logIsDelegated() {
prefixMonitor.debug(TEST_MESSAGE);
verify(mockMonitor).debug(eq(EXPECTED_MESSAGE));
}
}
Loading