Coder Social home page Coder Social logo

console_log's Introduction

console_log Crates.io

A logger that routes messages to the browser's console.

Usage

use log::Level;
fn main() {
    console_log::init_with_level(Level::Debug);

    info!("It works!");

    // ...
}

Details

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

Colors

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:

Styled log messages

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.

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();
    // ...
}

Limitations

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.

License

This project is licensed under either of

at your option.

Contributing

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.

See Also

console_log's People

Contributors

azriel91 avatar chidea avatar dependabot-preview[bot] avatar escobar-west avatar iamcodemaker avatar neurosie avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

console_log's Issues

wasm-logger

Hey, this seems to provide exactly the same functionality as https://crates.io/crates/wasm-logger.

Did you not know about that crate or is there something that's handled differently in console_log?

I always try to advocate for lack of duplication where possible.

[Feature Request]: Log Filters

The logger should contain support for filters much like how pretty_env_logger can filter with the RUST_LOG environment variable or how android_logger has a with_filter method

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.