| Crates.io | browser_log |
| lib.rs | browser_log |
| version | 0.4.0 |
| created_at | 2024-09-08 19:27:12.705232+00 |
| updated_at | 2025-08-09 18:34:56.02883+00 |
| description | Advanced logging and panic handling for WebAssembly applications with browser console integration |
| homepage | https://github.com/Wandalen/cgtools/tree/master/module/helper/browser_log |
| repository | https://github.com/Wandalen/cgtools |
| max_upload_size | |
| id | 1368478 |
| size | 51,222 |
Advanced logging and panic handling for WebAssembly applications
A specialized logging utility designed for Rust WebAssembly applications running in browsers and Node.js environments. Seamlessly integrates with JavaScript's console API while providing enhanced debugging capabilities and panic handling for WASM applications.
Add to your Cargo.toml:
browser_log = { workspace = true }
use browser_log::*;
// Initialize logging (call once at startup)
fn init_logging() {
// Setup panic handler
panic::setup(panic::Config::default());
// Initialize logger with default settings
log::setup::setup(log::setup::Config::default());
}
// Use standard Rust logging macros
fn example_logging() {
::log::info!("Application started");
::log::debug!("Debug information: {}", 42);
::log::warn!("Warning message");
::log::error!("Error occurred: {}", "connection failed");
}
use browser_log::*;
use browser_log::log::console;
fn setup_advanced_logging() {
// Custom panic handler configuration
let config = panic::Config {
with_location: true,
with_stack_trace: true,
};
panic::setup(config);
// Configure logger with specific level
log::setup::setup(log::setup::Config::new(::log::Level::Debug));
// Performance timing (basic console timing)
console::time();
// ... perform operation
console::time_end();
}
| Function | Purpose | Example |
|---|---|---|
log::setup::setup() |
Initialize logger | browser_log::log::setup::setup(Default::default()) |
panic::setup() |
Setup panic handling | browser_log::panic::setup(Default::default()) |
console::log_1() |
Direct console output | console::log_1(&JsValue::from_str("message")) |
console::time() |
Performance timing | console::time() |
// Standard Rust logging levels work seamlessly
log::trace!("Detailed tracing information");
log::debug!("Development debugging info");
log::info!("General information");
log::warn!("Warning conditions");
log::error!("Error conditions");
use browser_log::log::console;
use wasm_bindgen::JsValue;
// Direct console methods (using web-sys console API)
console::log_1(&JsValue::from_str("Basic log message"));
console::info_1(&JsValue::from_str("Information message"));
console::warn_1(&JsValue::from_str("Warning message"));
console::error_1(&JsValue::from_str("Error message"));
// Performance timing
console::time();
// ... perform database query
console::time_end();
// Grouped logging
console::group_1(&JsValue::from_str("User Actions"));
console::log_1(&JsValue::from_str("User clicked button"));
console::log_1(&JsValue::from_str("Form submitted"));
console::group_end();
use browser_log::*;
use std::panic;
// Custom panic handler with user notification
panic::set_hook(Box::new(|panic_info| {
let message = match panic_info.payload().downcast_ref::<&str>() {
Some(s) => *s,
None => "Unknown panic occurred",
};
// Use the browser_log panic handler
browser_log::panic::hook(panic_info, &browser_log::panic::Config::default());
// Show user-friendly error message (requires Window feature)
// web_sys::window().unwrap().alert_with_message(&format!("An error occurred: {}", message));
}));
// Only log in debug builds
#[cfg(debug_assertions)]
fn debug_log(message: &str) {
::log::debug!("{}", message);
}
// Log with context information
fn log_with_context(operation: &str, data: &impl std::fmt::Debug) {
::log::info!("[{}] Data: {:?}", operation, data);
}
use browser_log::log::console;
struct ProfileScope {
_name: String,
}
impl ProfileScope {
fn new(name: &str) -> Self {
console::time();
Self { _name: name.to_string() }
}
}
impl Drop for ProfileScope {
fn drop(&mut self) {
console::time_end();
}
}
// Usage
fn expensive_operation() {
let _profile = ProfileScope::new("expensive_operation");
// ... perform work
} // Automatically logs timing when dropped
[dependencies]
browser_log = { workspace = true }
console_error_panic_hook = "0.1"
The logger integrates seamlessly with popular Rust web frameworks like Yew, Seed, and others that compile to WebAssembly.
wasm32-unknown-unknown target