[][src]Crate console_log

A logger that logs to the browser's console.


use log::Level;
use log::info;
fn main() {

    info!("It works!");

    // ...

Log Levels

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()

Getting Fancy

The feature set provided by this crate is intentionally very basic. If you need more flexible formatting of log messages (timestamps, file and line info, etc.) this crate can be used with the fern logger via the console_log::log function.

Code Size

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.


cfg_if = "0.1"
log = "0.4"
console_log = { version = "0.1", optional = true }
default = ["console_log"]


This example is not tested
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() {}
pub fn main() {
    // ...


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.



Initializes the global logger with max_log_level set to Level::Info (a sensible default).


Initializes the global logger setting max_log_level to the given value.


Print a log::Record to the browser's console at the appropriate level.