This is an automated email from the ASF dual-hosted git repository.

chaokunyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-fury.git


The following commit(s) were added to refs/heads/main by this push:
     new 281b9e30 feat(java): support adjust logger level dynamically (#1557)
281b9e30 is described below

commit 281b9e300f485c4bfad3e398b4b714501c7e1c6d
Author: Shawn Yang <[email protected]>
AuthorDate: Tue Apr 23 18:50:37 2024 +0800

    feat(java): support adjust logger level dynamically (#1557)
    
    ## What does this PR do?
    This PR supports adjust logger level dynamically even Fury related
    classes are initialized.
    
    ## Related issues
    
    Closes #1554
    
    ## Does this PR introduce any user-facing change?
    
    <!--
    If any user-facing interface changes, please [open an
    issue](https://github.com/apache/incubator-fury/issues/new/choose)
    describing the need to do so and update the document if necessary.
    -->
    
    - [ ] Does this PR introduce any public API change?
    - [ ] Does this PR introduce any binary protocol compatibility change?
    
    
    ## Benchmark
    
    <!--
    When the PR has an impact on performance (if you don't know whether the
    PR will have an impact on performance, you can submit the PR first, and
    if it will have impact on performance, the code reviewer will explain
    it), be sure to attach a benchmark data here.
    -->
---
 .../java/org/apache/fury/logging/FuryLogger.java   | 34 +++++------
 .../org/apache/fury/logging/LoggerFactory.java     | 27 +++++----
 .../java/org/apache/fury/logging/NilLogger.java    | 65 ----------------------
 .../java/org/apache/fury/logging/Slf4jLogger.java  | 19 +++++++
 4 files changed, 48 insertions(+), 97 deletions(-)

diff --git 
a/java/fury-core/src/main/java/org/apache/fury/logging/FuryLogger.java 
b/java/fury-core/src/main/java/org/apache/fury/logging/FuryLogger.java
index d46f76e3..74820412 100644
--- a/java/fury-core/src/main/java/org/apache/fury/logging/FuryLogger.java
+++ b/java/fury-core/src/main/java/org/apache/fury/logging/FuryLogger.java
@@ -30,112 +30,106 @@ public class FuryLogger implements Logger {
   private static final DateTimeFormatter dateTimeFormatter =
       DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
   private final String name;
-  private final int level;
 
   public FuryLogger(Class<?> targetClass) {
-    this(targetClass, INFO_LEVEL);
-  }
-
-  public FuryLogger(Class<?> targetClass, int level) {
     this.name = targetClass.getSimpleName();
-    this.level = level;
   }
 
   // The implementation should not forward to other method, otherwise the 
fileNumber won't be right.
   @Override
   public void info(String msg) {
-    if (level >= INFO_LEVEL) {
+    if (LoggerFactory.getLogLevel() >= INFO_LEVEL) {
       log(INFO_LEVEL, msg, new Object[0], false);
     }
   }
 
   @Override
   public void info(String msg, Object arg) {
-    if (level >= INFO_LEVEL) {
+    if (LoggerFactory.getLogLevel() >= INFO_LEVEL) {
       log(INFO_LEVEL, msg, new Object[] {arg}, false);
     }
   }
 
   @Override
   public void info(String msg, Object arg1, Object arg2) {
-    if (level >= INFO_LEVEL) {
+    if (LoggerFactory.getLogLevel() >= INFO_LEVEL) {
       log(INFO_LEVEL, msg, new Object[] {arg1, arg2}, false);
     }
   }
 
   @Override
   public void info(String msg, Object... args) {
-    if (level >= INFO_LEVEL) {
+    if (LoggerFactory.getLogLevel() >= INFO_LEVEL) {
       log(INFO_LEVEL, msg, args, false);
     }
   }
 
   @Override
   public void warn(String msg) {
-    if (level >= WARN_LEVEL) {
+    if (LoggerFactory.getLogLevel() >= WARN_LEVEL) {
       log(WARN_LEVEL, msg, new Object[0], false);
     }
   }
 
   @Override
   public void warn(String msg, Object arg) {
-    if (level >= WARN_LEVEL) {
+    if (LoggerFactory.getLogLevel() >= WARN_LEVEL) {
       log(WARN_LEVEL, msg, new Object[] {arg}, true);
     }
   }
 
   @Override
   public void warn(String msg, Object arg1, Object arg2) {
-    if (level >= WARN_LEVEL) {
+    if (LoggerFactory.getLogLevel() >= WARN_LEVEL) {
       log(WARN_LEVEL, msg, new Object[] {arg1, arg2}, true);
     }
   }
 
   @Override
   public void warn(String msg, Object... args) {
-    if (level >= WARN_LEVEL) {
+    if (LoggerFactory.getLogLevel() >= WARN_LEVEL) {
       log(WARN_LEVEL, msg, args, true);
     }
   }
 
   @Override
   public void warn(String msg, Throwable t) {
-    if (level >= WARN_LEVEL) {
+    if (LoggerFactory.getLogLevel() >= WARN_LEVEL) {
       log(WARN_LEVEL, msg, new Object[] {t}, true);
     }
   }
 
   @Override
   public void error(String msg) {
-    if (level >= ERROR_LEVEL) {
+    if (LoggerFactory.getLogLevel() >= ERROR_LEVEL) {
       log(WARN_LEVEL, msg, new Object[0], false);
     }
   }
 
   @Override
   public void error(String msg, Object arg) {
-    if (level >= ERROR_LEVEL) {
+    if (LoggerFactory.getLogLevel() >= ERROR_LEVEL) {
       log(ERROR_LEVEL, msg, new Object[] {arg}, true);
     }
   }
 
   @Override
   public void error(String msg, Object arg1, Object arg2) {
-    if (level >= ERROR_LEVEL) {
+    if (LoggerFactory.getLogLevel() >= ERROR_LEVEL) {
       log(ERROR_LEVEL, msg, new Object[] {arg1, arg2}, true);
     }
   }
 
   @Override
   public void error(String msg, Object... args) {
-    if (level >= ERROR_LEVEL) {
+    if (LoggerFactory.getLogLevel() >= ERROR_LEVEL) {
       log(ERROR_LEVEL, msg, args, true);
     }
   }
 
   @Override
   public void error(String msg, Throwable t) {
-    if (level >= ERROR_LEVEL) {
+    if (LoggerFactory.getLogLevel() >= ERROR_LEVEL) {
       log(ERROR_LEVEL, msg, new Object[] {t}, true);
     }
   }
diff --git 
a/java/fury-core/src/main/java/org/apache/fury/logging/LoggerFactory.java 
b/java/fury-core/src/main/java/org/apache/fury/logging/LoggerFactory.java
index c5cd46da..fce5eed1 100644
--- a/java/fury-core/src/main/java/org/apache/fury/logging/LoggerFactory.java
+++ b/java/fury-core/src/main/java/org/apache/fury/logging/LoggerFactory.java
@@ -25,13 +25,12 @@ import org.apache.fury.util.GraalvmSupport;
  * A logger factory which can be used to disable fury logging more easily than 
configure logging.
  */
 public class LoggerFactory {
-  private static volatile boolean disableLogging;
   private static volatile boolean useSlf4jLogger;
   private static volatile int logLevel = LogLevel.INFO_LEVEL;
 
   /** Disable Logger, there will be no log output. */
   public static void disableLogging() {
-    disableLogging = true;
+    logLevel = LogLevel.ERROR_LEVEL - 1;
   }
 
   /**
@@ -39,7 +38,11 @@ public class LoggerFactory {
    * Slf4jLogger} through {@link LoggerFactory#createSlf4jLogger(Class)}.
    */
   public static void enableLogging() {
-    disableLogging = false;
+    logLevel = LogLevel.INFO_LEVEL;
+  }
+
+  public static boolean isLoggingDisabled() {
+    return logLevel < LogLevel.ERROR_LEVEL;
   }
 
   /**
@@ -52,6 +55,10 @@ public class LoggerFactory {
     logLevel = level;
   }
 
+  public static int getLogLevel() {
+    return logLevel;
+  }
+
   /**
    * Set whether to use Slf4jLogging.
    *
@@ -66,18 +73,14 @@ public class LoggerFactory {
    * Get a Logger for log output.
    *
    * @param clazz Class of output Log.
-   * @return If the logger is disabled, {@link NilLogger} will be returned, 
otherwise {@link
-   *     FuryLogger} or {@link Slf4jLogger} will be returned.
+   * @return Return {@link FuryLogger} if sf4j is not enabled, otherwise 
{@link Slf4jLogger} will be
+   *     returned.
    */
   public static Logger getLogger(Class<?> clazz) {
-    if (disableLogging) {
-      return new NilLogger();
+    if (GraalvmSupport.IN_GRAALVM_NATIVE_IMAGE || !useSlf4jLogger) {
+      return new FuryLogger(clazz);
     } else {
-      if (GraalvmSupport.IN_GRAALVM_NATIVE_IMAGE || !useSlf4jLogger) {
-        return new FuryLogger(clazz, logLevel);
-      } else {
-        return createSlf4jLogger(clazz);
-      }
+      return createSlf4jLogger(clazz);
     }
   }
 
diff --git 
a/java/fury-core/src/main/java/org/apache/fury/logging/NilLogger.java 
b/java/fury-core/src/main/java/org/apache/fury/logging/NilLogger.java
deleted file mode 100644
index 44e26a8d..00000000
--- a/java/fury-core/src/main/java/org/apache/fury/logging/NilLogger.java
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.fury.logging;
-
-/** An empty logger which does nothing. */
-public class NilLogger implements Logger {
-  @Override
-  public void info(String msg) {}
-
-  @Override
-  public void info(String msg, Object arg) {}
-
-  @Override
-  public void info(String msg, Object arg1, Object arg2) {}
-
-  @Override
-  public void info(String msg, Object... args) {}
-
-  @Override
-  public void warn(String msg) {}
-
-  @Override
-  public void warn(String msg, Object arg) {}
-
-  @Override
-  public void warn(String msg, Object... args) {}
-
-  @Override
-  public void warn(String msg, Object arg1, Object arg2) {}
-
-  @Override
-  public void warn(String msg, Throwable t) {}
-
-  @Override
-  public void error(String msg) {}
-
-  @Override
-  public void error(String msg, Object arg) {}
-
-  @Override
-  public void error(String msg, Object arg1, Object arg2) {}
-
-  @Override
-  public void error(String msg, Object... args) {}
-
-  @Override
-  public void error(String msg, Throwable t) {}
-}
diff --git 
a/java/fury-core/src/main/java/org/apache/fury/logging/Slf4jLogger.java 
b/java/fury-core/src/main/java/org/apache/fury/logging/Slf4jLogger.java
index 5f5d4fdf..5ea4476e 100644
--- a/java/fury-core/src/main/java/org/apache/fury/logging/Slf4jLogger.java
+++ b/java/fury-core/src/main/java/org/apache/fury/logging/Slf4jLogger.java
@@ -19,6 +19,10 @@
 
 package org.apache.fury.logging;
 
+import static org.apache.fury.logging.LogLevel.ERROR_LEVEL;
+import static org.apache.fury.logging.LogLevel.INFO_LEVEL;
+import static org.apache.fury.logging.LogLevel.WARN_LEVEL;
+
 import org.slf4j.spi.LocationAwareLogger;
 
 /** A logger to forward logging to slf4j. */
@@ -51,6 +55,9 @@ public class Slf4jLogger implements Logger {
 
   @Override
   public void info(String msg, Object... args) {
+    if (LoggerFactory.getLogLevel() < INFO_LEVEL) {
+      return;
+    }
     if (isLocationAwareLogger) {
       ((LocationAwareLogger) logger).log(null, FQCN, 
LocationAwareLogger.INFO_INT, msg, args, null);
     } else {
@@ -75,6 +82,9 @@ public class Slf4jLogger implements Logger {
 
   @Override
   public void warn(String msg, Object... args) {
+    if (LoggerFactory.getLogLevel() < WARN_LEVEL) {
+      return;
+    }
     if (isLocationAwareLogger) {
       ((LocationAwareLogger) logger).log(null, FQCN, 
LocationAwareLogger.WARN_INT, msg, args, null);
     } else {
@@ -84,6 +94,9 @@ public class Slf4jLogger implements Logger {
 
   @Override
   public void warn(String msg, Throwable t) {
+    if (LoggerFactory.getLogLevel() < WARN_LEVEL) {
+      return;
+    }
     if (isLocationAwareLogger) {
       ((LocationAwareLogger) logger).log(null, FQCN, 
LocationAwareLogger.WARN_INT, msg, null, t);
     } else {
@@ -108,6 +121,9 @@ public class Slf4jLogger implements Logger {
 
   @Override
   public void error(String msg, Object... args) {
+    if (LoggerFactory.getLogLevel() < ERROR_LEVEL) {
+      return;
+    }
     if (isLocationAwareLogger) {
       ((LocationAwareLogger) logger)
           .log(null, FQCN, LocationAwareLogger.ERROR_INT, msg, args, null);
@@ -118,6 +134,9 @@ public class Slf4jLogger implements Logger {
 
   @Override
   public void error(String msg, Throwable t) {
+    if (LoggerFactory.getLogLevel() < ERROR_LEVEL) {
+      return;
+    }
     if (isLocationAwareLogger) {
       ((LocationAwareLogger) logger).log(null, FQCN, 
LocationAwareLogger.ERROR_INT, msg, null, t);
     } else {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to