{"id":26365405,"url":"https://github.com/2moe/clg","last_synced_at":"2026-02-25T18:32:50.147Z","repository":{"id":243458279,"uuid":"812491689","full_name":"2moe/clg","owner":"2moe","description":"A logger specifically designed for wasm32-unknown-unknown.","archived":false,"fork":false,"pushed_at":"2025-03-14T05:34:18.000Z","size":27,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-14T06:27:41.397Z","etag":null,"topics":["logger","rust","wasm","wasm-logger"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/2moe.png","metadata":{"files":{"readme":"docs/Readme-zh.md","changelog":null,"contributing":null,"funding":null,"license":"License","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-09T03:39:03.000Z","updated_at":"2025-03-14T05:50:27.000Z","dependencies_parsed_at":"2024-06-09T04:39:02.197Z","dependency_job_id":"b1da61bf-7bc5-4233-9635-0c5221bbc4a1","html_url":"https://github.com/2moe/clg","commit_stats":null,"previous_names":["2moe/clg"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/2moe%2Fclg","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/2moe%2Fclg/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/2moe%2Fclg/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/2moe%2Fclg/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/2moe","download_url":"https://codeload.github.com/2moe/clg/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243920737,"owners_count":20369097,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["logger","rust","wasm","wasm-logger"],"created_at":"2025-03-16T19:36:22.606Z","updated_at":"2026-02-25T18:32:50.142Z","avatar_url":"https://github.com/2moe.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# clg\n\n专为 wasm32-unknown-unknown 打造的 logger (日志记录器)。\n\n[![crates.io](\u003chttps://img.shields.io/crates/v/clg.svg?label=lib(clg)\u003e)](https://crates.io/crates/clg)\n\n[![Documentation](https://img.shields.io/docsrs/clg?label=docs.rs)](https://docs.rs/clg)    [![Apache-2 licensed](https://img.shields.io/crates/l/clg)](../License)\n\n| Language/語言          | ID         |\n| ---------------------- | ---------- |\n| 中文                   | zh-Hans-CN |\n| [English](./Readme.md) | en-Latn-US |\n\n## Q\u0026A\n\n### Q: 这玩意儿有什么用？\n\nA: 用 rust 将日志输出到 Web/Node.js/Deno 的控制台。\n\nrust:\n\n```rust\n  #[wasm_bindgen(js_name = __clgTestLogger)]\n  pub fn test_logger() {\n    use log::*;\n    trace!(\"Trace\");\n    debug!(\"DBG\");\n    info!(\"information\");\n    warn!(\"warning\");\n    error!(\"panic\");\n  }\n```\n\n![rust-code](https://github.com/2moe/clg/assets/25324935/49c23c65-e9de-4cb0-aa57-7a3e51076778)\n\njs:\n\n```js\nconst wasm = require(\"/path/to/your_wasm_glue.js\")\n\nconst logLevel = wasm._clgNewLogLevel(\"debug\")\n\nconst _initLogger = new wasm._clgConsoleLogger(logLevel)\n\nwasm.__clgTestLogger()\n```\n\n![js-glue](https://github.com/2moe/clg/assets/25324935/7873a1cc-9764-48b6-861d-b8f9d03693d0)\n\n### Q: 为什么不直接用标准库？\n\nA: 对于 **wasm32-wasi**，我们可以直接使用 rust std 的 `eprintln`，以及常规的 logger。\n\n然而，对于 **wasm32-unknown-unknown**，我们得要调用 web 控制台，也就是调用 js 的 `console.log()` 和 `console.error()` 等函数。\n\n### Q: 要怎么用？\n\nA:\n\n- 如果已经配置了 logger, 那就用 log 宏：\n  - 例如 `warn!(\"{warn_1} {warn_2}\")`、 `debug!(\"{dbg_1} {dbg_2}\")`\n  - 调用 `log::debug!(\"这是一条调试消息\")` 后，输出的内容类似于 `06:23:16:223 [DEBUG] clg::test_wasm:186 这是一条调试消息`\n\n\u003e log 宏:\n\u003e\n\u003e - `log::error!()`\n\u003e - `log::warn!()`\n\u003e - `log::info!()`\n\u003e - `log::debug!()`\n\u003e - `log::trace!()`\n\n- 如果没有配置 logger, 那就用 clg 宏：\n  - 例如 `clg!(\"{message_1}\")`\n  - 调用 `clg` 宏时，不会包含额外的日志信息，也不会自动判断 log-level (日志输出级别) 。\n  - 由于部分浏览器不支持 `console.debug()` ，因此没有 `c_dbg!()`\n\n在 rust 中调用 `c_err!(\"{msg1} {msg2}\")`, 相当于在 js 中调用:\n\n```js\nconsole.error(`${msg1} ${msg2}`)\n```\n\n\u003e clg 宏：\n\u003e\n\u003e - `clg::clg!()` =\u003e `console.log()`\n\u003e - `clg::c_err!()` =\u003e `console.error()`\n\u003e - `clg::c_warn!()` =\u003e `console.warn()`\n\u003e - `clg::c_info!()` =\u003e `console.info()`\n\u003e - `clg::c_trace!()` =\u003e  `console.trace()`\n\n更多使用说明，详见下文。\n\n## Usage\n\n### Step1. 安装 wasm-pack\n\n```sh\ncargo install wasm-pack\n```\n\n### Step2. 添加依赖\n\n**Cargo.toml**:\n\n```toml\n[lib]\ncrate-type = [\"cdylib\"]\n\n[dependencies]\nwasm-bindgen = \"0.2\"\nlog = { version = \"0.4\", features = [\"std\"] }\nclg = \"0.0.5\"\n```\n\n### Step3. 配置 Logger\n\n先来介绍一下什么是 log level。\n\n从低到高分别为\n\n- Off    (无 Log)\n- Error  (错误)\n- Warn   (警告)\n- Info   (信息)\n- Debug  (调试)\n- Trace  (追踪)\n\n级别越高，打印的内容越详细。\n\n低级别的 level 无法显示高级别 level 的消息。\n\n假设当前 level 为 warn，且存在如下代码：\n\n```rust\nuse log::{debug, error, warn};\nlet id = \"div_2\";\ndebug!(\"正在获取 DOM 元素, id: {id}\");\nlet Some(element) = document.get_element_by_id(id) else {\n    warn!(\"无法获取元素\");\n    error!(\"出错惹\");\n    return;\n};\n```\n\n假设 debug = 3, warn = 2, error = 1\n\n由于 debug(3) \u003e warn(2) \u003e error(1)\n\n当 level = warn(2) 时，只能显示 warn(2) 和 error(1)，不会显示 debug(3) 日志。\n\n也就是说，上面的代码不会输出 \"正在获取 DOM 元素...\"。\n\n#### 在 rust 中配置\n\n您既可以在 rust 中配置 Log Level，亦可在 js 中配置。\n\n**src/lib.rs**:\n\n```rust\nuse clg::{log_level::LogLevel, ConsoleLogger};\nuse wasm_bindgen::prelude::wasm_bindgen;\n\n#[wasm_bindgen(js_name = initLogger)]\npub fn init_logger() {\n    ConsoleLogger::init(Some(LogLevel::Debug));\n}\n```\n\n编译与打包:\n\n```sh\n# 您可以将 nodejs 改成 deno，最后用 deno 来执行\nwasm-pack build --release --target nodejs --out-dir pkg --out-name wasm\n```\n\n**js/index.cjs**:\n\n```js\n// index.mjs: import * as wasm from \"../pkg/wasm.js\";\nconst wasm = require(\"../pkg/wasm.js\");\n\nconst _init = wasm.initLogger();\nwasm.__clgTestLogger();\n```\n\n最后，使用 nodejs 运行 index.cjs:\n\n```sh\nnode js/index.cjs\n```\n\n#### 在 js 中配置\n\n\u003e 注：logger 是全局的。每个线程只能运行一个 logger，不要多次初始化 logger。\n\u003e 一旦 logger 初始化完成，在其他地方 (rust 的函数内) 就能直接用 `log` 宏了。\n\n**src/lib.rs**:\n\n```rust\n#[allow(unused_imports)]\nuse clg::ConsoleLogger as _;\n```\n\n**js/index.cjs**:\n\n```js\nconst wasm = require(\"../pkg/wasm.js\");\n\n// const lv = wasm._clgLogLevel.Warn;\nconst lv = wasm._clgNewLogLevel(\"debug\");\nconst _init = new wasm._clgConsoleLogger(lv);\n\nwasm.__clgTestLogger();\n```\n\n运行:\n\n```sh\nnode js/index.cjs\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F2moe%2Fclg","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F2moe%2Fclg","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F2moe%2Fclg/lists"}