Merge 'bindings/java: Implement custom logger ' from Kim Seon Woo

## 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](https://github.com/tursodatabase/limbo/issues/615)

Closes #915
This commit is contained in:
Pekka Enberg
2025-02-07 12:37:56 +02:00
9 changed files with 147 additions and 25 deletions

View File

@@ -7,8 +7,8 @@ import org.github.tursodatabase.annotations.Nullable;
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);

View File

@@ -8,8 +8,8 @@ import java.sql.SQLException;
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);

View File

@@ -12,8 +12,8 @@ import org.github.tursodatabase.LimboErrorCode;
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 {

View File

@@ -3,8 +3,8 @@ package org.github.tursodatabase.core;
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

View File

@@ -4,8 +4,8 @@ import java.sql.SQLException;
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

View File

@@ -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);
}

View File

@@ -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));
}
}
}
}

View File

@@ -1,11 +0,0 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="${LOG_LEVEL:-info}">
<appender-ref ref="STDOUT" />
</root>
</configuration>