A Guide to Node.js Logging
发布日期:2021-05-07 18:04:44 浏览次数:7 分类:原创文章

本文共 1342 字,大约阅读时间需要 4 分钟。

A Guide to Node.js Logging

difference between console.log and console.error in nodejs?
process streams stdin, stdout, stderr
coggle.it-by-lineuman

通俗的理解就是标准输入,标准输出,标准错误输出其实是不同的文件描述符,最后会放到不同的地方

DEMO

index.js如下

console.log('Hello there');console.error('Bye bye');
node index.js > hello.log 2> error.log

When Do You Want to 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

Your Library Logs

如何开启其他库的日志

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

Your CLI Output

https://www.twilio.com/blog/how-to-build-a-cli-with-node-js

In Summary

我们学习日志的过程
第一步,使用console.log()
第二部,我们想着去优化日志问题。
推荐的方法是看看那些开源的node项目是怎么解决日志问题的

上一篇:使用heroku托管服务搭建网站
下一篇:nodejs中使用mongoDB

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2025年04月08日 23时59分25秒