Skip to content

Commit

Permalink
Bug 1812738 - allow user to set Glean log level
Browse files Browse the repository at this point in the history
  • Loading branch information
rosahbruno committed May 1, 2023
1 parent dd5c3eb commit 924f07d
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions glean-core/ios/Glean/Glean.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
9 changes: 9 additions & 0 deletions glean-core/rlb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,15 @@ pub fn set_source_tags(tags: Vec<String>) {
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()
Expand Down
1 change: 1 addition & 0 deletions glean-core/src/glean.udl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace glean {
boolean glean_set_debug_view_tag(string tag);
boolean glean_set_source_tags(sequence<string> 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();
Expand Down
24 changes: 24 additions & 0 deletions glean-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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`
Expand Down

0 comments on commit 924f07d

Please sign in to comment.