Skip to content

Commit f118570

Browse files
authored
Replace log with tracing for ios and android uniffi (#566)
## 🎟️ Tracking https://bitwarden.atlassian.net/browse/PM-26932 ## 📔 Objective Cascading the standardization of `tracing` crate from clients repo into the SDK, that was started per bitwarden/clients#16321 , this change updates the `bitwarden-uniffi` crate to use `tracing`. android: <img width="1472" height="147" alt="Screenshot 2025-11-21 at 10 15 51" src="https://github.com/user-attachments/assets/a92b79ce-8f87-4eac-975f-7b8a913e6fde" /> ios: <img width="1057" height="671" alt="Screenshot 2025-11-20 at 09 17 53" src="https://github.com/user-attachments/assets/30f37c89-de86-4455-8e55-701acf1dc41f" /> ## 🚨 Breaking Changes <!-- Does this PR introduce any breaking changes? If so, please describe the impact and migration path for clients. If you're unsure, the automated TypeScript compatibility check will run when you open/update this PR and provide feedback. For breaking changes: 1. Describe what changed in the client interface 2. Explain why the change was necessary 3. Provide migration steps for client developers 4. Link to any paired client PRs if needed Otherwise, you can remove this section. --> ## ⏰ Reminders before review - Contributor guidelines followed - All formatters and local linters executed and passed - Written new unit and / or integration tests where applicable - Protected functional changes with optionality (feature flags) - Used internationalization (i18n) for all UI strings - CI builds passed - Communicated to DevOps any deployment requirements - Updated any necessary documentation (Confluence, contributing docs) or informed the documentation team ## 🦮 Reviewer guidelines <!-- Suggested interactions but feel free to use (or not) as you desire! --> - 👍 (`:+1:`) or similar for great changes - 📝 (`:memo:`) or ℹ️ (`:information_source:`) for notes or general info - ❓ (`:question:`) for questions - 🤔 (`:thinking:`) or 💭 (`:thought_balloon:`) for more open inquiry that's not quite a confirmed issue and could potentially benefit from discussion - 🎨 (`:art:`) for suggestions / improvements - ❌ (`:x:`) or ⚠️ (`:warning:`) for more significant problems or concerns needing attention - 🌱 (`:seedling:`) or ♻️ (`:recycle:`) for future improvements or indications of technical debt - ⛏ (`:pick:`) for minor or nitpick changes
1 parent 1a076a1 commit f118570

File tree

4 files changed

+100
-68
lines changed

4 files changed

+100
-68
lines changed

Cargo.lock

Lines changed: 29 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bitwarden-uniffi/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,22 @@ bitwarden-state = { workspace = true, features = ["uniffi"] }
3434
bitwarden-uniffi-error = { workspace = true }
3535
bitwarden-vault = { workspace = true, features = ["uniffi"] }
3636
chrono = { workspace = true, features = ["std"] }
37-
env_logger = "0.11.1"
38-
log = { workspace = true }
3937
thiserror = { workspace = true }
38+
tracing = { workspace = true }
39+
tracing-subscriber = { workspace = true }
4040
uniffi = { workspace = true }
4141
uuid = { workspace = true }
4242

4343
[target.'cfg(target_os = "android")'.dependencies]
44-
android_logger = "0.15"
44+
tracing-android = "0.2.0"
4545

4646
# The use of rustls-platform-verifier requires some extra support to communicate with the Android platform
4747
jni = ">=0.21, <0.22"
4848
libloading = ">=0.8.1, <0.9"
4949
rustls-platform-verifier = "0.6.0"
5050

5151
[target.'cfg(target_os = "ios")'.dependencies]
52-
oslog = "0.2.0"
52+
tracing-oslog = "0.3.0"
5353

5454
[build-dependencies]
5555
uniffi = { workspace = true, features = ["build"] }

crates/bitwarden-uniffi/src/android_support.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{error::Error, sync::OnceLock};
22

33
use jni::sys::{JavaVM, jint, jsize};
4+
use tracing::{error, info};
45

56
pub static JAVA_VM: OnceLock<jni::JavaVM> = OnceLock::new();
67

@@ -9,7 +10,7 @@ pub static JAVA_VM: OnceLock<jni::JavaVM> = OnceLock::new();
910
#[allow(non_snake_case)]
1011
#[unsafe(no_mangle)]
1112
pub extern "system" fn JNI_OnLoad(vm_ptr: jni::JavaVM, _reserved: *mut std::ffi::c_void) -> jint {
12-
log::info!("JNI_OnLoad initializing");
13+
info!("JNI_OnLoad initializing");
1314
JAVA_VM.get_or_init(|| vm_ptr);
1415
jni::sys::JNI_VERSION_1_6
1516
}
@@ -18,25 +19,25 @@ pub fn init() {
1819
fn init_inner() -> Result<(), Box<dyn Error>> {
1920
let jvm = match JAVA_VM.get() {
2021
Some(jvm) => {
21-
log::info!("JavaVM already initialized");
22+
info!("JavaVM already initialized");
2223
jvm
2324
}
2425
None => {
25-
log::info!("JavaVM not initialized, initializing now");
26+
info!("JavaVM not initialized, initializing now");
2627
let jvm = java_vm()?;
2728
JAVA_VM.get_or_init(|| jvm)
2829
}
2930
};
3031

3132
let mut env = jvm.attach_current_thread_permanently()?;
32-
log::info!("Initializing Android verifier");
33+
info!("Initializing Android verifier");
3334
init_verifier(&mut env)?;
34-
log::info!("SDK Android support initialized");
35+
info!("SDK Android support initialized");
3536
Ok(())
3637
}
3738

38-
if let Err(e) = init_inner() {
39-
log::error!("Failed to initialize Android support: {:#?}", e);
39+
if let Err(error) = init_inner() {
40+
error!(%error, "Failed to initialize Android support");
4041
}
4142
}
4243

crates/bitwarden-uniffi/src/lib.rs

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
uniffi::setup_scaffolding!();
44

5-
use std::sync::Arc;
5+
use std::sync::{Arc, Once};
66

77
use auth::AuthClient;
88
use bitwarden_core::{ClientSettings, client::internal::ClientManagedTokens};
@@ -111,22 +111,65 @@ impl Client {
111111
}
112112
}
113113

114+
static INIT: Once = Once::new();
115+
114116
fn init_logger() {
115-
#[cfg(not(any(target_os = "android", target_os = "ios")))]
116-
let _ = env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info"))
117-
.try_init();
118-
119-
#[cfg(target_os = "ios")]
120-
let _ = oslog::OsLogger::new("com.8bit.bitwarden")
121-
.level_filter(log::LevelFilter::Info)
122-
.init();
123-
124-
#[cfg(target_os = "android")]
125-
android_logger::init_once(
126-
android_logger::Config::default()
127-
.with_tag("com.bitwarden.sdk")
128-
.with_max_level(log::LevelFilter::Info),
129-
);
117+
use tracing_subscriber::{EnvFilter, layer::SubscriberExt as _, util::SubscriberInitExt as _};
118+
119+
INIT.call_once(|| {
120+
// the log level prioritization is determined by:
121+
// 1. if RUST_LOG is detected at runtime
122+
// 2. if RUST_LOG is provided at compile time
123+
// 3. default to INFO
124+
let filter = EnvFilter::builder()
125+
.with_default_directive(
126+
option_env!("RUST_LOG")
127+
.unwrap_or("info")
128+
.parse()
129+
.expect("should provide valid log level at compile time."),
130+
)
131+
.from_env_lossy();
132+
133+
let fmtlayer = tracing_subscriber::fmt::layer()
134+
.with_ansi(true)
135+
.with_file(true)
136+
.with_line_number(true)
137+
.with_target(true)
138+
.pretty();
139+
140+
#[cfg(target_os = "ios")]
141+
{
142+
const TAG: &str = "com.8bit.bitwarden";
143+
144+
tracing_subscriber::registry()
145+
.with(fmtlayer)
146+
.with(filter)
147+
.with(tracing_oslog::OsLogger::new(TAG, "default"))
148+
.init();
149+
}
150+
151+
#[cfg(target_os = "android")]
152+
{
153+
const TAG: &str = "com.bitwarden.sdk";
154+
155+
tracing_subscriber::registry()
156+
.with(fmtlayer)
157+
.with(filter)
158+
.with(
159+
tracing_android::layer(TAG)
160+
.expect("initialization of android logcat tracing layer"),
161+
)
162+
.init();
163+
}
164+
165+
#[cfg(not(any(target_os = "android", target_os = "ios")))]
166+
{
167+
tracing_subscriber::registry()
168+
.with(fmtlayer)
169+
.with(filter)
170+
.init();
171+
}
172+
});
130173
}
131174

132175
/// Setup the error converter to ensure conversion errors don't cause panics

0 commit comments

Comments
 (0)