diff --git a/src/main/java/org/sqlite/JDBC.java b/src/main/java/org/sqlite/JDBC.java index b6419dedec..9c4813af44 100644 --- a/src/main/java/org/sqlite/JDBC.java +++ b/src/main/java/org/sqlite/JDBC.java @@ -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); } } diff --git a/src/main/java/org/sqlite/SQLiteJDBCLoader.java b/src/main/java/org/sqlite/SQLiteJDBCLoader.java index 5af86b436d..cfd41abc8e 100644 --- a/src/main/java/org/sqlite/SQLiteJDBCLoader.java +++ b/src/main/java/org/sqlite/SQLiteJDBCLoader.java @@ -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; @@ -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); } } @@ -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; } } @@ -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; } } @@ -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; } @@ -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; } } @@ -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; } diff --git a/src/main/java/org/sqlite/core/NativeDB.java b/src/main/java/org/sqlite/core/NativeDB.java index 4eef216384..a71e8ea9c8 100644 --- a/src/main/java/org/sqlite/core/NativeDB.java +++ b/src/main/java/org/sqlite/core/NativeDB.java @@ -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; @@ -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)); } @@ -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))); } diff --git a/src/main/java/org/sqlite/jdbc3/JDBC3DatabaseMetaData.java b/src/main/java/org/sqlite/jdbc3/JDBC3DatabaseMetaData.java index 3958d28a28..086fcbc000 100644 --- a/src/main/java/org/sqlite/jdbc3/JDBC3DatabaseMetaData.java +++ b/src/main/java/org/sqlite/jdbc3/JDBC3DatabaseMetaData.java @@ -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); } } } @@ -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); } } } diff --git a/src/main/java/org/sqlite/jdbc3/JDBC3Statement.java b/src/main/java/org/sqlite/jdbc3/JDBC3Statement.java index a634f3afa2..1af5694bd9 100644 --- a/src/main/java/org/sqlite/jdbc3/JDBC3Statement.java +++ b/src/main/java/org/sqlite/jdbc3/JDBC3Statement.java @@ -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; @@ -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)); } } diff --git a/src/main/java/org/sqlite/util/Logger.java b/src/main/java/org/sqlite/util/Logger.java index 78168e0802..556f801e7b 100644 --- a/src/main/java/org/sqlite/util/Logger.java +++ b/src/main/java/org/sqlite/util/Logger.java @@ -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 message); - void error(String message, Throwable t); + void info(Supplier message); - void error(String format, Object o1, Throwable t); + void warn(Supplier message); - void error(String format, Object o1, Object o2, Throwable t); + void error(Supplier message, Throwable t); } diff --git a/src/main/java/org/sqlite/util/LoggerFactory.java b/src/main/java/org/sqlite/util/LoggerFactory.java index cdcb63ec40..6b3e8c7618 100644 --- a/src/main/java/org/sqlite/util/LoggerFactory.java +++ b/src/main/java/org/sqlite/util/LoggerFactory.java @@ -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 @@ -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 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 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 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 message, Throwable t) { + if (logger.isLoggable(Level.SEVERE)) { + logger.log(Level.SEVERE, message.get(), t); } } } @@ -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 message) { + if (logger.isTraceEnabled()) { + logger.trace(message.get()); + } } @Override - public void error(String message, Throwable t) { - logger.error(message, t); + public void info(Supplier 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 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 message, Throwable t) { + if (logger.isErrorEnabled()) { + logger.error(message.get(), t); + } } } } diff --git a/src/main/java/org/sqlite/util/OSInfo.java b/src/main/java/org/sqlite/util/OSInfo.java index 05f2790684..731a155728 100644 --- a/src/main/java/org/sqlite/util/OSInfo.java +++ b/src/main/java/org/sqlite/util/OSInfo.java @@ -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"; } } @@ -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)