Skip to content

Commit

Permalink
fix(java): fix slf4j on graalvm (#1432)
Browse files Browse the repository at this point in the history
This PR closes #1404 by:
- Upgrade slf4j to 2.0.12
- Using print instead of slf4j for graalvm
  • Loading branch information
chaokunyang authored Mar 28, 2024
1 parent 552124e commit 5607cd7
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
2 changes: 1 addition & 1 deletion ci/run_ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ graalvm_test() {
mvn -T10 -B --no-transfer-progress clean install -DskipTests
echo "Start to build graalvm native image"
cd "$ROOT"/integration_tests/graalvm_tests
mvn -DskipTests=true -Pnative package
mvn -DskipTests=true --no-transfer-progress -Pnative package
echo "Built graalvm native image"
echo "Start to run graalvm native image"
./target/main
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

package org.apache.fury.util;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.helpers.NOPLogger;

Expand All @@ -40,7 +45,64 @@ public static Logger getLogger(Class<?> clazz) {
if (disableLogging) {
return NOPLogger.NOP_LOGGER;
} else {
if (GraalvmSupport.IN_GRAALVM_NATIVE_IMAGE) {
return (Logger)
Proxy.newProxyInstance(
clazz.getClassLoader(), new Class[] {Logger.class}, new GraalvmLogger(clazz));
}
return org.slf4j.LoggerFactory.getLogger(clazz);
}
}

private static final class GraalvmLogger implements InvocationHandler {
private static final DateTimeFormatter dateTimeFormatter =
DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
private final Class<?> targetClass;

private GraalvmLogger(Class<?> targetClass) {
this.targetClass = targetClass;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String name = method.getName();
switch (name) {
case "isEnabledForLevel":
case "isInfoEnabled":
case "isWarnEnabled":
case "isErrorEnabled":
return true;
case "info":
log("INFO", false, args);
return null;
case "warn":
log("WARN", false, args);
return null;
case "error":
log("ERROR", false, args);
return null;
default:
return method.invoke(NOPLogger.NOP_LOGGER, args);
}
}

private void log(String level, boolean mayPrintTrace, Object[] args) {
StringBuilder builder = new StringBuilder(dateTimeFormatter.format(LocalDateTime.now()));
builder.append(" ").append(level);
builder.append(" ").append(targetClass.getSimpleName());
builder.append(" [").append(Thread.currentThread().getName()).append(']');
builder.append(" -");
for (Object arg : args) {
builder.append(" ").append(arg);
}
System.out.println(builder);
int length = args.length;
if (mayPrintTrace && length > 0) {
Object o = args[length - 1];
if (o instanceof Throwable) {
((Throwable) o).printStackTrace();
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ Args=--initialize-at-build-time=org.apache.fury.memory.MemoryBuffer,\
org.apache.fury.shaded.org.codehaus.janino.Java$Invocation,\
org.apache.fury.shaded.org.codehaus.janino.ReflectionIClass,\
org.apache.fury.util.record.RecordUtils,\
org.apache.fury.util.LoggerFactory$GraalvmLogger,\
org.apache.fury.shaded.org.codehaus.janino.Java$Literal,\
org.apache.fury.shaded.org.codehaus.janino.IClassLoader,\
org.apache.fury.shaded.org.codehaus.janino.Java$PrimitiveType,\
Expand Down
2 changes: 1 addition & 1 deletion java/fury-test-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<artifactId>log4j-slf4j2-impl</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
Expand Down
2 changes: 1 addition & 1 deletion java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
<version>2.0.12</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
Expand Down

0 comments on commit 5607cd7

Please sign in to comment.