Update logger crate to match android_logger
- Mark with_min_level() as deprecated. This will be fully removed
once all platform code has been moved off of it.
- Switch logic over to use with_max_level().
This will allow updating individual pieces of the platform in
preparation for updating the android_logger crate.
Bug: 322718401
Test: build and run CF with the change.
Test: m aosp_cf_x86_64_phone
(cherry picked from https://android-review.googlesource.com/q/commit:6a26c18e0ddca1c2cffbfa8e94880fa194478653)
Merged-In: I094317c0c18b2802ac3fd8aa1b01229cad8da98d
Change-Id: I094317c0c18b2802ac3fd8aa1b01229cad8da98d
diff --git a/rust/logger.rs b/rust/logger.rs
index 19deda1..4664cf8 100644
--- a/rust/logger.rs
+++ b/rust/logger.rs
@@ -27,7 +27,7 @@
/// or env_logger where available.
#[derive(Default)]
pub struct Config<'a> {
- log_level: Option<log::Level>,
+ log_level: Option<log::LevelFilter>,
custom_format: Option<FormatFn>,
filter: Option<&'a str>,
#[allow(dead_code)] // Field is only used on device, and ignored on host.
@@ -36,11 +36,22 @@
/// Based on android_logger::Config
impl<'a> Config<'a> {
- /// Change the minimum log level.
+ // TODO: Remove on 0.13 version release.
+ /// **DEPRECATED**, use [`Config::with_max_level()`] instead.
+ #[deprecated(note = "use `.with_max_level()` instead")]
+ pub fn with_min_level(self, level: log::Level) -> Self {
+ self.with_max_level(level.to_level_filter())
+ }
+
+ /// Changes the maximum log level.
///
- /// All values above the set level are logged. For example, if
- /// `Warn` is set, the `Error` is logged too, but `Info` isn't.
- pub fn with_min_level(mut self, level: log::Level) -> Self {
+ /// Note, that `Trace` is the maximum level, because it provides the
+ /// maximum amount of detail in the emitted logs.
+ ///
+ /// If `Off` level is provided, then nothing is logged at all.
+ ///
+ /// [`log::max_level()`] is considered as the default level.
+ pub fn with_max_level(mut self, level: log::LevelFilter) -> Self {
self.log_level = Some(level);
self
}
@@ -56,7 +67,7 @@
/// # use universal_logger::Config;
/// universal_logger::init(
/// Config::default()
- /// .with_min_level(log::Level::Trace)
+ /// .with_max_level(log::LevelFilter::Trace)
/// .format(|record| format!("my_app: {}", record.args()))
/// )
/// ```
@@ -86,7 +97,7 @@
let mut builder = env_logger::Builder::from_default_env();
if let Some(log_level) = config.log_level {
- builder.filter_level(log_level.to_level_filter());
+ builder.filter_level(log_level);
}
if let Some(custom_format) = config.custom_format {
use std::io::Write; // Trait used by write!() macro, but not in Android code
@@ -116,7 +127,7 @@
// the builder instead.
let mut builder = android_logger::Config::default();
if let Some(log_level) = config.log_level {
- builder = builder.with_min_level(log_level);
+ builder = builder.with_max_level(log_level);
}
if let Some(custom_format) = config.custom_format {
builder = builder.format(move |f, r| {
@@ -149,7 +160,16 @@
.with_min_level(log::Level::Trace)
.with_min_level(log::Level::Error);
- assert_eq!(config.log_level, Some(log::Level::Error));
+ assert_eq!(config.log_level, Some(log::LevelFilter::Error));
+ }
+
+ #[test]
+ fn test_with_max_level() {
+ let config = Config::default()
+ .with_max_level(log::LevelFilter::Trace)
+ .with_max_level(log::LevelFilter::Error);
+
+ assert_eq!(config.log_level, Some(log::LevelFilter::Error));
}
#[test]
diff --git a/rust/tests/config_log_level.rs b/rust/tests/config_log_level.rs
index c4ff2d1..6c9e841 100644
--- a/rust/tests/config_log_level.rs
+++ b/rust/tests/config_log_level.rs
@@ -1,17 +1,17 @@
use std::env;
#[test]
-fn config_log_level() {
+fn config_log_max_level() {
// Environment variables should be overwritten by config values.
env::set_var("RUST_LOG", "debug");
let init_result = logger::init(
logger::Config::default()
- .with_min_level(log::Level::Trace));
+ .with_max_level(log::LevelFilter::Trace));
assert!(init_result);
// Setting the level through the Config struct should impact both host and device
assert_eq!(log::max_level(), log::LevelFilter::Trace);
env::remove_var("RUST_LOG");
-}
\ No newline at end of file
+}