Add slf4j delegate

This commit is contained in:
Andrea Cavalli 2021-02-20 21:04:58 +01:00
parent d41a0d135b
commit ea607cdd02
5 changed files with 537 additions and 0 deletions

View File

@ -50,6 +50,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>it.unimi.dsi</groupId>
<artifactId>fastutil</artifactId>

View File

@ -0,0 +1,9 @@
package org.warp.commonutils.log;
public enum Level {
TRACE,
DEBUG,
INFO,
WARN,
ERROR
}

View File

@ -0,0 +1,417 @@
package org.warp.commonutils.log;
import org.slf4j.Marker;
public class Logger implements org.slf4j.Logger {
private final org.slf4j.Logger loggerDelegate;
public Logger(org.slf4j.Logger logger) {
this.loggerDelegate = logger;
}
@Override
public String getName() {
return loggerDelegate.getName();
}
@Override
public boolean isTraceEnabled() {
return Logs.isTraceEnabled() && loggerDelegate.isTraceEnabled();
}
@Override
public void trace(String s) {
if (Logs.isTraceEnabled()) {
loggerDelegate.trace(s);
}
}
@Override
public void trace(String s, Object o) {
if (Logs.isTraceEnabled()) {
loggerDelegate.trace(s, o);
}
}
@Override
public void trace(String s, Object o, Object o1) {
if (Logs.isTraceEnabled()) {
loggerDelegate.trace(s, o, o1);
}
}
@Override
public void trace(String s, Object... objects) {
if (Logs.isTraceEnabled()) {
loggerDelegate.trace(s, objects);
}
}
@Override
public void trace(String s, Throwable throwable) {
if (Logs.isTraceEnabled()) {
loggerDelegate.trace(s, throwable);
}
}
@Override
public boolean isTraceEnabled(Marker marker) {
return Logs.isTraceEnabled() && loggerDelegate.isTraceEnabled(marker);
}
@Override
public void trace(Marker marker, String s) {
if (Logs.isTraceEnabled()) {
loggerDelegate.trace(marker, s);
}
}
@Override
public void trace(Marker marker, String s, Object o) {
if (Logs.isTraceEnabled()) {
loggerDelegate.trace(marker, s, o);
}
}
@Override
public void trace(Marker marker, String s, Object o, Object o1) {
if (Logs.isTraceEnabled()) {
loggerDelegate.trace(marker, s, o, o1);
}
}
@Override
public void trace(Marker marker, String s, Object... objects) {
if (Logs.isTraceEnabled()) {
loggerDelegate.trace(marker, s, objects);
}
}
@Override
public void trace(Marker marker, String s, Throwable throwable) {
if (Logs.isTraceEnabled()) {
loggerDelegate.trace(marker, s, throwable);
}
}
@Override
public boolean isDebugEnabled() {
return Logs.isDebugEnabled() && loggerDelegate.isDebugEnabled();
}
@Override
public void debug(String s) {
if (Logs.isDebugEnabled()) {
loggerDelegate.debug(s);
}
}
@Override
public void debug(String s, Object o) {
if (Logs.isDebugEnabled()) {
loggerDelegate.debug(s, o);
}
}
@Override
public void debug(String s, Object o, Object o1) {
if (Logs.isDebugEnabled()) {
loggerDelegate.debug(s, o, o1);
}
}
@Override
public void debug(String s, Object... objects) {
if (Logs.isDebugEnabled()) {
loggerDelegate.debug(s, objects);
}
}
@Override
public void debug(String s, Throwable throwable) {
if (Logs.isDebugEnabled()) {
loggerDelegate.debug(s, throwable);
}
}
@Override
public boolean isDebugEnabled(Marker marker) {
return Logs.isDebugEnabled() && loggerDelegate.isDebugEnabled(marker);
}
@Override
public void debug(Marker marker, String s) {
if (Logs.isDebugEnabled()) {
loggerDelegate.debug(marker, s);
}
}
@Override
public void debug(Marker marker, String s, Object o) {
if (Logs.isDebugEnabled()) {
loggerDelegate.debug(marker, s, o);
}
}
@Override
public void debug(Marker marker, String s, Object o, Object o1) {
if (Logs.isDebugEnabled()) {
loggerDelegate.debug(marker, s, o, o1);
}
}
@Override
public void debug(Marker marker, String s, Object... objects) {
if (Logs.isDebugEnabled()) {
loggerDelegate.debug(marker, s, objects);
}
}
@Override
public void debug(Marker marker, String s, Throwable throwable) {
if (Logs.isDebugEnabled()) {
loggerDelegate.debug(marker, s, throwable);
}
}
@Override
public boolean isInfoEnabled() {
return Logs.isInfoEnabled() && loggerDelegate.isInfoEnabled();
}
@Override
public void info(String s) {
if (Logs.isInfoEnabled()) {
loggerDelegate.info(s);
}
}
@Override
public void info(String s, Object o) {
if (Logs.isInfoEnabled()) {
loggerDelegate.info(s, o);
}
}
@Override
public void info(String s, Object o, Object o1) {
if (Logs.isInfoEnabled()) {
loggerDelegate.info(s, o, o1);
}
}
@Override
public void info(String s, Object... objects) {
if (Logs.isInfoEnabled()) {
loggerDelegate.info(s, objects);
}
}
@Override
public void info(String s, Throwable throwable) {
if (Logs.isInfoEnabled()) {
loggerDelegate.info(s, throwable);
}
}
@Override
public boolean isInfoEnabled(Marker marker) {
return Logs.isInfoEnabled() && loggerDelegate.isInfoEnabled(marker);
}
@Override
public void info(Marker marker, String s) {
if (Logs.isInfoEnabled()) {
loggerDelegate.info(marker, s);
}
}
@Override
public void info(Marker marker, String s, Object o) {
if (Logs.isInfoEnabled()) {
loggerDelegate.info(marker, s, o);
}
}
@Override
public void info(Marker marker, String s, Object o, Object o1) {
if (Logs.isInfoEnabled()) {
loggerDelegate.info(marker, s, o, o1);
}
}
@Override
public void info(Marker marker, String s, Object... objects) {
if (Logs.isInfoEnabled()) {
loggerDelegate.info(marker, s, objects);
}
}
@Override
public void info(Marker marker, String s, Throwable throwable) {
if (Logs.isInfoEnabled()) {
loggerDelegate.info(marker, s, throwable);
}
}
@Override
public boolean isWarnEnabled() {
return Logs.isWarnEnabled() && loggerDelegate.isWarnEnabled();
}
@Override
public void warn(String s) {
if (Logs.isWarnEnabled()) {
loggerDelegate.warn(s);
}
}
@Override
public void warn(String s, Object o) {
if (Logs.isWarnEnabled()) {
loggerDelegate.warn(s, o);
}
}
@Override
public void warn(String s, Object... objects) {
if (Logs.isWarnEnabled()) {
loggerDelegate.warn(s, objects);
}
}
@Override
public void warn(String s, Object o, Object o1) {
if (Logs.isWarnEnabled()) {
loggerDelegate.warn(s, o, o1);
}
}
@Override
public void warn(String s, Throwable throwable) {
if (Logs.isWarnEnabled()) {
loggerDelegate.warn(s, throwable);
}
}
@Override
public boolean isWarnEnabled(Marker marker) {
return Logs.isWarnEnabled() && loggerDelegate.isWarnEnabled(marker);
}
@Override
public void warn(Marker marker, String s) {
if (Logs.isWarnEnabled()) {
loggerDelegate.warn(marker, s);
}
}
@Override
public void warn(Marker marker, String s, Object o) {
if (Logs.isWarnEnabled()) {
loggerDelegate.warn(marker, s, o);
}
}
@Override
public void warn(Marker marker, String s, Object o, Object o1) {
if (Logs.isWarnEnabled()) {
loggerDelegate.warn(marker, s, o, o1);
}
}
@Override
public void warn(Marker marker, String s, Object... objects) {
if (Logs.isWarnEnabled()) {
loggerDelegate.warn(marker, s, objects);
}
}
@Override
public void warn(Marker marker, String s, Throwable throwable) {
if (Logs.isWarnEnabled()) {
loggerDelegate.warn(marker, s, throwable);
}
}
@Override
public boolean isErrorEnabled() {
return Logs.isErrorEnabled() && loggerDelegate.isErrorEnabled();
}
@Override
public void error(String s) {
if (Logs.isErrorEnabled()) {
loggerDelegate.error(s);
}
}
@Override
public void error(String s, Object o) {
if (Logs.isErrorEnabled()) {
loggerDelegate.error(s, o);
}
}
@Override
public void error(String s, Object o, Object o1) {
if (Logs.isErrorEnabled()) {
loggerDelegate.error(s, o, o1);
}
}
@Override
public void error(String s, Object... objects) {
if (Logs.isErrorEnabled()) {
loggerDelegate.error(s, objects);
}
}
@Override
public void error(String s, Throwable throwable) {
if (Logs.isErrorEnabled()) {
loggerDelegate.error(s, throwable);
}
}
@Override
public boolean isErrorEnabled(Marker marker) {
return Logs.isErrorEnabled() && loggerDelegate.isErrorEnabled(marker);
}
@Override
public void error(Marker marker, String s) {
if (Logs.isErrorEnabled()) {
loggerDelegate.error(marker, s);
}
}
@Override
public void error(Marker marker, String s, Object o) {
if (Logs.isErrorEnabled()) {
loggerDelegate.error(marker, s, o);
}
}
@Override
public void error(Marker marker, String s, Object o, Object o1) {
if (Logs.isErrorEnabled()) {
loggerDelegate.error(marker, s, o, o1);
}
}
@Override
public void error(Marker marker, String s, Object... objects) {
if (Logs.isErrorEnabled()) {
loggerDelegate.error(marker, s, objects);
}
}
@Override
public void error(Marker marker, String s, Throwable throwable) {
if (Logs.isErrorEnabled()) {
loggerDelegate.error(marker, s, throwable);
}
}
}

