From 35ed1c556654c66bd229ecf4994d336d9888ee22 Mon Sep 17 00:00:00 2001 From: Lauri Tulmin Date: Fri, 29 Oct 2021 03:59:21 +0300 Subject: [PATCH] Add missing java.util.logging.Logger methods to PatchLogger (#4540) * Add missing java.util.logging.Logger methods to PatchLogger * spotless --- .../javaagent/bootstrap/PatchLogger.java | 87 +++++++++++++++++++ .../javaagent/bootstrap/PatchLoggerTest.java | 10 ++- 2 files changed, 93 insertions(+), 4 deletions(-) diff --git a/javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/PatchLogger.java b/javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/PatchLogger.java index ece78da3e31c..05262afa40e6 100644 --- a/javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/PatchLogger.java +++ b/javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/PatchLogger.java @@ -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; @@ -61,30 +62,68 @@ public void severe(String msg) { slf4jLogger.error(msg); } + public void severe(Supplier msgSupplier) { + if (slf4jLogger.isErrorEnabled()) { + slf4jLogger.error(msgSupplier.get()); + } + } + public void warning(String msg) { slf4jLogger.warn(msg); } + public void warning(Supplier msgSupplier) { + if (slf4jLogger.isWarnEnabled()) { + slf4jLogger.warn(msgSupplier.get()); + } + } + public void info(String msg) { slf4jLogger.info(msg); } + public void info(Supplier msgSupplier) { + if (slf4jLogger.isInfoEnabled()) { + slf4jLogger.info(msgSupplier.get()); + } + } + public void config(String msg) { slf4jLogger.info(msg); } + public void config(Supplier msgSupplier) { + info(msgSupplier); + } + public void fine(String msg) { slf4jLogger.debug(msg); } + public void fine(Supplier msgSupplier) { + if (slf4jLogger.isDebugEnabled()) { + slf4jLogger.debug(msgSupplier.get()); + } + } + public void finer(String msg) { slf4jLogger.trace(msg); } + public void finer(Supplier msgSupplier) { + if (slf4jLogger.isTraceEnabled()) { + slf4jLogger.trace(msgSupplier.get()); + } + } + public void finest(String msg) { slf4jLogger.trace(msg); } + public void finest(Supplier msgSupplier) { + finer(msgSupplier); + } + public void log(LogRecord record) { Level level = record.getLevel(); if (level.intValue() >= Level.SEVERE.intValue()) { @@ -186,6 +225,40 @@ public void log(Level level, String msg, Throwable thrown) { } } + public void log(Level level, Supplier 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 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(); @@ -235,6 +308,20 @@ public void logp( log(level, msg, thrown); } + public void logp( + Level level, String sourceClass, String sourceMethod, Supplier msgSupplier) { + log(level, msgSupplier); + } + + public void logp( + Level level, + String sourceClass, + String sourceMethod, + Throwable thrown, + Supplier msgSupplier) { + log(level, thrown, msgSupplier); + } + public void logrb( Level level, String sourceClass, String sourceMethod, String bundleName, String msg) { log(level, msg); diff --git a/javaagent-bootstrap/src/test/java/io/opentelemetry/javaagent/bootstrap/PatchLoggerTest.java b/javaagent-bootstrap/src/test/java/io/opentelemetry/javaagent/bootstrap/PatchLoggerTest.java index 209a48ab4f8c..98ec85849643 100644 --- a/javaagent-bootstrap/src/test/java/io/opentelemetry/javaagent/bootstrap/PatchLoggerTest.java +++ b/javaagent-bootstrap/src/test/java/io/opentelemetry/javaagent/bootstrap/PatchLoggerTest.java @@ -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); @@ -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; + } } }