diff --git a/java/rocksjni/loggerjnicallback.cc b/java/rocksjni/loggerjnicallback.cc index 170be6396..c6f7f1934 100644 --- a/java/rocksjni/loggerjnicallback.cc +++ b/java/rocksjni/loggerjnicallback.cc @@ -154,6 +154,30 @@ void Java_org_rocksdb_AbstractLogger_createNewLoggerDbOptions( rocksdb::AbstractLoggerJni::setHandle(env, jobj, pLoggerJniCallback); } +/* + * Class: org_rocksdb_AbstractLogger + * Method: setInfoLogLevel + * Signature: (JB)V + */ +void Java_org_rocksdb_AbstractLogger_setInfoLogLevel( + JNIEnv* env, jobject jobj, jlong jhandle, jbyte jlog_level) { + std::shared_ptr *handle = + reinterpret_cast *>(jhandle); + (*handle)->SetInfoLogLevel(static_cast(jlog_level)); +} + +/* + * Class: org_rocksdb_AbstractLogger + * Method: infoLogLevel + * Signature: (J)B + */ +jbyte Java_org_rocksdb_AbstractLogger_infoLogLevel( + JNIEnv* env, jobject jobj, jlong jhandle) { + std::shared_ptr *handle = + reinterpret_cast *>(jhandle); + return static_cast((*handle)->GetInfoLogLevel()); +} + /* * Class: org_rocksdb_AbstractLogger * Method: disposeInternal diff --git a/java/rocksjni/loggerjnicallback.h b/java/rocksjni/loggerjnicallback.h index 23bf2ba23..600390d14 100644 --- a/java/rocksjni/loggerjnicallback.h +++ b/java/rocksjni/loggerjnicallback.h @@ -20,6 +20,9 @@ namespace rocksdb { public: LoggerJniCallback(JNIEnv* env, jobject jAbstractLogger); virtual ~LoggerJniCallback(); + + using Logger::SetInfoLogLevel; + using Logger::GetInfoLogLevel; // Write an entry to the log file with the specified format. virtual void Logv(const char* format, va_list ap); // Write an entry to the log file with the specified log level diff --git a/java/src/main/java/org/rocksdb/AbstractLogger.java b/java/src/main/java/org/rocksdb/AbstractLogger.java index e7a40f324..74f328779 100644 --- a/java/src/main/java/org/rocksdb/AbstractLogger.java +++ b/java/src/main/java/org/rocksdb/AbstractLogger.java @@ -37,6 +37,25 @@ public abstract class AbstractLogger extends RocksObject { createNewLoggerDbOptions(dboptions.nativeHandle_); } + /** + * Set {@link org.rocksdb.InfoLogLevel} to AbstractLogger. + * + * @param infoLogLevel {@link org.rocksdb.InfoLogLevel} instance. + */ + public void setInfoLogLevel(InfoLogLevel infoLogLevel) { + setInfoLogLevel(nativeHandle_, infoLogLevel.getValue()); + } + + /** + * Return the loggers log level. + * + * @return {@link org.rocksdb.InfoLogLevel} instance. + */ + public InfoLogLevel infoLogLevel() { + return InfoLogLevel.getInfoLogLevel( + infoLogLevel(nativeHandle_)); + } + protected abstract void log(InfoLogLevel infoLogLevel, String logMsg); @@ -56,5 +75,8 @@ public abstract class AbstractLogger extends RocksObject { long options); protected native void createNewLoggerDbOptions( long dbOptions); + protected native void setInfoLogLevel(long handle, + byte infoLogLevel); + protected native byte infoLogLevel(long handle); private native void disposeInternal(long handle); } diff --git a/java/src/test/java/org/rocksdb/AbstractLoggerTest.java b/java/src/test/java/org/rocksdb/AbstractLoggerTest.java index 64062f1a9..7b6b1db37 100644 --- a/java/src/test/java/org/rocksdb/AbstractLoggerTest.java +++ b/java/src/test/java/org/rocksdb/AbstractLoggerTest.java @@ -99,6 +99,7 @@ public class AbstractLoggerTest { @Test public void dbOptionsLogger() throws RocksDBException { RocksDB db = null; + AbstractLogger abstractLogger = null; List cfHandles = new ArrayList<>(); List cfDescriptors = new ArrayList<>(); cfDescriptors.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); @@ -111,7 +112,7 @@ public class AbstractLoggerTest { setCreateIfMissing(true); // Create new logger with max log level passed by options - AbstractLogger abstractLogger = new AbstractLogger(options) { + abstractLogger = new AbstractLogger(options) { @Override protected void log(InfoLogLevel infoLogLevel, String logMsg) { assertThat(logMsg).isNotNull(); @@ -135,6 +136,85 @@ public class AbstractLoggerTest { if (db != null) { db.close(); } + if (abstractLogger != null) { + abstractLogger.dispose(); + } } } + + @Test + public void setInfoLogLevel() { + AbstractLogger abstractLogger = null; + try { + // Setup options + final Options options = new Options(). + setInfoLogLevel(InfoLogLevel.FATAL_LEVEL). + setCreateIfMissing(true); + + // Create new logger with max log level passed by options + abstractLogger = new AbstractLogger(options) { + @Override + protected void log(InfoLogLevel infoLogLevel, String logMsg) { + assertThat(logMsg).isNotNull(); + assertThat(logMsg.length()).isGreaterThan(0); + logMessageCounter.incrementAndGet(); + } + }; + assertThat(abstractLogger.infoLogLevel()). + isEqualTo(InfoLogLevel.FATAL_LEVEL); + abstractLogger.setInfoLogLevel(InfoLogLevel.DEBUG_LEVEL); + assertThat(abstractLogger.infoLogLevel()). + isEqualTo(InfoLogLevel.DEBUG_LEVEL); + } finally { + if (abstractLogger != null) { + abstractLogger.dispose(); + } + } + } + + @Test + public void changeLogLevelAtRuntime() throws RocksDBException { + RocksDB db = null; + logMessageCounter.set(0); + + try { + // Setup options + final Options options = new Options(). + setInfoLogLevel(InfoLogLevel.FATAL_LEVEL). + setCreateIfMissing(true); + + // Create new logger with max log level passed by options + AbstractLogger abstractLogger = new AbstractLogger(options) { + @Override + protected void log(InfoLogLevel infoLogLevel, String logMsg) { + assertThat(logMsg).isNotNull(); + assertThat(logMsg.length()).isGreaterThan(0); + logMessageCounter.incrementAndGet(); + } + }; + + // Set custom logger to options + options.setLogger(abstractLogger); + db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath()); + + // there should be zero messages + // using fatal level as log level. + assertThat(logMessageCounter.get()).isEqualTo(0); + + // change log level to debug level + abstractLogger.setInfoLogLevel(InfoLogLevel.DEBUG_LEVEL); + + db.put("key".getBytes(), "value".getBytes()); + db.flush(new FlushOptions().setWaitForFlush(true)); + + // messages shall be received due to previous actions. + assertThat(logMessageCounter.get()).isNotEqualTo(0); + + } finally { + if (db != null) { + db.close(); + } + } + logMessageCounter.set(0); + } }