node. js Log Frame selection comparison: Winston

Source: Internet
Author: User

Logs are critical for problem locating, debugging, and system performance tuning, especially in the case of complex systems and online execution.

A good development framework will have a logging system that can turn on off/configurable logging levels. We will choose from the following aspects:

1. Every line of logs requires an accurate timestamp

2. Log format easy to be understood at the same time can also be computer analysis processing

3. Agree to configure different log outputs, for example, different levels of log configuration processing

Based on the above requirements, there are two node. JS frameworks that stand out, each Bunyan and Winston.

    • Bunyan by Trent Mick.
    • Winston (part of the Flatiron framework), sponsored by Nodejitstu.
WinstonWinston is one of the most popular logging frameworks in node. js, designed as a simple, universal log library that supports multiple transmissions (in Winston, where a transmission essentially represents a storage device, where the data is finally saved), each Winston instance can be configured to transmit different levels of logs. installation (Installation)NPM Install Winston
usage (usage)The most important way is to use the default Log object instance output by the Winston module.
var Winston = require (' Winston '); Winston.log (' Info ', ' Hello distributed Log files! '); Winston.info (' Hello again distributed logs ');
The code above has the same effect as the following code
var Winston = require (' Winston '); var logger = new Winston. Logger (); Logger.log (' info ', ' Hello distributed Log files! '); Logger.info (' Hello again distributed logs ');
Both pieces of code produce the following output:
Info:hello Distributed Log files!
Info:hello again distributed logs
The format (formatting) default formatter lacks detail information, no timestamp, machine name, and process number, and the output format is less suitable for machine processing. You can get all the information, just have to do a little more work.
Winston.info (' Hello world! ', {timestamp:Date.now (), pid:process.pid});
The output, such as the following, is more informative, but still less suitable for machine handling.
Info:hello world! timestamp=1402286804314, pid=80481
Finally, the log method provides the same string fill method as Util.format, for example:
Winston.log (' info ', ' test message%d ', 123);
The transfer (transporters) Winston can be configured by the parameters of the constructor, or through an exposed interface method, on GitHub with a detailed descriptive narrative.
Configuration is primarily related to transport, by default, the transfer uses the console and files, but only on the npmjs.org site can see a variety of community-contributed modules, from MongoDB to other third-party commercial platforms.
One of the more noteworthy is that WINSTON-IRC, which you can use to output logs to your team's IRC channel, seems convenient.
Winston.add (Require (' Winston-irc '), {  host: ' Irc.somewhere.net ',  Nick: ' Logger ',  pass: ' Hunter2 ',  Channels: {    ' #logs ': true,    ' sysadmin ': [' warn ', ' Error ']  }});
Multiple log instances (multiple loggers) when the scale of your application grows, you may need to configure different logs for different functional areas, which can be achieved by winston.loggers (actually Winston.container instances)
Winston.loggers.add (' Category1 ', {console: {...}, file: {...}}); Winston.loggers.add (' Category2 ', {IRC: {...}, file: {...}});
This allows you to access the pre-set log instances described above anywhere in the app:
var category1 = winston.loggers.get (' category1 '); Category1.info (' Logging from your IoC container-based logger ');

Post node. js Log Frame selection comparison: Bunyan

node. js Log Frame selection comparison: Winston

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.