专业编程基础技术教程

网站首页 > 基础教程 正文

nodejs 学习-7 模块 Console Debug

ccvgpt 2024-10-16 08:28:56 基础教程 34 ℃

Node.js 是一个开源和跨平台的 JavaScript 运行时环境 ,之前有了解但并不精通, 本系列将重新来学习Node.js 并将相对要点做记录和整理。

Console

Console 提供简单的控制台输出, 类似于浏览器中提供的控制台控制。

nodejs 学习-7 模块 Console Debug

使用Console例如:

     public consoleTest(){
 
         const out = createWriteStream('console.out.txt');
         const err = createWriteStream('console.err.txt');
         const myConsole = new console.Console(out, err);
 
         myConsole.log('hello world');
         myConsole.log('hello %s', 'world');
         myConsole.error(new Error('Whoops, something bad happened'));
         myConsole.table([{ a: 1, b: 'Y' }, { a: 'Z', b: 2 }]);
         const name = 'AAA';
         myConsole.warn(`Danger ${name}! Danger!`);
 
     }

可以看到对应的输出已经输出到对应的文件中。

console.out.txt

 hello world
 hello world
 ┌─────────┬─────┬─────┐
 │ (index) │  a  │  b  │
 ├─────────┼─────┼─────┤
 │    0    │  1  │ 'Y' │
 │    1    │ 'Z' │  2  │
 └─────────┴─────┴─────┘
 

全局的console其实是特殊的Console, 他把输出发送到process.stdout和process.stderr.

 new Console({ stdout: process.stdout, stderr: process.stderr }); 

较为常用的方法包括

  • console.assert(value[,...message]) : 如果value是false,则写入消息。
  • console.clear(): 当stdout是终端时, 清除终端。
  • console.count([label]):维护内部的lable计数器。
  • console.countReset([label]):重置计数器
  • console.debug(data[,...arg]):console.log的别名
  • console.group([...label]): 将后续行的缩进增加 groupIndentation 长度的空格。
  • console.groupEnd():将后续行的缩进减少 groupIndentation 长度的空格。
  • console.log():stdout输出
  • console.table(tabularData[,properties]):表格输出
  • console.time([label]):启动计算持续时间计时器, label标识
  • console.timeEnd([label]):停止之前的time(),并将计时结果输出
  • console.timeLog([label]):对于先前通过调用 console.time() 启动的计时器,将经过时间和其他 data 参数打印到 stdout

Debug

类似Console的增强版本, 和console.log 不同的是我们不需要再代码中注释掉日志, 默认情况下处于关闭状态, 并可以通过NODE_DEBUG环境变量有条件打开。

 import { debug } from "util";
         
 let d= debug("fo");
 
 d("tessssssssst")

当环境变量开启:NODE_DEBUG=fo 后, 则对应的log被打开。

util.debuglog(section[, callback])

  • section 标识正在为其创建 debuglog 函数的应用部分的字符串。
  • callback 第一次调用日志函数时调用的回调函数参数是更优化的日志函数。

Debug 包

工程中, 可以将log相关通过debug包进行使用和管理。

npm install debug -s 的方式引入debug包

 import {debug} from 'debug';
 
 const APP_NAME = 'test-app';
 
 export class LogService {
   private pDebug: any;
   private pInfo: any;
   private pWarn: any;
   private pError: any;
 
   constructor(
   ) {
     this.pDebug = debug(`${APP_NAME}:DEBUG`);
     this.pWarn = debug(`${APP_NAME}:WARN`);
     this.pInfo = debug(`${APP_NAME}:INFO`);
 
     this.pError = debug(`${APP_NAME}:ERROR`);
 
     this.pDebug.log = console.info.bind(console);
     this.pInfo.log = console.info.bind(console);
     this.pWarn.log = console.warn.bind(console);
     this.pError.log = console.error.bind(console);
   }
 
   get debug() {
     return this.pDebug;
   }
 
   get info() {
     return this.pInfo;
   }
   get warn() {
     return this.pWarn;
   }
 
   get error() {
     return this.pError;
   }
 
 }
 

使用方法:

 const log = new LogService();
        log.debug("abcde")



Tags:

最近发表
标签列表