Skip to content

Commit

Permalink
fix: use lambda loggers and fix message format for JUL
Browse files Browse the repository at this point in the history
Closes: #1195
  • Loading branch information
gotson committed Oct 14, 2024
1 parent 761eb2e commit fd21058
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 85 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/sqlite/JDBC.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class JDBC implements Driver {
try {
DriverManager.registerDriver(new JDBC());
} catch (SQLException e) {
logger.error("Could not register driver", e);
logger.error(() -> "Could not register driver", e);
}
}

Expand Down
26 changes: 17 additions & 9 deletions src/main/java/org/sqlite/SQLiteJDBCLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.MessageFormat;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
Expand Down Expand Up @@ -101,12 +102,12 @@ static void cleanup() {
try {
Files.delete(nativeLib);
} catch (Exception e) {
logger.error("Failed to delete old native lib", e);
logger.error(() -> "Failed to delete old native lib", e);
}
}
});
} catch (IOException e) {
logger.error("Failed to open directory", e);
logger.error(() -> "Failed to open directory", e);
}
}

Expand Down Expand Up @@ -222,7 +223,7 @@ private static boolean extractAndLoadLibraryFile(
}
return loadNativeLibrary(targetFolder, extractedLibFileName);
} catch (IOException e) {
logger.error("Unexpected IOException", e);
logger.error(() -> "Unexpected IOException", e);
return false;
}
}
Expand All @@ -246,7 +247,7 @@ private static InputStream getResourceAsStream(String name) {
connection.setUseCaches(false);
return connection.getInputStream();
} catch (IOException e) {
logger.error("Could not connect", e);
logger.error(() -> "Could not connect", e);
return null;
}
}
Expand All @@ -268,9 +269,10 @@ private static boolean loadNativeLibrary(String path, String name) {
} catch (UnsatisfiedLinkError e) {

logger.error(
"Failed to load native library: {}. osinfo: {}",
name,
OSInfo.getNativeLibFolderPathForCurrentOS(),
() ->
MessageFormat.format(
"Failed to load native library: {0}. osinfo: {1}",
name, OSInfo.getNativeLibFolderPathForCurrentOS()),
e);
return false;
}
Expand All @@ -285,7 +287,7 @@ private static boolean loadNativeLibraryJdk() {
System.loadLibrary(LibraryLoaderUtil.NATIVE_LIB_BASE_NAME);
return true;
} catch (UnsatisfiedLinkError e) {
logger.error("Failed to load native library through System.loadLibrary", e);
logger.error(() -> "Failed to load native library through System.loadLibrary", e);
return false;
}
}
Expand Down Expand Up @@ -417,8 +419,14 @@ public static final class VersionHolder {
} catch (IOException e) {
// inline creation of logger to avoid build-time initialization of the logging
// framework in native-image
URL finalVersionFile = versionFile;
LoggerFactory.getLogger(VersionHolder.class)
.error("Could not read version from file: {}", versionFile, e);
.error(
() ->
MessageFormat.format(
"Could not read version from file: {0}",
finalVersionFile),
e);
}
VERSION = version;
}
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/org/sqlite/core/NativeDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.text.MessageFormat;
import org.sqlite.BusyHandler;
import org.sqlite.Collation;
import org.sqlite.Function;
Expand Down Expand Up @@ -90,10 +91,11 @@ protected synchronized void _open(String file, int openFlags) throws SQLExceptio
/** @see org.sqlite.core.DB#_exec(java.lang.String) */
@Override
public synchronized int _exec(String sql) throws SQLException {
if (logger.isTraceEnabled()) {
logger.trace(
"DriverManager [{}] [SQLite EXEC] {}", Thread.currentThread().getName(), sql);
}
logger.trace(
() ->
MessageFormat.format(
"DriverManager [{0}] [SQLite EXEC] {1}",
Thread.currentThread().getName(), sql));
return _exec_utf8(stringToUtf8ByteArray(sql));
}

