Skip to content

Commit 4921d8a

Browse files
authored
Replace log with tracing in WASM crate (#550)
## 🎟️ Tracking <!-- Paste the link to the Jira or GitHub issue or otherwise describe / point to where this change is coming from. --> ## 📔 Objective Cascading the standardization of `tracing` crate from clients repo into the SDK, that was started per bitwarden/clients#16321 , this change updates the WASM crate to use `tracing`. ## 📸 Screenshots <img width="918" height="157" alt="Screenshot 2025-11-19 at 09 32 04" src="https://github.com/user-attachments/assets/d84d9563-3dbc-49a1-bc5e-8caec6988cb1" /> ## ⏰ 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 55c14dd commit 4921d8a

File tree

4 files changed

+94
-32
lines changed

4 files changed

+94
-32
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ subtle = ">=2.5.0, <3.0"
7777
syn = ">=2.0.87, <3"
7878
thiserror = ">=1.0.40, <3"
7979
tokio = { version = "1.36.0", features = ["macros"] }
80+
tracing = { version = "0.1.41" }
81+
tracing-subscriber = { version = "0.3.20", features = [
82+
"fmt",
83+
"env-filter",
84+
"tracing-log",
85+
] }
8086
tsify = { version = ">=0.5.5, <0.6", features = [
8187
"js",
8288
], default-features = false }

crates/bitwarden-wasm-internal/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ bitwarden-state = { workspace = true, features = ["wasm"] }
3333
bitwarden-threading = { workspace = true }
3434
bitwarden-vault = { workspace = true, features = ["wasm"] }
3535
console_error_panic_hook = "0.1.7"
36-
console_log = { version = "1.0.0", features = ["color"] }
37-
log = "0.4.20"
3836
serde = { workspace = true }
37+
tracing = { workspace = true }
38+
tracing-subscriber = { workspace = true }
39+
tracing-web = { version = "0.1.3" }
3940
tsify = { workspace = true }
4041
wasm-bindgen = { workspace = true }
4142
wasm-bindgen-futures = { workspace = true }
Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use log::{Level, set_max_level};
1+
use tracing::Level;
2+
use tracing_subscriber::{
3+
EnvFilter, fmt::format::Pretty, layer::SubscriberExt as _, util::SubscriberInitExt as _,
4+
};
5+
use tracing_web::{MakeWebConsoleWriter, performance_layer};
26
use wasm_bindgen::prelude::*;
37

48
#[wasm_bindgen]
@@ -12,26 +16,35 @@ pub enum LogLevel {
1216

1317
fn convert_level(level: LogLevel) -> Level {
1418
match level {
15-
LogLevel::Trace => Level::Trace,
16-
LogLevel::Debug => Level::Debug,
17-
LogLevel::Info => Level::Info,
18-
LogLevel::Warn => Level::Warn,
19-
LogLevel::Error => Level::Error,
19+
LogLevel::Trace => Level::TRACE,
20+
LogLevel::Debug => Level::DEBUG,
21+
LogLevel::Info => Level::INFO,
22+
LogLevel::Warn => Level::WARN,
23+
LogLevel::Error => Level::ERROR,
2024
}
2125
}
2226

23-
#[wasm_bindgen]
24-
pub fn set_log_level(level: LogLevel) {
25-
let log_level = convert_level(level);
26-
set_max_level(log_level.to_level_filter());
27-
}
28-
2927
#[allow(missing_docs)]
3028
#[wasm_bindgen]
3129
pub fn init_sdk(log_level: Option<LogLevel>) {
3230
console_error_panic_hook::set_once();
31+
3332
let log_level = convert_level(log_level.unwrap_or(LogLevel::Info));
34-
if let Err(_e) = console_log::init_with_level(log_level) {
35-
set_max_level(log_level.to_level_filter())
36-
}
33+
34+
let filter = EnvFilter::builder()
35+
.with_default_directive(log_level.into())
36+
.from_env_lossy();
37+
38+
let fmt = tracing_subscriber::fmt::layer()
39+
.with_ansi(false) // only partially supported across browsers
40+
.without_time() // time is not supported in wasm
41+
.with_writer(MakeWebConsoleWriter::new()); // write events to the console
42+
43+
let perf_layer = performance_layer().with_details_from_fields(Pretty::default());
44+
45+
tracing_subscriber::registry()
46+
.with(perf_layer)
47+
.with(filter)
48+
.with(fmt)
49+
.init();
3750
}

0 commit comments

Comments
 (0)