diff --git a/zenoh-jni/src/logger.rs b/zenoh-jni/src/logger.rs index 19be0ec25..1011c905f 100644 --- a/zenoh-jni/src/logger.rs +++ b/zenoh-jni/src/logger.rs @@ -21,14 +21,15 @@ use crate::{errors::Result, jni_error, throw_exception}; /// Redirects the Rust logs either to logcat for Android systems or to the standard output (for non-Android systems). /// -/// This function is meant to be called from Java/Kotlin code through JNI. It takes a `log_level` -/// indicating the desired log level, which must be one of the following: "info", "debug", "warn", -/// "trace", or "error". +/// This function is meant to be called from Java/Kotlin code through JNI. It takes a `filter` +/// indicating the desired log level. +/// +/// See https://docs.rs/env_logger/latest/env_logger/index.html for accepted filter format. /// /// # Parameters: /// - `env`: The JNI environment. /// - `_class`: The JNI class. -/// - `log_level`: The log level java string indicating the desired log level. +/// - `filter`: The logs filter. /// /// # Errors: /// - If there is an error parsing the log level string, a `JNIException` is thrown on the JVM. @@ -38,10 +39,10 @@ use crate::{errors::Result, jni_error, throw_exception}; pub extern "C" fn Java_io_zenoh_Logger_00024Companion_startLogsViaJNI( mut env: JNIEnv, _class: JClass, - log_level: JString, + filter: JString, ) { || -> Result<()> { - let log_level = parse_log_level(&mut env, log_level)?; + let log_level = parse_filter(&mut env, filter)?; android_logd_logger::builder() .parse_filters(log_level.as_str()) .tag_target_strip() @@ -52,7 +53,7 @@ pub extern "C" fn Java_io_zenoh_Logger_00024Companion_startLogsViaJNI( .unwrap_or_else(|err| throw_exception!(env, err)) } -fn parse_log_level(env: &mut JNIEnv, log_level: JString) -> Result { +fn parse_filter(env: &mut JNIEnv, log_level: JString) -> Result { let log_level = env.get_string(&log_level).map_err(|err| jni_error!(err))?; log_level .to_str() diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Logger.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Logger.kt index b900e0c76..b53025772 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Logger.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Logger.kt @@ -21,17 +21,17 @@ internal class Logger { internal const val LOG_ENV: String = "RUST_LOG" - fun start(logLevel: String) = runCatching { - startLogsViaJNI(logLevel) + fun start(filter: String) = runCatching { + startLogsViaJNI(filter) } /** * Redirects the rust logs either to logcat for Android systems or to the standard output (for non-android * systems). * - * @param logLevel must be either "info", "debug", "warn", "trace" or "error". + * See https://docs.rs/env_logger/latest/env_logger/index.html for accepted filter format. */ @Throws(Exception::class) - private external fun startLogsViaJNI(logLevel: String) + private external fun startLogsViaJNI(filter: String) } } diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Zenoh.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Zenoh.kt index 0209922fd..b9514574c 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Zenoh.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Zenoh.kt @@ -109,27 +109,33 @@ object Zenoh { } /** - * Try starting the logs with the level specified under the [LOG_ENV] environment variable. + * Initializes the zenoh runtime logger, using rust environment settings. + * E.g.: `RUST_LOG=info` will enable logging at info level. Similarly, you can set the variable to `error` or `debug`. + * + * Note that if the environment variable is not set, then logging will not be enabled. + * See https://docs.rs/env_logger/latest/env_logger/index.html for accepted filter format. * * @see Logger */ - fun tryInitLogFromEnv(): Result = runCatching { + fun tryInitLogFromEnv() { ZenohLoad - val logLevel = System.getenv(LOG_ENV) - ?: return Result.failure(Exception("Failure during logs initialization: couldn't find environment variable '$LOG_ENV'.")) - return Logger.start(logLevel) + Logger.start(System.getenv(LOG_ENV) ?: "") } /** - * Try starting the logs with the level specified under the [LOG_ENV] environment variable or by default [defaultLogLevel]. + * Initializes the zenoh runtime logger, using rust environment settings or the provided fallback level. + * E.g.: `RUST_LOG=info` will enable logging at info level. Similarly, you can set the variable to `error` or `debug`. + * + * Note that if the environment variable is not set, then [fallbackFilter] will be used instead. + * See https://docs.rs/env_logger/latest/env_logger/index.html for accepted filter format. * - * @param defaultLogLevel A string that must be either "info", "debug", "error", "trace", "warn". + * @param fallbackFilter: The fallback filter if the `RUST_LOG` environment variable is not set. * @see Logger */ - fun initLogFromEnvOr(defaultLogLevel: String): Result = runCatching { + fun initLogFromEnvOr(fallbackFilter: String): Result = runCatching { ZenohLoad val logLevelProp = System.getenv(LOG_ENV) - logLevelProp?.let { Logger.start(it) } ?: Logger.start(defaultLogLevel) + logLevelProp?.let { Logger.start(it) } ?: Logger.start(fallbackFilter) } }