clg
A logger specifically designed for wasm32-unknown-unknown.
| Languages/語言 | ID |
|---|---|
| English | en-Latn-US |
| 中文 | zh-Hans-CN |
Q&A
Q: What is this used for?
A: Output logs to the Web/Node.js/Deno console in Rust.
Q: Why not use the standard library directly?
A: For wasm32-wasi, we can directly use Rust std's eprintln, as well as the regular logger.
However, for wasm32-unknown-unknown, we need to call the web console, that is, call js's console.log() and console.error() functions.
Q: How to use it?
A:
- If a logger has already been configured, use the log macro:
- For example,
warn!("{warn_1} {warn_2}"),debug!("{dbg_1} {dbg_2}") - After calling
log::debug!("This is a debug message"), the output content is similar to06:23:16:223 [DEBUG] clg::test_wasm:186 This is a debug message
- For example,
log macro:
log::error!()log::warn!()log::info!()log::debug!()log::trace!()
- If a logger is not configured, use the
clgmacro:- For example,
clg!("{message_1}") - When calling the
clgmacro, it will not include additional log information, nor will it automatically determine the log-level. - Since some browsers do not support
console.debug(), there is noc_dbg!()
- For example,
Calling c_err!("{msg1} {msg2}") in Rust is equivalent to calling the following in JavaScript:
console.error
clg macro:
clg::clg!()=>console.log()clg::c_err!()=>console.error()clg::c_warn!()=>console.warn()clg::c_info!()=>console.info()clg::c_trace!()=>console.trace()
For more usage instructions, see below.
Usage
Step1. Install wasm-pack
Step2. Add Deps
Cargo.toml:
[]
= ["cdylib"]
[]
= "0.2.92"
= { = "0.4.21", = ["std"] }
= "0.0.0"
# js-sys = "0.3.69"
# web-sys = "0.3.69"
Step3. Configure Logger
Let's first introduce what a log level is.
From low to high, they are:
- Off (No Log)
- Error
- Warn (Warning)
- Info (Information)
- Debug
- Trace
The higher the level, the more detailed the printed content.
A lower level cannot display messages of a higher level.
Assume the current level is warn, and the following code exists:
use ;
let id = "div_2";
debug!;
let Some = document.get_element_by_id else ;
Assume debug = 3, warn = 2, error = 1
Since debug(3) > warn(2) > error(1)
When level = warn(2), only warn(2) and error(1) can be displayed, debug(3) logs will not be displayed.
That is to say, the above code will not output "Getting DOM element...".
Configuring in Rust
You can configure the Log Level in Rust, as well as in js.
src/lib.rs:
use ;
use wasm_bindgen;
Compile and package:
# You can change nodejs to deno, and finally use deno to execute wasm.js
js/index.cjs:
// index.mjs: import * as wasm from "../pkg/wasm.js";
const wasm = require;
const _init = wasm.;
wasm.;
Finally, run index.cjs with nodejs:
Configuring in JS
Note: The logger is global. Each thread can only run one logger, do not initialize the logger multiple times. Once the logger is initialized, you can directly use the
logmacro elsewhere (inside Rust functions).
src/lib.rs:
use ConsoleLogger as _;
js/index.cjs:
const wasm = require;
// const lv = wasm._clg_LogLevel.Warn;
const lv = wasm.;
const _init = ;
wasm.;
Run: