Debug 项目技术文档
2024-12-24 03:40:09作者:胡唯隽
1. 安装指南
使用 npm 安装
要安装 debug 模块,请在终端中运行以下命令:
npm install debug
在浏览器中使用
如果你需要在浏览器中使用 debug,可以通过 browserify 构建一个浏览器兼容的脚本,或者使用 browserify-as-a-service 提供的构建服务。
2. 项目的使用说明
基本用法
debug 模块提供了一个简单的调试工具,类似于 Node.js 核心的调试技术。它可以在 Node.js 和浏览器中使用。
示例代码
以下是一个简单的 Node.js 示例:
var debug = require('debug')('http')
, http = require('http')
, name = 'My App';
// 模拟应用
debug('booting %o', name);
http.createServer(function(req, res){
debug(req.method + ' ' + req.url);
res.end('hello\n');
}).listen(3000, function(){
debug('listening');
});
// 模拟工作线程
require('./worker');
在 worker.js 中:
var a = require('debug')('worker:a')
, b = require('debug')('worker:b');
function work() {
a('doing lots of uninteresting work');
setTimeout(work, Math.random() * 1000);
}
work();
function workb() {
b('doing some work');
setTimeout(workb, Math.random() * 2000);
}
workb();
环境变量配置
通过设置 DEBUG 环境变量,可以控制哪些调试信息会被输出。例如:
DEBUG=*:启用所有调试信息。DEBUG=worker:*:仅启用worker命名空间下的调试信息。DEBUG=*,-worker:*:启用所有调试信息,但排除worker命名空间。
Windows 命令提示符设置
在 CMD 中:
set DEBUG=* & node app.js
在 PowerShell 中:
$env:DEBUG='*'; node app.js
3. 项目API使用文档
主要API
debug(namespace)
创建一个调试实例,namespace 是调试的命名空间,通常是模块的名称。
const debug = require('debug')('myModule');
debug('This is a debug message');
debug.extend(namespace)
扩展调试实例的命名空间。
const log = require('debug')('auth');
const logSign = log.extend('sign');
log('hello'); // auth hello
logSign('hello'); // auth:sign hello
格式化输出
debug 支持 printf 风格的格式化输出:
%O:多行打印对象。%o:单行打印对象。%s:字符串。%d:数字(整数和浮点数)。%j:JSON 格式。%%:百分号。
示例:
debug('User object: %O', user);
自定义格式化器
你可以通过扩展 debug.formatters 对象来添加自定义格式化器。例如:
const createDebug = require('debug');
createDebug.formatters.h = (v) => {
return v.toString('hex');
};
const debug = createDebug('foo');
debug('this is hex: %h', new Buffer('hello world'));
4. 项目安装方式
通过 npm 安装
npm install debug
在浏览器中使用
通过 browserify 构建浏览器兼容的脚本,或者使用 browserify-as-a-service 提供的构建服务。
环境变量配置
通过设置 DEBUG 环境变量来控制调试信息的输出。例如:
DEBUG=* node app.js
在浏览器中,可以通过 localStorage 来设置:
localStorage.debug = 'worker:*';
5. 其他功能
命名空间颜色
每个调试实例都有一个基于命名空间名称生成的颜色,这有助于在视觉上区分不同的调试输出。
毫秒差
在开发过程中,显示两次 debug() 调用之间的时间差非常有用。debug 会自动显示这个时间差。
输出流
默认情况下,debug 会将日志输出到 stderr,但你可以通过覆盖 log 方法来更改输出流。例如:
const debug = require('debug');
const log = debug('app:log');
log.log = console.log.bind(console);
log('goes to stdout');
通过这篇文档,你应该能够顺利安装和使用 debug 模块,并了解其主要功能和 API。
登录后查看全文
热门项目推荐
相关项目推荐
atomcodeClaude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed. Get StartedRust0218
cann-learning-hubCANN 学习中心仓,支持在线互动运行、边学边练,提供教程、示例与优化方案,一站式助力昇腾开发者快速上手。Jupyter Notebook0139
uni-appA cross-platform framework using Vue.jsJavaScript09
GLM-5.2智谱开源 GLM-5.2,这是针对长文本任务的最新旗舰模型。相较于前代产品 GLM-5.1,它在长文本任务处理能力上实现了显著飞跃,并且首次在稳定的 100 万 token 上下文中提供这一能力。Jinja00
SwanLab⚡️SwanLab - an open-source, modern-design AI training tracking and visualization tool. Supports Cloud / Self-hosted use. Integrated with PyTorch / Transformers / LLaMA Factory / veRL/ Swift / Ultralytics / MMEngine / Keras etc.Python00
tiny-universe《大模型白盒子构建指南》:一个全手搓的Tiny-UniverseJupyter Notebook03
项目优选
收起
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
471
465
deepin linux kernel
C
32
16
Claude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed.
Get Started
Rust
2.09 K
218
本项目是CANN提供的神经网络类计算算子库,实现网络在NPU上加速计算。
C++
700
1.4 K
暂无描述
Dockerfile
780
5.08 K
Ascend Extension for PyTorch
Python
758
968
本仓库是 Flutter SDK 与 Flutter Engine 的 OpenHarmony 适配版本,由 CPF-Flutter 团队维护。开发者可使用熟悉的 Flutter 技术栈开发 OpenHarmony 应用,3.35.7 及以后的适配版本可基于本仓库源码构建支持 OpenHarmony 的 Flutter Engine。
Dart
1.04 K
271
本项目是CANN提供的transformer类大模型算子库,实现网络在NPU上加速计算。
C++
880
2.03 K
MindQuantum is a general software library supporting the development of applications for quantum computation.
Python
183
111
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
1.11 K
682