View File

@ -0,0 +1,36 @@
package org.warp.commonutils.log;
/**
* Convenient equivalents of the static functions on SLF4J's {@link org.slf4j.LoggerFactory}.
*
* These return an SLF4J Ext {@link Logger} decorating the SLF4J {@link org.slf4j.Logger} that would be returned by the equivalent
* function on SLF4J's {@link org.slf4j.LoggerFactory}.
*/
public final class LoggerFactory {
/**
* Return a {@link Logger} decorating the SLF4J {@link org.slf4j.Logger} returned by a call to
* SLF4J's {@link org.slf4j.LoggerFactory#getLogger(Class)}.
*
* @param aClass the returned logger will be named after clazz
* @return logger
*/
public static Logger getLogger(final Class<?> aClass) {
return new Logger(org.slf4j.LoggerFactory.getLogger(aClass));
}
/**
* Return a {@link Logger} decorating the SLF4J {@link org.slf4j.Logger} returned by a call to
* SLF4J's {@link org.slf4j.LoggerFactory#getLogger(String)}.
*
* @param loggerName The name of the logger.
* @return logger
*/
public static Logger getLogger(final String loggerName) {
return new Logger(org.slf4j.LoggerFactory.getLogger(loggerName));
}
private LoggerFactory() {
throw new UnsupportedOperationException("Not instantiable");
}
}