Expand Down Expand Up @@ -125,10 +127,11 @@ public synchronized int _exec(String sql) throws SQLException {
/** @see org.sqlite.core.DB#prepare(java.lang.String) */
@Override
protected synchronized SafeStmtPtr prepare(String sql) throws SQLException {
if (logger.isTraceEnabled()) {
logger.trace(
"DriverManager [{}] [SQLite EXEC] {}", Thread.currentThread().getName(), sql);
}
logger.trace(
() ->
MessageFormat.format(
"DriverManager [{0}] [SQLite EXEC] {1}",
Thread.currentThread().getName(), sql));
return new SafeStmtPtr(this, prepare_utf8(stringToUtf8ByteArray(sql)));
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/sqlite/jdbc3/JDBC3DatabaseMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -964,14 +964,14 @@ public ResultSet getColumns(String c, String s, String tblNamePattern, String co
try {
rsColAutoinc.close();
} catch (Exception e) {
LogHolder.logger.error("Could not close ResultSet", e);
LogHolder.logger.error(() -> "Could not close ResultSet", e);
}
}
if (statColAutoinc != null) {
try {
statColAutoinc.close();
} catch (Exception e) {
LogHolder.logger.error("Could not close statement", e);
LogHolder.logger.error(() -> "Could not close statement", e);
}
}
}
Expand Down Expand Up @@ -1125,7 +1125,7 @@ public ResultSet getColumns(String c, String s, String tblNamePattern, String co
try {
rs.close();
} catch (Exception e) {
LogHolder.logger.error("Could not close ResultSet", e);
LogHolder.logger.error(() -> "Could not close ResultSet", e);
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/sqlite/jdbc3/JDBC3Statement.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.text.MessageFormat;
import java.util.Arrays;
import org.sqlite.ExtendedCommand;
import org.sqlite.ExtendedCommand.SQLExtension;
Expand Down Expand Up @@ -100,7 +101,10 @@ static class BackupObserver implements ProgressObserver {
private static final Logger logger = LoggerFactory.getLogger(BackupObserver.class);

public void progress(int remaining, int pageCount) {
logger.info("remaining:{}, page count:{}", remaining, pageCount);
logger.info(
() ->
MessageFormat.format(
"remaining:{0}, page count:{1}", remaining, pageCount));
}
}

Expand Down
16 changes: 6 additions & 10 deletions src/main/java/org/sqlite/util/Logger.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package org.sqlite.util;

import java.util.function.Supplier;

/** A simple internal Logger interface. */
public interface Logger {
boolean isTraceEnabled();

void trace(String format, Object o1, Object o2);

void info(String format, Object o1, Object o2);

void warn(String msg);
void trace(Supplier<String> message);

void error(String message, Throwable t);
void info(Supplier<String> message);

void error(String format, Object o1, Throwable t);
void warn(Supplier<String> message);

void error(String format, Object o1, Object o2, Throwable t);
void error(Supplier<String> message, Throwable t);
}
81 changes: 30 additions & 51 deletions src/main/java/org/sqlite/util/LoggerFactory.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.sqlite.util;

import java.text.MessageFormat;
import java.util.function.Supplier;
import java.util.logging.Level;

/**
* A factory for {@link Logger} instances that uses SLF4J if present, falling back on a
Expand Down Expand Up @@ -42,45 +43,30 @@ public JDKLogger(Class<?> hostClass) {
}

@Override
public boolean isTraceEnabled() {
return logger.isLoggable(java.util.logging.Level.FINEST);
}

@Override
public void trace(String format, Object o1, Object o2) {
if (logger.isLoggable(java.util.logging.Level.FINEST)) {
logger.log(java.util.logging.Level.FINEST, MessageFormat.format(format, o1, o2));
public void trace(Supplier<String> message) {
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST, message.get());
}
}

@Override
public void info(String format, Object o1, Object o2) {
if (logger.isLoggable(java.util.logging.Level.INFO)) {
logger.log(java.util.logging.Level.INFO, MessageFormat.format(format, o1, o2));
public void info(Supplier<String> message) {
if (logger.isLoggable(Level.INFO)) {
logger.log(Level.INFO, message.get());
}
}

@Override
public void warn(String msg) {
logger.log(java.util.logging.Level.WARNING, msg);
}

@Override
public void error(String message, Throwable t) {
logger.log(java.util.logging.Level.SEVERE, message, t);
}

@Override
public void error(String format, Object o1, Throwable t) {
if (logger.isLoggable(java.util.logging.Level.SEVERE)) {
logger.log(java.util.logging.Level.SEVERE, MessageFormat.format(format, o1), t);
public void warn(Supplier<String> message) {
if (logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING, message.get());
}
}

@Override
public void error(String format, Object o1, Object o2, Throwable t) {
if (logger.isLoggable(java.util.logging.Level.SEVERE)) {
logger.log(java.util.logging.Level.SEVERE, MessageFormat.format(format, o1, o2), t);
public void error(Supplier<String> message, Throwable t) {
if (logger.isLoggable(Level.SEVERE)) {
logger.log(Level.SEVERE, message.get(), t);
}
}
}
Expand All @@ -93,38 +79,31 @@ private static class SLF4JLogger implements Logger {
}

@Override
public boolean isTraceEnabled() {
return logger.isTraceEnabled();
}

@Override
public void trace(String format, Object o1, Object o2) {
logger.trace(format, o1, o2);
}

@Override
public void info(String format, Object o1, Object o2) {
logger.info(format, o1, o2);
}

@Override
public void warn(String msg) {
logger.warn(msg);
public void trace(Supplier<String> message) {
if (logger.isTraceEnabled()) {
logger.trace(message.get());
}
}

@Override
public void error(String message, Throwable t) {
logger.error(message, t);
public void info(Supplier<String> message) {
if (logger.isInfoEnabled()) {
logger.info(message.get());
}
}

@Override
public void error(String format, Object o1, Throwable t) {
logger.error(format, o1, t);
public void warn(Supplier<String> message) {
if (logger.isWarnEnabled()) {
logger.warn(message.get());
}
}

@Override
public void error(String format, Object o1, Object o2, Throwable t) {
logger.error(format, o1, o2, t);
public void error(Supplier<String> message, Throwable t) {
if (logger.isErrorEnabled()) {
logger.error(message.get(), t);
}
}
}
}
5 changes: 3 additions & 2 deletions src/main/java/org/sqlite/util/OSInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ static String getHardwareName() {
try {
return processRunner.runAndWaitFor("uname -m");
} catch (Throwable e) {
LogHolder.logger.error("Error while running uname -m", e);
LogHolder.logger.error(() -> "Error while running uname -m", e);
return "unknown";
}
}
Expand Down Expand Up @@ -229,7 +229,8 @@ static String resolveArmArchType() {
}
} else {
LogHolder.logger.warn(
"readelf not found. Cannot check if running on an armhf system, armel architecture will be presumed");
() ->
"readelf not found. Cannot check if running on an armhf system, armel architecture will be presumed");
}
} catch (IOException | InterruptedException e) {
// ignored: fall back to "arm" arch (soft-float ABI)
Expand Down

0 comments on commit fd21058

Please sign in to comment.