技术控

    今日:0| 主题:63445
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Better Logging in Nodejs

[复制链接]
壹人天荒地老 发表于 2016-10-17 02:16:49
311 3
In this post we will be discussing about better logging in Node.js. If you are writing an application, detailed logging is required for spotting problems and debugging. Logging is an integral part of any application. A well implemented logging mechanism in the application reduces the time to identify the root cause of the problem.
  But developer should be careful on how much amount of logging should be done, thus in this post we will be going through some of the npm packages which will be helpful in reducing the amount consumed by the developer to do logging.
  Simple Logging Using Console

  The most basic kind of logging used in Node.js is to log the errors or information using the built in console. Console module can put the messages as an info or as an error as shown below :
  But it definitely does not serve our purpose for better logging because it lacks the feature of time-stamps, we cannot distinguish between the info and error and also the severity of the error cannot be distinguish.
   

Better Logging in Nodejs

Better Logging in Nodejs

  Logging using NPM Modules

  There are number of modules which can help us in doing the logging right for our application. Here we will be discussing about Winston and Bunyan.
   Bunyan: As per  Bunyan , it is a simple and fast JSON logging library for nodejs services. They also have a Bunyan CLI tool for nicely viewing the logs.
   How to Use Bunyan
  Create a file named example5.js whose contents will be :
   var bunyan = require('bunyan');
   var log = bunyan.createLogger({name: 'myFirstBunyanExample'});
   log.info('1st Log using Bunyan');
   log.warn('It's just a warning');
   And then run it as node example5.js | bunyan
   

Better Logging in Nodejs

Better Logging in Nodejs

   Winston: As per Winston ,  it is a multi-transport async logging library for node.js. Winston is designed to be a simple and universal logging library with support for multiple transports.
   How to use Winston
  Create a file name example6.js whose contents will be "
    var winston = require('winston');
    winston.log('info', 'Coding Defined Info Logger at www.CodingDefined.com');
    winston.info('Coding Defined Again Info Logger at www.CodingDefined.com');
   And then run it as node example6.js
   

Better Logging in Nodejs

Better Logging in Nodejs

  Which one to use : Winston or Bunyan ?

  Winston and Bunyan are both established logging frameworks and have lot of features. It all depends on the preference and how easy it to integrate in your application.
  Please Like and Share the Blog, if you find it interesting and helpful.
12138 发表于 2016-10-21 02:44:57
呵呵。。。顶顶
回复 支持 反对

使用道具 举报

湄涟 发表于 2016-11-19 23:12:05
魏渭(奇点)、谭宗明(谭总)、包奕凡(小包总)都没壹人天荒地老帅
回复 支持 反对

使用道具 举报

胡成鑫 发表于 2016-11-21 09:04:42
趁今日埋头苦干,免他日仰慕求人.
回复 支持 反对

使用道具 举报

我要投稿

推荐阅读


回页顶回复上一篇下一篇回列表
手机版/c.CoLaBug.com ( 粤ICP备05003221号 | 粤公网安备 44010402000842号 )

© 2001-2017 Comsenz Inc.

返回顶部 返回列表