Skip to content

Commit

Permalink
Merge 'bindings/java: Implement custom logger ' from Kim Seon Woo
Browse files Browse the repository at this point in the history
## Purpose of the PR
- As bindings/java is just a library, we shouldn't have to add specific
library dependency(such as slf4j) to itself
- In order to load bindings/java to 3rd party software(such as
Datagrip), we shouldn't provide a library with slf4j included
## Changes
- Remove slf4j, logback dependency and files
- Add custom logger implementation
## ETC
We can now connect to Datagrip(but there are some errors though)
![image](https://github.com/user-
attachments/assets/ec8becf1-b9a8-415a-8943-74edee9b29c3)
## Reference
[Issue](#615)

Closes #915
  • Loading branch information
penberg committed Feb 7, 2025
2 parents 0841ed2 + cd8f580 commit 400dd6d
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 25 deletions.
5 changes: 1 addition & 4 deletions bindings/java/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,14 @@ repositories {
}

dependencies {
implementation("org.slf4j:slf4j-api:1.7.32")
compileOnly("org.slf4j:slf4j-api:1.7.32")

errorprone("com.uber.nullaway:nullaway:0.10.26") // maximum version which supports java 8
errorprone("com.google.errorprone:error_prone_core:2.10.0") // maximum version which supports java 8

testImplementation(platform("org.junit:junit-bom:5.10.0"))
testImplementation("org.junit.jupiter:junit-jupiter")
testImplementation("org.assertj:assertj-core:3.27.0")

testImplementation("ch.qos.logback:logback-classic:1.2.13")
testImplementation("ch.qos.logback:logback-core:1.2.13")
}

application {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import org.github.tursodatabase.annotations.SkipNullableCheck;
import org.github.tursodatabase.core.LimboConnection;
import org.github.tursodatabase.jdbc4.JDBC4Connection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.github.tursodatabase.utils.Logger;
import org.github.tursodatabase.utils.LoggerFactory;

public class JDBC implements Driver {
private static final Logger logger = LoggerFactory.getLogger(JDBC.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import java.util.Properties;
import org.github.tursodatabase.annotations.NativeInvocation;
import org.github.tursodatabase.utils.LimboExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.github.tursodatabase.utils.Logger;
import org.github.tursodatabase.utils.LoggerFactory;

public abstract class LimboConnection implements Connection {
private static final Logger logger = LoggerFactory.getLogger(LimboConnection.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import org.github.tursodatabase.annotations.NativeInvocation;
import org.github.tursodatabase.annotations.VisibleForTesting;
import org.github.tursodatabase.utils.LimboExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.github.tursodatabase.utils.Logger;
import org.github.tursodatabase.utils.LoggerFactory;

/** This class provides a thin JNI layer over the SQLite3 C API. */
public final class LimboDB extends AbstractDB {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import org.github.tursodatabase.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.github.tursodatabase.utils.Logger;
import org.github.tursodatabase.utils.LoggerFactory;

/**
* A table of data representing limbo database result set, which is generated by executing a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import org.github.tursodatabase.annotations.NativeInvocation;
import org.github.tursodatabase.annotations.Nullable;
import org.github.tursodatabase.utils.LimboExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.github.tursodatabase.utils.Logger;
import org.github.tursodatabase.utils.LoggerFactory;

/**
* By default, only one <code>resultSet</code> object per <code>LimboStatement</code> can be open at
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.github.tursodatabase.utils;

/** A simple internal Logger interface. */
public interface Logger {
void trace(String message, Object... params);

void debug(String message, Object... params);

void info(String message, Object... params);

void warn(String message, Object... params);

void error(String message, Object... params);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package org.github.tursodatabase.utils;

import java.util.logging.Level;

/**
* A factory for {@link Logger} instances that uses SLF4J if present, falling back on a
* java.util.logging implementation otherwise.
*/
public class LoggerFactory {
static final boolean USE_SLF4J;

static {
boolean useSLF4J;
try {
Class.forName("org.slf4j.Logger");
useSLF4J = true;
} catch (Exception e) {
useSLF4J = false;
}
USE_SLF4J = useSLF4J;
}

/**
* Get a {@link Logger} instance for the given host class.
*
* @param hostClass the host class from which log messages will be issued
* @return a Logger
*/
public static Logger getLogger(Class<?> hostClass) {
if (USE_SLF4J) {
return new SLF4JLogger(hostClass);
}

return new JDKLogger(hostClass);
}

private static class JDKLogger implements Logger {
final java.util.logging.Logger logger;

public JDKLogger(Class<?> hostClass) {
logger = java.util.logging.Logger.getLogger(hostClass.getCanonicalName());
}

@Override
public void trace(String message, Object... params) {
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST, String.format(message, params));
}
}

@Override
public void debug(String message, Object... params) {
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, String.format(message, params));
}
}

@Override
public void info(String message, Object... params) {
if (logger.isLoggable(Level.INFO)) {
logger.log(Level.INFO, String.format(message, params));
}
}

@Override
public void warn(String message, Object... params) {
if (logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING, String.format(message, params));
}
}

@Override
public void error(String message, Object... params) {
if (logger.isLoggable(Level.SEVERE)) {
logger.log(Level.SEVERE, String.format(message, params));
}
}
}

private static class SLF4JLogger implements Logger {
final org.slf4j.Logger logger;

SLF4JLogger(Class<?> hostClass) {
logger = org.slf4j.LoggerFactory.getLogger(hostClass);
}

@Override
public void trace(String message, Object... params) {
if (logger.isTraceEnabled()) {
logger.trace(message, String.format(message, params));
}
}

@Override
public void debug(String message, Object... params) {
if (logger.isDebugEnabled()) {
logger.debug(message, String.format(message, params));
}
}

@Override
public void info(String message, Object... params) {
if (logger.isInfoEnabled()) {
logger.info(message, String.format(message, params));
}
}

@Override
public void warn(String message, Object... params) {
if (logger.isWarnEnabled()) {
logger.warn(message, String.format(message, params));
}
}

@Override
public void error(String message, Object... params) {
if (logger.isErrorEnabled()) {
logger.error(message, String.format(message, params));
}
}
}
}
11 changes: 0 additions & 11 deletions bindings/java/src/test/resources/logback.xml

This file was deleted.

0 comments on commit 400dd6d

Please sign in to comment.