Skip to content

Commit

Permalink
Add missing java.util.logging.Logger methods to PatchLogger (#4540)
Browse files Browse the repository at this point in the history
* Add missing java.util.logging.Logger methods to PatchLogger

* spotless
  • Loading branch information
laurit authored and anuraaga committed Nov 4, 2021
1 parent f84c40f commit 35ed1c5
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.text.MessageFormat;
import java.util.ResourceBundle;
import java.util.function.Supplier;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
Expand Down Expand Up @@ -61,30 +62,68 @@ public void severe(String msg) {
slf4jLogger.error(msg);
}

public void severe(Supplier<String> msgSupplier) {
if (slf4jLogger.isErrorEnabled()) {
slf4jLogger.error(msgSupplier.get());
}
}

public void warning(String msg) {
slf4jLogger.warn(msg);
}

public void warning(Supplier<String> msgSupplier) {
if (slf4jLogger.isWarnEnabled()) {
slf4jLogger.warn(msgSupplier.get());
}
}

public void info(String msg) {
slf4jLogger.info(msg);
}

public void info(Supplier<String> msgSupplier) {
if (slf4jLogger.isInfoEnabled()) {
slf4jLogger.info(msgSupplier.get());
}
}

public void config(String msg) {
slf4jLogger.info(msg);
}

public void config(Supplier<String> msgSupplier) {
info(msgSupplier);
}

public void fine(String msg) {
slf4jLogger.debug(msg);
}

public void fine(Supplier<String> msgSupplier) {
if (slf4jLogger.isDebugEnabled()) {
slf4jLogger.debug(msgSupplier.get());
}
}

public void finer(String msg) {
slf4jLogger.trace(msg);
}

public void finer(Supplier<String> msgSupplier) {
if (slf4jLogger.isTraceEnabled()) {
slf4jLogger.trace(msgSupplier.get());
}
}

public void finest(String msg) {
slf4jLogger.trace(msg);
}

public void finest(Supplier<String> msgSupplier) {
finer(msgSupplier);
}

public void log(LogRecord record) {
Level level = record.getLevel();
if (level.intValue() >= Level.SEVERE.intValue()) {
Expand Down Expand Up @@ -186,6 +225,40 @@ public void log(Level level, String msg, Throwable thrown) {
}
}

public void log(Level level, Supplier<String> msgSupplier) {
if (!isLoggable(level)) {
return;
}
if (level.intValue() >= Level.SEVERE.intValue()) {
slf4jLogger.error(msgSupplier.get());
} else if (level.intValue() >= Level.WARNING.intValue()) {
slf4jLogger.warn(msgSupplier.get());
} else if (level.intValue() >= Level.CONFIG.intValue()) {
slf4jLogger.info(msgSupplier.get());
} else if (level.intValue() >= Level.FINE.intValue()) {
slf4jLogger.debug(msgSupplier.get());
} else {
slf4jLogger.trace(msgSupplier.get());
}
}

public void log(Level level, Throwable thrown, Supplier<String> msgSupplier) {
if (!isLoggable(level)) {
return;
}
if (level.intValue() >= Level.SEVERE.intValue()) {
slf4jLogger.error(msgSupplier.get(), thrown);
} else if (level.intValue() >= Level.WARNING.intValue()) {
slf4jLogger.warn(msgSupplier.get(), thrown);
} else if (level.intValue() >= Level.CONFIG.intValue()) {
slf4jLogger.info(msgSupplier.get(), thrown);
} else if (level.intValue() >= Level.FINE.intValue()) {
slf4jLogger.debug(msgSupplier.get(), thrown);
} else {
slf4jLogger.trace(msgSupplier.get(), thrown);
}
}

public boolean isLoggable(Level level) {
if (level.intValue() >= Level.SEVERE.intValue()) {
return slf4jLogger.isErrorEnabled();
Expand Down Expand Up @@ -235,6 +308,20 @@ public void logp(
log(level, msg, thrown);
}

public void logp(
Level level, String sourceClass, String sourceMethod, Supplier<String> msgSupplier) {
log(level, msgSupplier);
}

public void logp(
Level level,
String sourceClass,
String sourceMethod,
Throwable thrown,
Supplier<String> msgSupplier) {
log(level, thrown, msgSupplier);
}

public void logrb(
Level level, String sourceClass, String sourceMethod, String bundleName, String msg) {
log(level, msg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ void testImplementsAllMethods() {
for (Class<?> clazz : method.getParameterTypes()) {
parameterTypes.add(clazz.getName());
}
if (parameterTypes.contains("java.util.function.Supplier")) {
// FIXME it would be good to include Java 8 methods
continue;
}
builder.parameterTypes.addAll(parameterTypes);
builder.returnType = method.getReturnType().getName();
julLoggerMethods.add(builder);
Expand Down Expand Up @@ -866,5 +862,11 @@ public boolean equals(Object obj) {
public int hashCode() {
return Objects.hash(name, parameterTypes, returnType);
}

@Override
public String toString() {
String params = parameterTypes.stream().reduce((a, b) -> a + ", " + b).orElse("");
return name + "(" + params + ")" + returnType;
}
}
}

0 comments on commit 35ed1c5

Please sign in to comment.