博客
关于我
A Guide to Node.js Logging
阅读量:341 次
发布时间:2019-03-04

本文共 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项目是怎么解决日志问题的

转载地址:http://wtjr.baihongyu.com/

你可能感兴趣的文章
chrome浏览器功能介绍
查看>>
linux shell 读取文件脚本
查看>>
git命令升级版用法
查看>>
sed常用命令
查看>>
linux下各种小命令
查看>>
checksec未完待续~
查看>>
python pexpect
查看>>
inode索引节点的概念
查看>>
python时间格式转换time模块
查看>>
文件校验
查看>>
python can i use return in wiht statement?
查看>>
coddenomicon工具
查看>>
create-react-app第一步
查看>>
testng测试工具简介
查看>>
mysql查看数据库状态
查看>>
怎么去利用已有的数据做分析?
查看>>
小程序开发之第三天
查看>>
某易游戏经典吃豆豆动画404页面源码
查看>>
专升本——英语视频学习
查看>>
Future education software
查看>>