Skip to content

Commit 645da61

Browse files
committed
feat: Support comet native log level conf
1 parent 72eb0e9 commit 645da61

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

common/src/main/java/org/apache/comet/NativeBase.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
import static org.apache.comet.Constants.LOG_CONF_NAME;
3737
import static org.apache.comet.Constants.LOG_CONF_PATH;
38+
import static org.apache.comet.Constants.LOG_LEVEL;
3839

3940
/** Base class for JNI bindings. MUST be inherited by all classes that introduce JNI APIs. */
4041
public abstract class NativeBase {
@@ -153,18 +154,25 @@ private static void bundleLoadLibrary() {
153154

154155
private static void initWithLogConf() {
155156
String logConfPath = System.getProperty(LOG_CONF_PATH(), Utils.getConfPath(LOG_CONF_NAME()));
157+
String logLevel = System.getProperty(LOG_LEVEL());
156158

157159
// If both the system property and the environmental variable failed to find a log
158160
// configuration, then fall back to using the deployed default
159161
if (logConfPath == null) {
160162
LOG.info(
161163
"Couldn't locate log file from either COMET_CONF_DIR or comet.log.file.path. "
162-
+ "Using default log configuration which emits to stdout");
164+
+ "Using default log configuration with {} log level which emits to stdout",
165+
logLevel == null ? "INFO" : logLevel);
163166
logConfPath = "";
164167
} else {
168+
// Ignore log level if a log configuration file is specified
169+
if (logLevel != null) {
170+
LOG.warn("Ignoring log level {} because a log configuration file is specified", logLevel);
171+
}
172+
165173
LOG.info("Using {} for native library logging", logConfPath);
166174
}
167-
init(logConfPath);
175+
init(logConfPath, logLevel);
168176
}
169177

170178
private static void cleanupOldTempLibs() {
@@ -283,5 +291,5 @@ private static String resourceName() {
283291
*
284292
* @param logConfPath location to the native log configuration file
285293
*/
286-
static native void init(String logConfPath);
294+
static native void init(String logConfPath, String logLevel);
287295
}

common/src/main/scala/org/apache/comet/Constants.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ package org.apache.comet
2222
object Constants {
2323
val LOG_CONF_PATH = "comet.log.file.path"
2424
val LOG_CONF_NAME = "log4rs.yaml"
25+
val LOG_LEVEL = "comet.log.level"
2526
}

native/core/src/lib.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use jni::{
3030
objects::{JClass, JString},
3131
JNIEnv, JavaVM,
3232
};
33-
use log::{info, LevelFilter};
33+
use log::info;
3434
use log4rs::{
3535
append::console::{ConsoleAppender, Target},
3636
config::{load_config_file, Appender, Deserializers, Root},
@@ -70,6 +70,7 @@ pub extern "system" fn Java_org_apache_comet_NativeBase_init(
7070
e: JNIEnv,
7171
_: JClass,
7272
log_conf_path: JString,
73+
log_level: JString,
7374
) {
7475
// Initialize the error handling to capture panic backtraces
7576
errors::init();
@@ -80,7 +81,11 @@ pub extern "system" fn Java_org_apache_comet_NativeBase_init(
8081
// empty path means there is no custom log4rs config file provided, so fallback to use
8182
// the default configuration
8283
let log_config = if path.is_empty() {
83-
default_logger_config()
84+
let log_level: String = match env.get_string(&log_level) {
85+
Ok(level) => level.into(),
86+
Err(_) => "info".parse().unwrap(),
87+
};
88+
default_logger_config(&log_level)
8489
} else {
8590
load_config_file(path, Deserializers::default())
8691
.map_err(|err| CometError::Config(err.to_string()))
@@ -100,14 +105,18 @@ pub extern "system" fn Java_org_apache_comet_NativeBase_init(
100105

101106
const LOG_PATTERN: &str = "{d(%y/%m/%d %H:%M:%S)} {l} {f}: {m}{n}";
102107

103-
// Creates a default log4rs config, which logs to console with `INFO` level.
104-
fn default_logger_config() -> CometResult<Config> {
108+
// Creates a default log4rs config, which logs to console with log level.
109+
fn default_logger_config(log_level: &str) -> CometResult<Config> {
105110
let console_append = ConsoleAppender::builder()
106111
.target(Target::Stderr)
107112
.encoder(Box::new(PatternEncoder::new(LOG_PATTERN)))
108113
.build();
109114
let appender = Appender::builder().build("console", Box::new(console_append));
110-
let root = Root::builder().appender("console").build(LevelFilter::Info);
115+
let root = Root::builder().appender("console").build(
116+
log_level
117+
.parse()
118+
.map_err(|err| CometError::Config(format!("{err}")))?,
119+
);
111120
Config::builder()
112121
.appender(appender)
113122
.build(root)

0 commit comments

Comments
 (0)