[RocksJava] Change log level at runtime in custom logger

This commit introduces the possibility to change the log level
of a used custom logger at runtime.
This commit is contained in:
fyrz 2015-03-10 22:57:15 +01:00
parent a3bd4142f2
commit 814627af3d
4 changed files with 130 additions and 1 deletions

View File

@ -154,6 +154,30 @@ void Java_org_rocksdb_AbstractLogger_createNewLoggerDbOptions(
rocksdb::AbstractLoggerJni::setHandle(env, jobj, pLoggerJniCallback); 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<rocksdb::LoggerJniCallback> *handle =
reinterpret_cast<std::shared_ptr<rocksdb::LoggerJniCallback> *>(jhandle);
(*handle)->SetInfoLogLevel(static_cast<rocksdb::InfoLogLevel>(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<rocksdb::LoggerJniCallback> *handle =
reinterpret_cast<std::shared_ptr<rocksdb::LoggerJniCallback> *>(jhandle);
return static_cast<jbyte>((*handle)->GetInfoLogLevel());
}
/* /*
* Class: org_rocksdb_AbstractLogger * Class: org_rocksdb_AbstractLogger
* Method: disposeInternal * Method: disposeInternal

View File

@ -20,6 +20,9 @@ namespace rocksdb {
public: public:
LoggerJniCallback(JNIEnv* env, jobject jAbstractLogger); LoggerJniCallback(JNIEnv* env, jobject jAbstractLogger);
virtual ~LoggerJniCallback(); virtual ~LoggerJniCallback();
using Logger::SetInfoLogLevel;
using Logger::GetInfoLogLevel;
// Write an entry to the log file with the specified format. // Write an entry to the log file with the specified format.
virtual void Logv(const char* format, va_list ap); virtual void Logv(const char* format, va_list ap);
// Write an entry to the log file with the specified log level // Write an entry to the log file with the specified log level

View File

@ -37,6 +37,25 @@ public abstract class AbstractLogger extends RocksObject {
createNewLoggerDbOptions(dboptions.nativeHandle_); 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, protected abstract void log(InfoLogLevel infoLogLevel,
String logMsg); String logMsg);
@ -56,5 +75,8 @@ public abstract class AbstractLogger extends RocksObject {
long options); long options);
protected native void createNewLoggerDbOptions( protected native void createNewLoggerDbOptions(
long dbOptions); long dbOptions);
protected native void setInfoLogLevel(long handle,
byte infoLogLevel);
protected native byte infoLogLevel(long handle);
private native void disposeInternal(long handle); private native void disposeInternal(long handle);
} }

View File

@ -99,6 +99,7 @@ public class AbstractLoggerTest {
@Test @Test
public void dbOptionsLogger() throws RocksDBException { public void dbOptionsLogger() throws RocksDBException {
RocksDB db = null; RocksDB db = null;
AbstractLogger abstractLogger = null;
List<ColumnFamilyHandle> cfHandles = new ArrayList<>(); List<ColumnFamilyHandle> cfHandles = new ArrayList<>();
List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>(); List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>();
cfDescriptors.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); cfDescriptors.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY));
@ -111,7 +112,7 @@ public class AbstractLoggerTest {
setCreateIfMissing(true); setCreateIfMissing(true);
// Create new logger with max log level passed by options // Create new logger with max log level passed by options
AbstractLogger abstractLogger = new AbstractLogger(options) { abstractLogger = new AbstractLogger(options) {
@Override @Override
protected void log(InfoLogLevel infoLogLevel, String logMsg) { protected void log(InfoLogLevel infoLogLevel, String logMsg) {
assertThat(logMsg).isNotNull(); assertThat(logMsg).isNotNull();
@ -135,6 +136,85 @@ public class AbstractLoggerTest {
if (db != null) { if (db != null) {
db.close(); 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);
}
}