Node.js Log Frame selection comparison: Winston__div

Source: Internet
Author: User

Logging is critical for problem positioning, debugging, and system performance tuning, especially when the system is complex and running online.

A good development framework will have a logging system that turns off/configurable logging levels. We choose from following several aspects:

1. Every line of the log needs to have accurate time stamp

2. The log format is easy to understand and can be easily analyzed and processed by computer.

3. Allow different log outputs to be configured, for example, different levels of log configuration for different processing methods

Based on the above requirements, there are two sections of the Node.js framework to stand out, respectively, Bunyan and Winston.

Bunyan by Trent Mick. Winston (part of the Flatiron framework), sponsored by Nodejitstu. Winston Winston is one of the most popular log frames in Node.js, designed as a simple common log library that supports multiple transmissions (in Winston, a transmission essentially represents a storage device, where the data is ultimately saved), and each Winston instance can configure different transmissions for different levels of logging. Install (Installation)NPM install Winston
The most basic way to use (Usage) is to use the default Log object instance that the Winston module outputs.

var Winston = require (' Winston ');
Winston.log (' info ', ' Hello distributed Log files! ');
Winston.info (' Hello again distributed logs ');
The above code is the same as the following code effect
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
format (formatting)The default formatter lacks detail information, has no timestamp, machine name, and process number, and the output format is less suited to machine processing. You can get all the information, just have to do a little more work yourself.
Winston.info (' Hello world! ', {timestamp:Date.now (), pid:process.pid});
The output is as follows, the information is richer, but it is still less suitable for machine processing.
Info:hello world! timestamp=1402286804314, pid=80481
Finally, the log method provides the same string filling method as Util.format, such as:
Winston.log (' info ', ' test message%d ', 123);
Transmission (Transporters)Winston can be configured by the parameters of the constructor, or through the exposed interface method, which is described in detail on the GitHub.
Configuration is mainly related to transport, by default, the transport uses the console and files, but on the npmjs.org Web site you can see a variety of community-contributing modules, from MongoDB to other Third-party commercial platforms.
One of the more noteworthy is WINSTON-IRC, which you can use to output logs to your team's IRC channel, which seems very handy.
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 application scale grows, it may be necessary to configure different logs for different functional areas, which can be achieved by winston.loggers (actually a Winston.container instance)
Winston.loggers.add (' Category1 ', {console: {...}, file: {...}}});
Winston.loggers.add (' Category2 ', {IRC: {...}, file: {...}});
This allows you to access the above predefined log instances anywhere in the application:
var category1 = winston.loggers.get (' category1 ');
Category1.info (' Logging from your IoC container-based logger ');

Node.js Log Frame selection comparison: Bunyan

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.