Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* MIT License
*
* Copyright (c) 2018 Kelvin Wahome
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package io.github.kwahome.sopa;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import org.slf4j.LoggerFactory;
import org.slf4j.event.Level;
import io.github.kwahome.sopa.interfaces.LogRenderer;
import io.github.kwahome.sopa.interfaces.LoggableObject;
import io.github.kwahome.sopa.interfaces.Logger;
import io.github.kwahome.sopa.utils.Helpers;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
/**
* Concrete implementation of the Logger interface
*
* @author Kelvin Wahome
*/
@RequiredArgsConstructor
public class StructLogger implements Logger {
private final org.slf4j.Logger slf4jLogger;
private Optional instanceBoundContext = Optional.empty();
StructLogger(String name) {
slf4jLogger = LoggerFactory.getLogger(name);
}
StructLogger(Class> source) {
slf4jLogger = LoggerFactory.getLogger(source);
}
/**
* {@link Logger} error method implementation.
*
* @param message {@link String} message
* @param params {@link Object []} params
*/
@Override
public void error(String message, Object... params) {
if (slf4jLogger.isErrorEnabled()) {
log(Level.ERROR, message, params);
}
}
/**
* {@link Logger} warn method implementation.
*
* @param message {@link String} message
* @param params {@link Object []} params
*/
@Override
public void warn(String message, Object... params) {
if (slf4jLogger.isWarnEnabled()) {
log(Level.WARN, message, params);
}
}
/**
* {@link Logger} info method implementation.
*
* @param message {@link String} message
* @param params {@link Object []} params
*/
@Override
public void info(String message, Object... params) {
if (slf4jLogger.isInfoEnabled()) {
log(Level.INFO, message, params);
}
}
/**
* {@link Logger} debug method implementation.
*
* @param message {@link String} message
* @param params {@link Object []} params
*/
@Override
public void debug(String message, Object... params) {
if (slf4jLogger.isDebugEnabled()) {
log(Level.DEBUG, message, params);
}
}
/**
* {@link Logger} trace method implementation.
*
* @param message {@link String} message
* @param params {@link Object []} params
*/
@Override
public void trace(String message, Object... params) {
if (slf4jLogger.isTraceEnabled()) {
log(Level.TRACE, message, params);
}
}
/**
* {@link Logger} isErrorEnabled method implementation.
*
* @return boolean
*/
@Override
public boolean isErrorEnabled() {
return slf4jLogger.isErrorEnabled();
}
/**
* {@link Logger} isWarnEnabled method implementation.
*
* @return boolean
*/
@Override
public boolean isWarnEnabled() {
return slf4jLogger.isWarnEnabled();
}
/**
* {@link Logger} isInfoEnabled method implementation.
*
* @return boolean
*/
@Override
public boolean isInfoEnabled() {
return slf4jLogger.isInfoEnabled();
}
/**
* {@link Logger} isDebugEnabled method implementation.
*
* @return boolean
*/
@Override
public boolean isDebugEnabled() {
return slf4jLogger.isDebugEnabled();
}
/**
* {@link Logger} isTraceEnabled method implementation.
*
* @return boolean
*/
@Override
public boolean isTraceEnabled() {
return slf4jLogger.isTraceEnabled();
}
/**
* {@link org.slf4j.Logger} getter.
*
* @return {@link org.slf4j.Logger}
*/
public org.slf4j.Logger getSlf4jLogger() {
return slf4jLogger;
}
/**
* Returns a {@link LoggableObject} from the optional {@link #instanceBoundContext}.
* If the {@link Optional} is empty, an empty {@link GenericLoggableObject} is returned.
*
* @return {@link LoggableObject}
*/
private LoggableObject getLoggableInstanceBoundContext() {
LoggableObject loggableObject = new GenericLoggableObject();
if (instanceBoundContext.isPresent()) {
loggableObject = instanceBoundContext.get();
}
return loggableObject;
}
/**
* {@link #instanceBoundContext} setter method.
*
* @param instanceBoundContext {@link LoggableObject}
*/
private void setInstanceBoundContext(LoggableObject instanceBoundContext) {
this.instanceBoundContext = Optional.of(instanceBoundContext);
}
/**
* Returns a {@link LoggableObject} from the {@link StructLoggerConfig} contextSupplier.
*
* If the optional is empty, an empty {@link GenericLoggableObject} is returned.
*
* @return {@link LoggableObject}
*/
private LoggableObject getLoggableGlobalContextSupplier() {
LoggableObject loggableObject = new GenericLoggableObject();
if (StructLoggerConfig.getContextSupplier().isPresent()) {
loggableObject = StructLoggerConfig.getContextSupplier().get();
}
return loggableObject;
}
/**
* Binds passed context to {@link Logger} instance. Existing context will be overwritten.
*
* Takes a list of key-value pairs at alternate positions e.g:
*
* [key1, value1, key2, value2]
*
* @param params {@link Object[]} of key-value pairs
*/
@Override
public void newBind(Object...params) {
instanceBoundContext = Optional.empty();
setInstanceBoundContext(new GenericLoggableObject(addParamsToBoundContext(params)));
}
/**
* Binds passed context to {@link Logger} instance while preserving existing context
* by adding new params onto the already bound context.
*
* Takes a list of key-value pairs at alternate positions e.g:
*
* [key1, value1, key2, value2]
*
* @param params {@link Object[]} of key-value pairs
*/
@Override
public void bind(Object...params) {
if (!instanceBoundContext.isPresent()) {
newBind(params);
} else {
setInstanceBoundContext(new GenericLoggableObject(addParamsToBoundContext(params)));
}
}
/**
* Removes passed context from {@link Logger} instance bound context.
*
* Takes a list of key-value pairs at alternate positions e.g:
*
* [key1, value1, key2, value2]
*
* @param params {@link Object[]} of key-value pairs
*/
@Override
public void unbind(Object...params) {
instanceBoundContext = Optional.of(new GenericLoggableObject(removeItemFromBoundContext(params)));
}
/**
* Adds passed log context params to context bound to the logger instance.
*
* The {@link Object}[] instanceBoundContext and the array of params are converted into a
* {@link Map}<{@link String}, {@link Object}> to guarantee that keys are not duplicated.
*
* @param params {@link Object[]}
* @return {@link Object}[]
*/
private Object[] addParamsToBoundContext(Object...params) {
Map globalLoggerContext = Helpers.objectArrayToMap(
getLoggableGlobalContextSupplier().loggableObject());
Map stringObjectMap = Helpers.objectArrayToMap(
getLoggableInstanceBoundContext().loggableObject());
boolean proceed = true;
for (int i = 0; i < params.length; i++) {
Object param = params[i];
if (param instanceof LoggableObject) {
LoggableObject loggableObject = (LoggableObject) param;
stringObjectMap.putAll(Helpers.objectArrayToMap(loggableObject.loggableObject()));
} else if (param instanceof Map &&
(i % 2 == 0 || (i % 2 != 0 && !validateKey(params[i - 1], null, false)))) {
stringObjectMap.putAll((Map) param);
} else if (proceed) {
// dynamic key-value pairs being passed in
// process the key-value pairs only if no errors were encountered and order can is reliably correct
// move on to the next field automatically and assume it's the value
i++;
if (i < params.length) {
if (proceed = validateKey(param, null, true)) {
String key = (String) param;
// check if key in global context in which case the
// global context values takes precedence & we don't want duplication
if (!globalLoggerContext.containsKey(key)) {
stringObjectMap.put(key, params[i]);
} else {
slf4jLogger.warn(
String.format("%s key `%s` ignored because it exists in the global context with " +
"value `%s` which takes precedence.", StructLoggerConfig.getSopaLoggerTag(), key,
globalLoggerContext.get(key)));
}
}
} else {
slf4jLogger.warn(String.format("%s odd number of parameters (%s) passed in. " +
"The value pair for key `%s` not found thus it has been ignored.",
StructLoggerConfig.getSopaLoggerTag(), params.length, param));
}
}
}
return Helpers.mapToObjectArray(stringObjectMap);
}
/**
* Removes passed log context params to context bound to the logger instance
*
* @param params {@link Object[]}
* @return {@link Object[]}
*/
private Object[] removeItemFromBoundContext(Object...params) {
Map stringObjectMap = Helpers.objectArrayToMap(
getLoggableInstanceBoundContext().loggableObject());
boolean proceed = true;
for (int i = 0; i < params.length; i++) {
Object param = params[i];
if (param instanceof LoggableObject) {
LoggableObject loggableObject = (LoggableObject) param;
stringObjectMap.entrySet().removeAll(
Helpers.objectArrayToMap(loggableObject.loggableObject()).entrySet());
} else if (param instanceof Map &&
(i % 2 == 0 || (i % 2 != 0 && !validateKey(params[i - 1], null, false)))) {
stringObjectMap.entrySet().removeAll(((Map) param).entrySet());
} else if (proceed) {
// process the key-value pairs only if no errors were encountered and order can is reliably correct
// next field is construed to be the value
i++;
if (i < params.length) {
// check if key in global context in which case the
// global context values takes precedence
if (proceed = validateKey(param, null, true)) {
String key = (String) param;
stringObjectMap.remove(key, params[i]);
}
} else {
slf4jLogger.warn(String.format("%s odd number of parameters (%s) passed in. " +
"The value pair for key `%s` not found thus it has been ignored.",
StructLoggerConfig.getSopaLoggerTag(), params.length, param));
}
}
}
return Helpers.mapToObjectArray(stringObjectMap);
}
/**
* Handle {@link LoggableObject} implementations
*
* @param logRenderer "{@link LogRenderer } implementation"
* @param builderObject "{@link Object} builder"
* @param loggableObject "{@link LoggableObject}"
*/
private void handleLoggableObject(
LogRenderer