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 0b3e7d15 chore(java): FuryLogger add level control. (#1492)
0b3e7d15 is described below

commit 0b3e7d15773b6e503f3c84bc99dac998d2094842
Author: LiangliangSui <[email protected]>
AuthorDate: Fri Apr 12 00:20:27 2024 +0800

    chore(java): FuryLogger add level control. (#1492)
    
    <!--
    **Thanks for contributing to Fury.**
    
    **If this is your first time opening a PR on fury, you can refer to
    
[CONTRIBUTING.md](https://github.com/apache/incubator-fury/blob/main/CONTRIBUTING.md).**
    
    Contribution Checklist
    
    - The **Apache Fury (incubating)** community has restrictions on the
    naming of pr titles. You can also find instructions in
    
[CONTRIBUTING.md](https://github.com/apache/incubator-fury/blob/main/CONTRIBUTING.md).
    
    - Fury has a strong focus on performance. If the PR you submit will have
    an impact on performance, please benchmark it first and provide the
    benchmark result here.
    -->
    
    ## What does this PR do?
    
    <!-- Describe the purpose of this PR. -->
    FuryLooger adds default log level control.
    
    ## Related issues
    
    <!--
    Is there any related issue? Please attach here.
    
    - #xxxx0
    - #xxxx1
    - #xxxx2
    -->
    
    
    ## 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.
    -->
    
    ---------
    
    Signed-off-by: LiangliangSui <[email protected]>
---
 .../java/org/apache/fury/logging/FuryLogger.java   | 70 +++++++++++++++++-----
 .../java/org/apache/fury/logging/LogLevel.java     | 41 +++++++++++++
 .../org/apache/fury/logging/LoggerFactory.java     | 14 +++--
 .../java/org/apache/fury/logging/Slf4jLogger.java  | 13 ++--
 4 files changed, 111 insertions(+), 27 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 78301805..d46f76e3 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
@@ -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 java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 
@@ -26,85 +30,119 @@ 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) {
-    log("INFO", msg, new Object[0], false);
+    if (level >= INFO_LEVEL) {
+      log(INFO_LEVEL, msg, new Object[0], false);
+    }
   }
 
   @Override
   public void info(String msg, Object arg) {
-    log("INFO", msg, new Object[] {arg}, false);
+    if (level >= INFO_LEVEL) {
+      log(INFO_LEVEL, msg, new Object[] {arg}, false);
+    }
   }
 
   @Override
   public void info(String msg, Object arg1, Object arg2) {
-    log("INFO", msg, new Object[] {arg1, arg2}, false);
+    if (level >= INFO_LEVEL) {
+      log(INFO_LEVEL, msg, new Object[] {arg1, arg2}, false);
+    }
   }
 
   @Override
   public void info(String msg, Object... args) {
-    log("INFO", msg, args, false);
+    if (level >= INFO_LEVEL) {
+      log(INFO_LEVEL, msg, args, false);
+    }
   }
 
   @Override
   public void warn(String msg) {
-    log("WARN", msg, new Object[0], false);
+    if (level >= WARN_LEVEL) {
+      log(WARN_LEVEL, msg, new Object[0], false);
+    }
   }
 
   @Override
   public void warn(String msg, Object arg) {
-    log("WARN", msg, new Object[] {arg}, true);
+    if (level >= WARN_LEVEL) {
+      log(WARN_LEVEL, msg, new Object[] {arg}, true);
+    }
   }
 
   @Override
   public void warn(String msg, Object arg1, Object arg2) {
-    log("WARN", msg, new Object[] {arg1, arg2}, true);
+    if (level >= WARN_LEVEL) {
+      log(WARN_LEVEL, msg, new Object[] {arg1, arg2}, true);
+    }
   }
 
   @Override
   public void warn(String msg, Object... args) {
-    log("WARN", msg, args, true);
+    if (level >= WARN_LEVEL) {
+      log(WARN_LEVEL, msg, args, true);
+    }
   }
 
   @Override
   public void warn(String msg, Throwable t) {
-    log("WARN", msg, new Object[] {t}, true);
+    if (level >= WARN_LEVEL) {
+      log(WARN_LEVEL, msg, new Object[] {t}, true);
+    }
   }
 
   @Override
   public void error(String msg) {
-    log("ERROR", msg, new Object[0], false);
+    if (level >= ERROR_LEVEL) {
+      log(WARN_LEVEL, msg, new Object[0], false);
+    }
   }
 
   @Override
   public void error(String msg, Object arg) {
-    log("ERROR", msg, new Object[] {arg}, true);
+    if (level >= ERROR_LEVEL) {
+      log(ERROR_LEVEL, msg, new Object[] {arg}, true);
+    }
   }
 
   @Override
   public void error(String msg, Object arg1, Object arg2) {
-    log("ERROR", msg, new Object[] {arg1, arg2}, true);
+    if (level >= ERROR_LEVEL) {
+      log(ERROR_LEVEL, msg, new Object[] {arg1, arg2}, true);
+    }
   }
 
   @Override
   public void error(String msg, Object... args) {
-    log("ERROR", msg, args, true);
+    if (level >= ERROR_LEVEL) {
+      log(ERROR_LEVEL, msg, args, true);
+    }
   }
 
   @Override
   public void error(String msg, Throwable t) {
-    log("ERROR", msg, new Object[] {t}, true);
+    if (level >= ERROR_LEVEL) {
+      log(ERROR_LEVEL, msg, new Object[] {t}, true);
+    }
   }
 
-  private void log(String level, String msg, Object[] args, boolean 
mayPrintTrace) {
+  private void log(int level, String msg, Object[] args, boolean 
mayPrintTrace) {
     StringBuilder builder = new 
StringBuilder(dateTimeFormatter.format(LocalDateTime.now()));
-    builder.append(" ").append(level);
+    builder.append(" ").append(LogLevel.level2String(level));
     builder.append("  ").append(name);
     int lineNumber = Thread.currentThread().getStackTrace()[3].getLineNumber();
     builder.append(":").append(lineNumber);
diff --git a/java/fury-core/src/main/java/org/apache/fury/logging/LogLevel.java 
b/java/fury-core/src/main/java/org/apache/fury/logging/LogLevel.java
new file mode 100644
index 00000000..6e07d3de
--- /dev/null
+++ b/java/fury-core/src/main/java/org/apache/fury/logging/LogLevel.java
@@ -0,0 +1,41 @@
+/*
+ * 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;
+
+public class LogLevel {
+  public static final int ERROR_LEVEL = 0;
+
+  public static final int WARN_LEVEL = 1;
+
+  public static final int INFO_LEVEL = 2;
+
+  public static String level2String(int level) {
+    switch (level) {
+      case ERROR_LEVEL:
+        return "ERROR";
+      case WARN_LEVEL:
+        return "WARN";
+      case INFO_LEVEL:
+        return "INFO";
+      default:
+        return "UNKNOWN";
+    }
+  }
+}
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 9b18d297..7dd63d48 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
@@ -27,6 +27,7 @@ import org.apache.fury.util.GraalvmSupport;
 public class LoggerFactory {
   private static volatile boolean disableLogging;
   private static volatile boolean useSlf4jlogger;
+  private static volatile int logLevel;
 
   public static void disableLogging() {
     disableLogging = true;
@@ -36,6 +37,10 @@ public class LoggerFactory {
     disableLogging = false;
   }
 
+  public static void setLogLevel(int level) {
+    logLevel = level;
+  }
+
   public static void useSlf4jLogging(boolean useSlf4jLogging) {
     LoggerFactory.useSlf4jlogger = useSlf4jLogging;
   }
@@ -44,13 +49,10 @@ public class LoggerFactory {
     if (disableLogging) {
       return new NilLogger();
     } else {
-      if (GraalvmSupport.IN_GRAALVM_NATIVE_IMAGE) {
-        return new FuryLogger(clazz);
-      }
-      if (useSlf4jlogger) {
-        return createSlf4jLogger(clazz);
+      if (GraalvmSupport.IN_GRAALVM_NATIVE_IMAGE || !useSlf4jlogger) {
+        return new FuryLogger(clazz, logLevel);
       } else {
-        return new FuryLogger(clazz);
+        return createSlf4jLogger(clazz);
       }
     }
   }
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 2f621eb3..5f5d4fdf 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
@@ -25,10 +25,13 @@ import org.slf4j.spi.LocationAwareLogger;
 public class Slf4jLogger implements Logger {
   private static final String FQCN = Slf4jLogger.class.getName();
 
+  private final boolean isLocationAwareLogger;
+
   private final org.slf4j.Logger logger;
 
   public Slf4jLogger(Class<?> cls) {
     this.logger = org.slf4j.LoggerFactory.getLogger(cls);
+    this.isLocationAwareLogger = logger instanceof LocationAwareLogger;
   }
 
   @Override
@@ -48,7 +51,7 @@ public class Slf4jLogger implements Logger {
 
   @Override
   public void info(String msg, Object... args) {
-    if (logger instanceof LocationAwareLogger) {
+    if (isLocationAwareLogger) {
       ((LocationAwareLogger) logger).log(null, FQCN, 
LocationAwareLogger.INFO_INT, msg, args, null);
     } else {
       logger.info(msg, args);
@@ -72,7 +75,7 @@ public class Slf4jLogger implements Logger {
 
   @Override
   public void warn(String msg, Object... args) {
-    if (logger instanceof LocationAwareLogger) {
+    if (isLocationAwareLogger) {
       ((LocationAwareLogger) logger).log(null, FQCN, 
LocationAwareLogger.WARN_INT, msg, args, null);
     } else {
       logger.warn(msg, args);
@@ -81,7 +84,7 @@ public class Slf4jLogger implements Logger {
 
   @Override
   public void warn(String msg, Throwable t) {
-    if (logger instanceof LocationAwareLogger) {
+    if (isLocationAwareLogger) {
       ((LocationAwareLogger) logger).log(null, FQCN, 
LocationAwareLogger.WARN_INT, msg, null, t);
     } else {
       logger.warn(msg, t);
@@ -105,7 +108,7 @@ public class Slf4jLogger implements Logger {
 
   @Override
   public void error(String msg, Object... args) {
-    if (logger instanceof LocationAwareLogger) {
+    if (isLocationAwareLogger) {
       ((LocationAwareLogger) logger)
           .log(null, FQCN, LocationAwareLogger.ERROR_INT, msg, args, null);
     } else {
@@ -115,7 +118,7 @@ public class Slf4jLogger implements Logger {
 
   @Override
   public void error(String msg, Throwable t) {
-    if (logger instanceof LocationAwareLogger) {
+    if (isLocationAwareLogger) {
       ((LocationAwareLogger) logger).log(null, FQCN, 
LocationAwareLogger.ERROR_INT, msg, null, t);
     } else {
       logger.error(msg, t);


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

Reply via email to