本文共 1342 字,大约阅读时间需要 4 分钟。
difference between console.log and console.error in nodejs?
process streams
stdin
, stdout
, stderr
通俗的理解就是标准输入,标准输出,标准错误输出其实是不同的文件描述符,最后会放到不同的地方
index.js如下
console.log('Hello there');console.error('Bye bye');
node index.js > hello.log 2> error.log
We are using console.log(’%O’, req) here to log the entire object. console.log uses util.format under the hood that supports additionally to %O other placeholders.
there’s often additional information we need
- Timestamp - to know when things happened
- Computer/Server Name - in case you are running a distributed system
- Process ID - in case you are running multiple Node processes using something like pm2
-Message - an actual message with some content- Stack Trace - in case we are logging an error
Maybe some additional variables/info
如何开启其他库的日志
DEBUG=express:* node index.js
那么这个功能是怎么实现的呢?
This is done through a package fittingly called debug. It allows us to write messages under a “namespace” and if the user of the library includes the namespace or a wildcard that matches it in their DEBUG environment variable, it will output these.
npm install debug
https://www.twilio.com/blog/how-to-build-a-cli-with-node-js
我们学习日志的过程
第一步,使用console.log()
第二部,我们想着去优化日志问题。
推荐的方法是看看那些开源的node项目是怎么解决日志问题的
转载地址:http://wtjr.baihongyu.com/