Crates.io | console_log |
lib.rs | console_log |
version | 1.0.0 |
source | src |
created_at | 2019-01-01 05:10:54.595689 |
updated_at | 2023-03-10 04:23:52.901965 |
description | A logging facility that routes Rust log messages to the browser's console. |
homepage | |
repository | https://github.com/iamcodemaker/console_log |
max_upload_size | |
id | 104796 |
size | 44,081 |
A logger that routes messages to the browser's console.
use log::Level;
fn main() {
console_log::init_with_level(Level::Debug);
info!("It works!");
// ...
}
Rust's log levels map to the browser's console log in the following way.
Rust | Web Console |
---|---|
trace!() |
console.debug() |
debug!() |
console.log() |
info!() |
console.info() |
warn!() |
console.warn() |
error!() |
console.error() |
The "color"
feature adds styling to the log messages.
Cargo.toml
console_log = { version = "1", features = ["color"] }
The styled log messages will be rendered as follows:
Twiggy reports this library adding about 180Kb to the size of a minimal wasm binary in a debug build. If you want to avoid this, mark the library as optional and conditionally initialize it in your code for non-release builds.
Cargo.toml
[dependencies]
cfg-if = "0.1"
log = "0.4"
console_log = { version = "1", optional = true }
[features]
default = ["console_log"]
lib.rs
use wasm_bindgen::prelude::*;
use cfg_if::cfg_if;
cfg_if! {
if #[cfg(feature = "console_log")] {
fn init_log() {
use log::Level;
console_log::init_with_level(Level::Trace).expect("error initializing log");
}
} else {
fn init_log() {}
}
}
#[wasm_bindgen]
pub fn main() {
init_log();
// ...
}
The file and line number information associated with the log messages reports
locations from the shims generated by wasm-bindgen
, not the location of the
logger call.
This project is licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
wasm-logger
wasm-bindgen-console-logger
fern
(use with console_log::log
)