View File

@ -0,0 +1,70 @@
package org.warp.commonutils.log;
public class Logs {
private static boolean traceEnabled;
private static boolean debugEnabled;
private static boolean infoEnabled;
private static boolean warnEnabled;
private static Level rootLevel;
public static void setRootLevel(Level level) {
rootLevel = level;
switch (level) {
case ERROR:
traceEnabled = false;
debugEnabled = false;
infoEnabled = false;
warnEnabled = false;
break;
case WARN:
traceEnabled = false;
debugEnabled = false;
infoEnabled = false;
warnEnabled = true;
break;
case INFO:
traceEnabled = false;
debugEnabled = false;
infoEnabled = true;
warnEnabled = true;
break;
case DEBUG:
traceEnabled = false;
debugEnabled = true;
infoEnabled = true;
warnEnabled = true;
break;
case TRACE:
traceEnabled = true;
debugEnabled = true;
infoEnabled = true;
warnEnabled = true;
break;
}
}
public static Level getRootLevel() {
return rootLevel;
}
public static boolean isTraceEnabled() {
return traceEnabled;
}
public static boolean isDebugEnabled() {
return debugEnabled;
}
public static boolean isInfoEnabled() {
return infoEnabled;
}
public static boolean isWarnEnabled() {
return warnEnabled;
}
public static boolean isErrorEnabled() {
return true;
}
}