diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b8115a500..e26e337615 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ [Full changelog](https://github.com/mozilla/glean/compare/v52.6.0...main) +* General + * Allow user to control the log level of the Glean ([#2459](https://github.com/mozilla/glean/pull/2459)) + # v52.6.0 (2023-04-20) [Full changelog](https://github.com/mozilla/glean/compare/v52.5.0...v52.6.0) diff --git a/glean-core/android/src/main/java/mozilla/telemetry/glean/Glean.kt b/glean-core/android/src/main/java/mozilla/telemetry/glean/Glean.kt index e1eca4f88f..12ad773121 100644 --- a/glean-core/android/src/main/java/mozilla/telemetry/glean/Glean.kt +++ b/glean-core/android/src/main/java/mozilla/telemetry/glean/Glean.kt @@ -482,6 +482,15 @@ open class GleanInternalAPI internal constructor() { gleanSetLogPings(value) } + /** + * Sets the log level option for Glean internal processes. + * + * @param value One of the following options: "Debug", "Error", "Info", "Off", "Trace", "Warn". + */ + fun setLogLevel(value: String) { + gleanSetLogLevel(value) + } + /** * TEST ONLY FUNCTION. * This is called by the GleanTestRule, to enable test mode. diff --git a/glean-core/ios/Glean/Glean.swift b/glean-core/ios/Glean/Glean.swift index c2bb57e1e7..2a8742b3c9 100644 --- a/glean-core/ios/Glean/Glean.swift +++ b/glean-core/ios/Glean/Glean.swift @@ -352,6 +352,14 @@ public class Glean { gleanSetSourceTags(tags) } + /// Sets the log level option for Glean internal processes. + /// + /// - parameters: + /// * value: One of the following options: "Debug", "Error", "Info", "Off", "Trace", "Warn". + public func setLogLevel(_ value: String) { + gleanSetLogLevel(value) + } + /// Set configuration for metrics' disabled property, typically from remote_settings /// experiment or rollout. /// diff --git a/glean-core/rlb/src/lib.rs b/glean-core/rlb/src/lib.rs index f4311a434b..5be3b9072d 100644 --- a/glean-core/rlb/src/lib.rs +++ b/glean-core/rlb/src/lib.rs @@ -266,6 +266,15 @@ pub fn set_source_tags(tags: Vec) { glean_core::glean_set_source_tags(tags); } +/// Sets the log level for the `log` crate. +/// +/// # Arguments +/// +/// * `value` - A string representation of the `log::LevelFilter` enum. +pub fn set_log_level(value: String) { + glean_core::glean_set_log_level(value); +} + /// Returns a timestamp corresponding to "now" with millisecond precision. pub fn get_timestamp_ms() -> u64 { glean_core::get_timestamp_ms() diff --git a/glean-core/src/glean.udl b/glean-core/src/glean.udl index bbbe7f04f5..3d2e53e0d1 100644 --- a/glean-core/src/glean.udl +++ b/glean-core/src/glean.udl @@ -35,6 +35,7 @@ namespace glean { boolean glean_set_debug_view_tag(string tag); boolean glean_set_source_tags(sequence tags); void glean_set_log_pings(boolean value); + void glean_set_log_level(string log_level); void glean_handle_client_active(); void glean_handle_client_inactive(); diff --git a/glean-core/src/lib.rs b/glean-core/src/lib.rs index d03d901ffa..eaffd635d5 100644 --- a/glean-core/src/lib.rs +++ b/glean-core/src/lib.rs @@ -25,6 +25,7 @@ use std::thread; use std::time::Duration; use crossbeam_channel::unbounded; +use log::{set_max_level, LevelFilter}; use once_cell::sync::{Lazy, OnceCell}; use uuid::Uuid; @@ -864,6 +865,29 @@ pub fn glean_set_log_pings(value: bool) { } } +/// Sets the log level for the `log` crate. +/// +/// # Arguments +/// +/// * `value` - A string representation of the `log::LevelFilter` enum. +pub fn glean_set_log_level(value: String) { + let filter_level = match &value[..] { + "Debug" => LevelFilter::Debug, + "Error" => LevelFilter::Error, + "Info" => LevelFilter::Info, + "Off" => LevelFilter::Off, + "Trace" => LevelFilter::Trace, + "Warn" => LevelFilter::Warn, + _ => { + // We were unable to map to a logging level, so we return without setting anything. + log::error!("Invalid logging level: {}", value); + return; + } + }; + + set_max_level(filter_level); +} + /// Performs the collection/cleanup operations required by becoming active. /// /// This functions generates a baseline ping with reason `active`