mirror of
https://github.com/facebook/rocksdb.git
synced 2024-11-26 16:30:56 +00:00
[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:
parent
a3bd4142f2
commit
814627af3d
|
@ -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<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
|
||||
* Method: disposeInternal
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -99,6 +99,7 @@ public class AbstractLoggerTest {
|
|||
@Test
|
||||
public void dbOptionsLogger() throws RocksDBException {
|
||||
RocksDB db = null;
|
||||
AbstractLogger abstractLogger = null;
|
||||
List<ColumnFamilyHandle> cfHandles = new ArrayList<>();
|
||||
List<ColumnFamilyDescriptor> 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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue