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/fory.git


The following commit(s) were added to refs/heads/main by this push:
     new e66733aa8 chore(java): configure default Fory log level (#3657)
e66733aa8 is described below

commit e66733aa81c2cf382bd75af13463306de5596e2e
Author: Shawn Yang <[email protected]>
AuthorDate: Fri May 8 13:20:10 2026 +0800

    chore(java): configure default Fory log level (#3657)
    
    ## Why?
    
    
    
    ## What does this PR do?
    
    
    
    ## Related issues
    
    
    
    ## AI Contribution Checklist
    
    
    
    - [ ] Substantial AI assistance was used in this PR: `yes` / `no`
    - [ ] If `yes`, I included a completed [AI Contribution
    
Checklist](https://github.com/apache/fory/blob/main/AI_POLICY.md#9-contributor-checklist-for-ai-assisted-prs)
    in this PR description and the required `AI Usage Disclosure`.
    - [ ] If `yes`, my PR description includes the required `ai_review`
    summary and screenshot evidence of the final clean AI review results
    from both fresh reviewers on the current PR diff or current HEAD after
    the latest code changes.
    
    
    
    ## Does this PR introduce any user-facing change?
    
    
    
    - [ ] Does this PR introduce any public API change?
    - [ ] Does this PR introduce any binary protocol compatibility change?
    
    ## Benchmark
---
 .agents/languages/java.md                          |  2 ++
 README.md                                          |  2 +-
 docs/guide/java/advanced-features.md               |  2 +-
 docs/guide/java/troubleshooting.md                 |  4 +--
 .../java/org/apache/fory/logging/LogLevel.java     | 23 +++++++++---
 .../org/apache/fory/logging/LoggerFactory.java     |  8 +++--
 .../org/apache/fory/logging/Slf4jLoggerTest.java   | 41 +++++++++++++++++++---
 7 files changed, 66 insertions(+), 16 deletions(-)

diff --git a/.agents/languages/java.md b/.agents/languages/java.md
index 5283d5a4c..b4d013a25 100644
--- a/.agents/languages/java.md
+++ b/.agents/languages/java.md
@@ -75,4 +75,6 @@ mvn -T16 test -Dtest=org.apache.fory.TestClass#testMethod
 
 - Set `FORY_CODE_DIR` to dump generated code.
 - Set `ENABLE_FORY_GENERATED_CLASS_UNIQUE_ID=false` when you need stable 
generated class names.
+- When debugging Java tests or runtime behavior, set `FORY_LOG_LEVEL=INFO` 
unless a narrower
+  level is required.
 - In IntelliJ IDEA, use a JDK 11+ project SDK and disable `--release` if it 
blocks `sun.misc.Unsafe` access.
diff --git a/README.md b/README.md
index 03e1ef149..5e8b708c6 100644
--- a/README.md
+++ b/README.md
@@ -113,7 +113,7 @@ For more detailed benchmarks and methodology, see [C++ 
Benchmarks](benchmarks/cp
 ### Go Serialization Performance
 
 <p align="center">
-<img src="docs/benchmarks/go/benchmark_combined.png" width="95%">
+<img src="docs/benchmarks/go/throughput.png" width="95%">
 </p>
 
 For more detailed benchmarks and methodology, see [Go 
Benchmark](benchmarks/go).
diff --git a/docs/guide/java/advanced-features.md 
b/docs/guide/java/advanced-features.md
index 7337643f4..8661f8755 100644
--- a/docs/guide/java/advanced-features.md
+++ b/docs/guide/java/advanced-features.md
@@ -155,7 +155,7 @@ Custom memory allocators are useful for:
 
 ### ForyLogger
 
-By default, Fory uses a custom logger `ForyLogger` for internal needs. It 
builds resulting logged data into a single string and sends it directly to 
`System.out`. The result line layout is similar to (in Log4j notation):
+By default, Fory uses a custom logger `ForyLogger` for internal needs at 
`WARN` level, or `INFO` level when `ENABLE_FORY_DEBUG_OUTPUT=1` is set. Set 
`FORY_LOG_LEVEL` to `ERROR`, `WARN`, `INFO`, or `DEBUG` to configure the 
process default level before startup. `ForyLogger` builds resulting logged data 
into a single string and sends it directly to `System.out`. The result line 
layout is similar to (in Log4j notation):
 
 ```
 %d{yyyy-MM-dd hh:mm:ss} %p  %C:%L [%t] - %m%n
diff --git a/docs/guide/java/troubleshooting.md 
b/docs/guide/java/troubleshooting.md
index 673499777..ab97d98c5 100644
--- a/docs/guide/java/troubleshooting.md
+++ b/docs/guide/java/troubleshooting.md
@@ -191,8 +191,8 @@ Fory fory = Fory.builder()
 3. **Verify registration order** - must be consistent across peers
 4. **Enable logging** to see internal operations:
 
-```java
-LoggerFactory.setLogLevel(LogLevel.DEBUG_LEVEL);
+```bash
+FORY_LOG_LEVEL=INFO mvn test -Dtest=org.apache.fory.TestClass#testMethod
 ```
 
 ## Related Topics
diff --git a/java/fory-core/src/main/java/org/apache/fory/logging/LogLevel.java 
b/java/fory-core/src/main/java/org/apache/fory/logging/LogLevel.java
index 10ecec756..6450ea60f 100644
--- a/java/fory-core/src/main/java/org/apache/fory/logging/LogLevel.java
+++ b/java/fory-core/src/main/java/org/apache/fory/logging/LogLevel.java
@@ -19,6 +19,7 @@
 
 package org.apache.fory.logging;
 
+import java.util.Locale;
 import org.apache.fory.util.Utils;
 
 /** Defines a series of log levels. */
@@ -34,10 +35,24 @@ public class LogLevel {
   public static final int DEFAULT_LEVEL;
 
   static {
-    if (Utils.DEBUG_OUTPUT_ENABLED) {
-      DEFAULT_LEVEL = DEBUG_LEVEL;
-    } else {
-      DEFAULT_LEVEL = ERROR_LEVEL;
+    DEFAULT_LEVEL = getDefaultLogLevel(System.getenv("FORY_LOG_LEVEL"), 
Utils.DEBUG_OUTPUT_ENABLED);
+  }
+
+  static int getDefaultLogLevel(String level, boolean debugOutputEnabled) {
+    if (level == null || level.trim().isEmpty()) {
+      return debugOutputEnabled ? INFO_LEVEL : WARN_LEVEL;
+    }
+    switch (level.trim().toUpperCase(Locale.ROOT)) {
+      case "ERROR":
+        return ERROR_LEVEL;
+      case "WARN":
+        return WARN_LEVEL;
+      case "INFO":
+        return INFO_LEVEL;
+      case "DEBUG":
+        return DEBUG_LEVEL;
+      default:
+        return debugOutputEnabled ? INFO_LEVEL : WARN_LEVEL;
     }
   }
 
diff --git 
a/java/fory-core/src/main/java/org/apache/fory/logging/LoggerFactory.java 
b/java/fory-core/src/main/java/org/apache/fory/logging/LoggerFactory.java
index 0f50c3090..ec28a6d93 100644
--- a/java/fory-core/src/main/java/org/apache/fory/logging/LoggerFactory.java
+++ b/java/fory-core/src/main/java/org/apache/fory/logging/LoggerFactory.java
@@ -26,7 +26,7 @@ import org.apache.fory.util.GraalvmSupport;
  */
 public class LoggerFactory {
   private static volatile boolean useSlf4jLogger;
-  private static volatile int logLevel = LogLevel.INFO_LEVEL;
+  private static volatile int logLevel = LogLevel.DEFAULT_LEVEL;
 
   /** Disable Logger, there will be no log output. */
   public static void disableLogging() {
@@ -38,7 +38,7 @@ public class LoggerFactory {
    * Slf4jLogger} through {@link LoggerFactory#createSlf4jLogger(Class)}.
    */
   public static void enableLogging() {
-    logLevel = LogLevel.INFO_LEVEL;
+    logLevel = LogLevel.DEFAULT_LEVEL;
   }
 
   public static boolean isLoggingDisabled() {
@@ -47,7 +47,9 @@ public class LoggerFactory {
 
   /**
    * Set the {@link ForyLogger} log output control level, the default is {@link
-   * LogLevel#INFO_LEVEL}.
+   * LogLevel#WARN_LEVEL}, or {@link LogLevel#INFO_LEVEL} when debug output is 
enabled. The default
+   * can be overridden by setting the {@code FORY_LOG_LEVEL} environment 
variable to {@code ERROR},
+   * {@code WARN}, {@code INFO}, or {@code DEBUG}.
    *
    * @param level The log control level to be set, see {@link LogLevel}.
    */
diff --git 
a/java/fory-core/src/test/java/org/apache/fory/logging/Slf4jLoggerTest.java 
b/java/fory-core/src/test/java/org/apache/fory/logging/Slf4jLoggerTest.java
index 228f4ced0..154655230 100644
--- a/java/fory-core/src/test/java/org/apache/fory/logging/Slf4jLoggerTest.java
+++ b/java/fory-core/src/test/java/org/apache/fory/logging/Slf4jLoggerTest.java
@@ -19,6 +19,8 @@
 
 package org.apache.fory.logging;
 
+import static org.testng.Assert.assertEquals;
+
 import org.testng.annotations.Test;
 
 public class Slf4jLoggerTest {
@@ -30,13 +32,42 @@ public class Slf4jLoggerTest {
     logger.info("testInfo");
     logger.info("testInfo {}", "placeHolder");
     logger.warn("testInfo {}", "placeHolder");
-    logger.error("testInfo {}", "placeHolder", new Exception("test log"));
-    logger.error("testInfo {}", "placeHolder", new Exception("test log"));
     foryLogger.info("testInfo");
     foryLogger.info("testInfo {}", "placeHolder");
     foryLogger.warn("testInfo {}", "placeHolder");
-    foryLogger.error("testInfo {}", "placeHolder", new Exception("test log"));
-    foryLogger.error(null, new Exception("test log"));
-    foryLogger.error("test log {} {}", new Exception("test log {} {}"));
+    int previousLogLevel = LoggerFactory.getLogLevel();
+    try {
+      LoggerFactory.disableLogging();
+      logger.error("testInfo {}", "placeHolder", new Exception("test log"));
+      logger.error("testInfo {}", "placeHolder", new Exception("test log"));
+      foryLogger.error("testInfo {}", "placeHolder", new Exception("test 
log"));
+      foryLogger.error(null, new Exception("test log"));
+      foryLogger.error("test log {} {}", new Exception("test log {} {}"));
+    } finally {
+      LoggerFactory.setLogLevel(previousLogLevel);
+    }
+  }
+
+  @Test
+  public void testDefaultLogLevel() {
+    assertEquals(LoggerFactory.getLogLevel(), LogLevel.DEFAULT_LEVEL);
+    String envLogLevel = System.getenv("FORY_LOG_LEVEL");
+    boolean debugOutputEnabled = 
"1".equals(System.getenv("ENABLE_FORY_DEBUG_OUTPUT"));
+    assertEquals(
+        LogLevel.DEFAULT_LEVEL, LogLevel.getDefaultLogLevel(envLogLevel, 
debugOutputEnabled));
+    if (envLogLevel == null) {
+      assertEquals(
+          LogLevel.DEFAULT_LEVEL, debugOutputEnabled ? LogLevel.INFO_LEVEL : 
LogLevel.WARN_LEVEL);
+    }
+    assertEquals(LogLevel.getDefaultLogLevel(null, false), 
LogLevel.WARN_LEVEL);
+    assertEquals(LogLevel.getDefaultLogLevel("", false), LogLevel.WARN_LEVEL);
+    assertEquals(LogLevel.getDefaultLogLevel(null, true), LogLevel.INFO_LEVEL);
+    assertEquals(LogLevel.getDefaultLogLevel("", true), LogLevel.INFO_LEVEL);
+    assertEquals(LogLevel.getDefaultLogLevel("error", true), 
LogLevel.ERROR_LEVEL);
+    assertEquals(LogLevel.getDefaultLogLevel("WARN", true), 
LogLevel.WARN_LEVEL);
+    assertEquals(LogLevel.getDefaultLogLevel("Info", false), 
LogLevel.INFO_LEVEL);
+    assertEquals(LogLevel.getDefaultLogLevel("DEBUG", false), 
LogLevel.DEBUG_LEVEL);
+    assertEquals(LogLevel.getDefaultLogLevel("unknown", false), 
LogLevel.WARN_LEVEL);
+    assertEquals(LogLevel.getDefaultLogLevel("unknown", true), 
LogLevel.INFO_LEVEL);
   }
 }


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

Reply via email to