博客
关于我
A Guide to Node.js Logging
阅读量:342 次
发布时间: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/

你可能感兴趣的文章
js中[]、{}、()的区别
查看>>
js-禁止右键菜单代码、禁止复制粘贴代码
查看>>
搭建samba服务器
查看>>
Java: 错误: 不支持发行版本 5
查看>>
SpringBoot中使用Mybatis访问MySQL数据库(使用xml方式)
查看>>
python中的map( )函数及lambda()函数简介
查看>>
普通平衡树板子
查看>>
JSP内置对象:操作cookie、session对象
查看>>
【SE-02】多线程-02
查看>>
$set的使用(视图不能实时更新)
查看>>
一、硬件防火墙
查看>>
Javaweb jQuery功能练习
查看>>
余生,愿你能靠近那些正能量的人——
查看>>
蓝桥杯入门练习题斐波那契数列
查看>>
context:include-filter与exclude-filte控制扫描组件
查看>>
【SSL】1072砝码称重
查看>>
js数据结构--队列--常见操作
查看>>
全排列(深度优先搜索+递归)
查看>>
多项式插值法的Python程序
查看>>
vue.js常用指令及用法
查看>>