Ver código fonte

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

* Add missing java.util.logging.Logger methods to PatchLogger

* spotless
Lauri Tulmin 3 anos atrás
pai
commit
2d0aa19179

+ 87 - 0
javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/PatchLogger.java

@@ -7,6 +7,7 @@ package io.opentelemetry.javaagent.bootstrap;
 
 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 class PatchLogger {
     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()) {
@@ -186,6 +225,40 @@ public class PatchLogger {
     }
   }
 
+  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();
@@ -235,6 +308,20 @@ public class PatchLogger {
     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);

+ 6 - 4
javaagent-bootstrap/src/test/java/io/opentelemetry/javaagent/bootstrap/PatchLoggerTest.java

@@ -55,10 +55,6 @@ class PatchLoggerTest {
       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 @@ class PatchLoggerTest {
     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;
+    }
   }
 }