Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing java.util.logging.Logger methods to PatchLogger #4540

Merged
merged 2 commits into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,16 @@ 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;
}
Comment on lines -58 to -61
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😱

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;
}
}
}