io.datakernel.util.LogUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datakernel-eventloop Show documentation
Show all versions of datakernel-eventloop Show documentation
Efficient non-blocking network and file I/O, for building Node.js-like client/server applications with
high performance requirements. It is similar to Event Loop in Node.js.
Although Eventloop runs in a single thread, multiple event loops can be
launched at the same time allowing for efficient CPU usage.
package io.datakernel.util;
import io.datakernel.annotation.Nullable;
import org.slf4j.Logger;
import java.util.Arrays;
import java.util.Collection;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;
import static io.datakernel.util.LogUtils.Level.INFO;
import static io.datakernel.util.LogUtils.Level.TRACE;
import static java.util.stream.Collectors.joining;
public class LogUtils {
private static final int LIST_LIMIT = 100;
public enum Level {
OFF(null) {
@Override
protected boolean isEnabled(Logger logger) {
return false;
}
},
TRACE(Logger::trace) {
@Override
protected boolean isEnabled(Logger logger) {
return logger.isTraceEnabled();
}
},
DEBUG(Logger::debug) {
@Override
protected boolean isEnabled(Logger logger) {
return logger.isDebugEnabled();
}
},
INFO(Logger::info) {
@Override
protected boolean isEnabled(Logger logger) {
return logger.isInfoEnabled();
}
},
WARN(Logger::warn) {
@Override
protected boolean isEnabled(Logger logger) {
return logger.isWarnEnabled();
}
},
ERROR(Logger::error) {
@Override
protected boolean isEnabled(Logger logger) {
return logger.isErrorEnabled();
}
};
private final BiConsumer logConsumer;
Level(BiConsumer logConsumer) {
this.logConsumer = logConsumer;
}
protected abstract boolean isEnabled(Logger logger);
public final void log(Logger logger, Supplier messageSupplier) {
if (isEnabled(logger)) {
logConsumer.accept(logger, messageSupplier.get());
}
}
public final void log(Logger logger, String message) {
if (isEnabled(logger)) {
logConsumer.accept(logger, message);
}
}
}
public static String thisMethod() {
try {
StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[2];
return stackTraceElement.getMethodName();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static BiConsumer toLogger(Logger logger,
Level callLevel, Supplier callMsg,
Level resultLevel, Function resultMsg,
@Nullable Level errorLevel, Function errorMsg) {
if (!logger.isErrorEnabled()) return ($, e) -> {};
callLevel.log(logger, callMsg);
return (result, throwable) -> {
if (throwable == null) {
resultLevel.log(logger, () -> resultMsg.apply(result));
} else {
if (errorLevel == null) {
if (logger.isErrorEnabled()) {
logger.error(errorMsg.apply(throwable), throwable);
}
} else {
errorLevel.log(logger, () -> errorMsg.apply(throwable));
}
}
};
}
public static BiConsumer toLogger(Logger logger,
Level callLevel, Supplier callMsg,
Level resultLevel, Function resultMsg) {
return toLogger(logger,
callLevel, callMsg,
resultLevel, resultMsg,
null, throwable -> callMsg.get());
}
public static BiConsumer toLogger(Logger logger,
Level callLevel, Level resultLevel, Level errorLevel,
String methodName, Object... parameters) {
return toLogger(logger,
callLevel, () -> formatCall(methodName, parameters),
resultLevel, result -> formatResult(methodName, result, parameters),
errorLevel, errorLevel == null ?
throwable -> formatCall(methodName, parameters) :
throwable -> formatResult(methodName, throwable, parameters));
}
public static BiConsumer toLogger(Logger logger,
Level callLevel, Level resultLevel,
String methodName, Object... parameters) {
return toLogger(logger, callLevel, resultLevel, null, methodName, parameters);
}
public static BiConsumer toLogger(Logger logger,
Level level,
String methodName, Object... parameters) {
return toLogger(logger, level, level, methodName, parameters);
}
public static BiConsumer toLogger(Logger logger, String methodName, Object... parameters) {
return toLogger(logger, TRACE, INFO, methodName, parameters);
}
private static String toString(Object object) {
if (object == null) {
return "null";
}
if (object instanceof Collection) {
return CollectionUtils.toLimitedString((Collection>) object, LIST_LIMIT);
}
return object.toString();
}
public static String formatCall(String methodName, Object... parameters) {
return methodName +
(parameters.length != 0 ? " " + Arrays.stream(parameters)
.map(LogUtils::toString)
.collect(joining(", ")) : "") +
" ...";
}
public static String formatResult(String methodName, Object result, Object... parameters) {
return methodName +
(parameters.length != 0 ? " " + Arrays.stream(parameters)
.map(LogUtils::toString)
.collect(joining(", ")) : "") +
" -> " + toString(result);
}
}