Commit 27f36de9 authored by Kelly Domico's avatar Kelly Domico Committed by Kishore Battula
Browse files

Add a common logger interface

parent 3f229c22
......@@ -20,7 +20,7 @@
<groupId>org.opengroup.osdu</groupId>
<artifactId>core-lib-azure</artifactId>
<packaging>jar</packaging>
<version>0.0.43</version>
<version>0.0.44</version>
<name>core-lib-azure</name>
<properties>
......
package org.opengroup.osdu.azure.logging;
import org.opengroup.osdu.core.common.logging.audit.AuditPayload;
import org.slf4j.Logger;
/**
* Logger wrapper around SLF4J APIs.
*/
public class CoreLogger implements ICoreLogger {
private final Logger logger;
/**
* @param traceLogger the logger instance
*/
public CoreLogger(final Logger traceLogger) {
this.logger = traceLogger;
}
/**
* Log a message at the INFO level.
*
* @param msg the message string to be logged
*/
@Override
public void info(final String msg) {
this.logger.info(msg);
}
/**
* Log a message at the INFO level according to the specified format and arguments.
*
* @param format the format string
* @param arguments a list of arguments
*/
@Override
public void info(final String format, final Object... arguments) {
this.logger.info(format, arguments);
}
/**
* Log a message at the WARN level.
*
* @param msg the message string to be logged
*/
@Override
public void warn(final String msg) {
this.logger.warn(msg);
}
/**
* Log a message at the WARN level according to the specified format and arguments.
*
* @param format the format string
* @param arguments a list of arguments
*/
@Override
public void warn(final String format, final Object... arguments) {
this.logger.warn(format, arguments);
}
/**
* Log an exception (throwable) at the WARN level with an accompanying message.
*
* @param msg the message accompanying the exception
* @param t the exception (throwable) to log
*/
@Override
public void warn(final String msg, final Throwable t) {
this.logger.warn(msg, t);
}
/**
* Log a message at the ERROR level.
*
* @param msg the message string to be logged
*/
@Override
public void error(final String msg) {
this.logger.error(msg);
}
/**
* Log a message at the ERROR level according to the specified format and arguments.
*
* @param format the format string
* @param arguments a list of arguments
*/
@Override
public void error(final String format, final Object... arguments) {
this.logger.error(format, arguments);
}
/**
* Log an exception (throwable) at the ERROR level with an accompanying message.
*
* @param msg the message accompanying the exception
* @param t the exception (throwable) to log
*/
@Override
public void error(final String msg, final Throwable t) {
this.logger.error(msg, t);
}
/**
* Log an audit.
*
* @param payload the audit payload
*/
@Override
public void logAudit(final AuditPayload payload) {
this.logger.info("{}", payload);
}
/**
* Log a dependency.
*
* @param payload the dependency payload
*/
@Override
public void logDependency(final DependencyPayload payload) {
this.logger.info("{}", payload);
}
}
package org.opengroup.osdu.azure.logging;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
/**
* Implementation for Core Logger Factory instances.
*/
public final class CoreLoggerFactory implements ICoreLoggerFactory {
private static CoreLoggerFactory instance = new CoreLoggerFactory();
private Map<String, ICoreLogger> loggers = new HashMap<>();
/**
* CoreLoggerFactory singleton.
*/
private CoreLoggerFactory() {
}
/**
* Return this instance.
*
* @return this instance
*/
public static CoreLoggerFactory getInstance() {
return instance;
}
/**
* @param name the name of the logger
* @return Return a logger named according to the name parameter.
*/
public ICoreLogger getLogger(final String name) {
if (!loggers.containsKey(name)) {
CoreLogger logger = new CoreLogger(LoggerFactory.getLogger(name));
loggers.put(name, logger);
}
return loggers.get(name);
}
/**
* @param clazz the name of the logger
* @return Return a logger named corresponding to the class passed as parameter.
*/
public ICoreLogger getLogger(final Class<?> clazz) {
return this.getLogger(clazz.getName());
}
}
package org.opengroup.osdu.azure.logging;
import org.springframework.stereotype.Component;
/**
* Implementation for Core Logger Factory Provider instances.
*/
@Component
public final class CoreLoggerFactoryProvider implements ICoreLoggerFactoryProvider {
/**
* @return Return a logger factory.
*/
@Override
public ICoreLoggerFactory getLoggerFactory() {
return CoreLoggerFactory.getInstance();
}
}
package org.opengroup.osdu.azure.logging;
import lombok.Getter;
import lombok.Setter;
import java.time.Duration;
/**
* Dependency payload.
*/
@Getter
@Setter
public final class DependencyPayload {
private String name;
private String data;
private Duration duration;
private String resultCode;
private boolean success;
/**
* Instantiate empty payload.
*/
public DependencyPayload() {
}
/**
* Instantiate payload with specified values.
*
* @param dependencyData the name of the command initiated with this dependency call
* @param dependencyName the command initiated by this dependency call
* @param dependencyDuration the request duration
* @param dependencyResultCode the result code of the call
* @param dependencySuccess indication of successful or unsuccessful call
*/
public DependencyPayload(final String dependencyName, final String dependencyData, final Duration dependencyDuration, final String dependencyResultCode, final boolean dependencySuccess) {
this.name = dependencyName;
this.data = dependencyData;
this.duration = dependencyDuration;
this.resultCode = dependencyResultCode;
this.success = dependencySuccess;
}
@Override
public String toString() {
return String.format("{\"name\": \"%s\", \"data\": \"%s\", \"duration\": %d, \"resultCode\": \"%s\", \"success\": %s}", name, data, duration.getNano(), resultCode, success);
}
}
package org.opengroup.osdu.azure.logging;
import org.opengroup.osdu.core.common.logging.audit.AuditPayload;
/**
* Interface for common logger to support traces, exceptions, dependencies among services, and audit events.
*/
public interface ICoreLogger {
/**
* Log a message at the INFO level.
*
* @param msg the message string to be logged
*/
void info(String msg);
/**
* Log a message at the INFO level according to the specified format and arguments.
*
* @param format the format string
* @param arguments a list of arguments
*/
void info(String format, Object... arguments);
/**
* Log a message at the WARN level.
*
* @param msg the message string to be logged
*/
void warn(String msg);
/**
* Log a message at the WARN level according to the specified format and arguments.
*
* @param format the format string
* @param arguments a list of arguments
*/
void warn(String format, Object... arguments);
/**
* Log an exception (throwable) at the WARN level with an accompanying message.
*
* @param msg the message accompanying the exception
* @param t the exception (throwable) to log
*/
void warn(String msg, Throwable t);
/**
* Log a message at the ERROR level.
*
* @param msg the message string to be logged
*/
void error(String msg);
/**
* Log a message at the ERROR level according to the specified format and arguments.
*
* @param format the format string
* @param arguments a list of arguments
*/
void error(String format, Object... arguments);
/**
* Log an exception (throwable) at the ERROR level with an accompanying message.
*
* @param msg the message accompanying the exception
* @param t the exception (throwable) to log
*/
void error(String msg, Throwable t);
/**
* Log an audit.
*
* @param payload the audit payload
*/
void logAudit(AuditPayload payload);
/**
* Log a dependency.
*
* @param payload the dependency payload
*/
void logDependency(DependencyPayload payload);
}
package org.opengroup.osdu.azure.logging;
/**
* Interface for Core Logger Factory to return appropriate logger instances.
*/
public interface ICoreLoggerFactory {
/**
* @param name the name of the logger
* @return Return a logger named according to the name parameter.
*/
ICoreLogger getLogger(String name);
/**
* @param clazz the name of the logger
* @return Return a logger named corresponding to the class passed as parameter.
*/
ICoreLogger getLogger(Class<?> clazz);
}
package org.opengroup.osdu.azure.logging;
/**
* Interface for Core Logger Factory Provider.
*/
public interface ICoreLoggerFactoryProvider {
/**
* @return Return a logger factory.
*/
ICoreLoggerFactory getLoggerFactory();
}
package org.opengroup.osdu.azure.logging;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opengroup.osdu.core.common.logging.audit.AuditAction;
import org.opengroup.osdu.core.common.logging.audit.AuditPayload;
import org.opengroup.osdu.core.common.logging.audit.AuditStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import static org.mockito.Mockito.*;
/**
* Contains tests for {@link CoreLogger}
*/
@ExtendWith(MockitoExtension.class)
public class CoreLoggerTest {
private static final String LOGGER_NAME = CoreLoggerTest.class.getName();
private static final String LOG_MESSAGE = "Hello logger test";
private static final String EXCEPTION_MESSAGE = "Logger exception message";
private static final String ARG1 = "Argument 1 value";
private static final String ARG2 = "Argument 2 value";
private static final String ARG3 = "Argument 3 value";
private static Logger spySlf4jLogger;
private static CoreLogger coreLogger;
@BeforeAll
public static void setup() {
spySlf4jLogger = spy(LoggerFactory.getLogger(LOGGER_NAME));
coreLogger = mock(CoreLogger.class, withSettings().useConstructor(spySlf4jLogger).defaultAnswer(CALLS_REAL_METHODS));
}
@Test
public void testInfoWithMessage() {
coreLogger.info(LOG_MESSAGE);
verify(spySlf4jLogger).info(LOG_MESSAGE);
}
@Test
public void testInfoWithArguments() {
coreLogger.info("{} {} {}", ARG1, ARG2, ARG3);
verify(spySlf4jLogger).info("{} {} {}", ARG1, ARG2, ARG3);
}
@Test
public void testWarnWithMessage() {
coreLogger.warn(LOG_MESSAGE);
verify(spySlf4jLogger).warn(LOG_MESSAGE);
}
@Test
public void testWarnWithArguments() {
coreLogger.warn("{} {} {}", ARG1, ARG2, ARG3);
verify(spySlf4jLogger).warn("{} {} {}", ARG1, ARG2, ARG3);
}
@Test
public void testWarnWithThrowable() {
Exception e = new Exception(EXCEPTION_MESSAGE);
coreLogger.warn(LOG_MESSAGE, e);
verify(spySlf4jLogger).warn(LOG_MESSAGE, e);
}
@Test
public void testErrorWithMessage() {
coreLogger.error(LOG_MESSAGE);
verify(spySlf4jLogger).error(LOG_MESSAGE);
}
@Test
public void testErrorWithArguments() {
coreLogger.error("{} {} {}", ARG1, ARG2, ARG3);
verify(spySlf4jLogger).error("{} {} {}", ARG1, ARG2, ARG3);
}
@Test
public void testErrorWithThrowable() {
Exception e = new Exception(EXCEPTION_MESSAGE);
coreLogger.error(LOG_MESSAGE, e);
verify(spySlf4jLogger).error(LOG_MESSAGE, e);
}
@Test
public void testLogAudit() {
List<String> resource = new ArrayList<>();
resource.add("TestResource");
AuditPayload payload = AuditPayload.builder()
.action(AuditAction.READ)
.status(AuditStatus.FAILURE)
.actionId("TestAuditId")
.message("TestAuditMessage")
.resources(resource)
.user("TestUser")
.build();
coreLogger.logAudit(payload);
verify(spySlf4jLogger).info("{}", payload);
}
@Test
public void testLogDependency() {
DependencyPayload payload = new DependencyPayload("DependencyName", "DependencyPayload", Duration.ofMillis(1000), "200", true);
coreLogger.logDependency(payload);
verify(spySlf4jLogger).info("{}", payload);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment