node. js Log Frame selection comparison: Bunyan

Source: Internet
Author: User

node. js Log Frame selection comparison: Winstonbunyan

Bunyan (by Trent Mick) is another log framework worth considering, with a slightly different approach to structuring, and machine readability being treated with emphasis.

As a result, Bunyan per row of log records is actually an output of json.stringify.

Installation (installation) NPM install Bunyan
Use (usage) var bunyan= require (' Bunyan ');
var Log=bunyan.createlogger ({name: ' MyApp '});
Log.info (' Hi ');
Log.warn ({lang: ' fr '}, ' Au revoir ');
Output:
{"Name": "MyApp", "hostname": "Pwony-2", "pid": 12616, "level": +, "msg": "Hi", "Time": "2014-05-26t17:58:32.835z", "V": 0 }
{"Name": "MyApp", "hostname": "Pwony-2", "pid": 12616, "level": +, "lang": "Fr", "msg": "Au revoir", "Time": "2014-05-26t17 : 58:32.837z "," V ": 0}
You can see Bunyan. The log output by default is not readable for people, but it is more compatible with modern computer data processing formats and does not require additional formatting when outputting to other storage.
It's just that we humans, the information in this format is still inconvenient to read, and there is a Bunyan command-line tool that processes JSON data in a standard command-line input mode. The following is an example of an output after Bunyan pipeline processing:
Node Example.js |bunyan
Produces the following output:
[2014-05-26T18:03:40.820Z] info:myapp/13372 on pwony-2: Hi
[2014-05-26t18:03:40.824z] Warn:myapp/13372on Pwony-2:au Revoir (LANG=FR)
The main advantage of this is that there is no need to configure the development environment again, just pass the output to the Bunyan pipeline for processing.
A key difference between Jsonbunyan and Winston is that Bunyan can handle complex contexts and objects. Then look at the example above:
Log.warn ({lang: ' fr '}, ' Au revoir ');
{"Name": "MyApp", "hostname": "Pwony-2", "pid": 12616, "level": +, "lang": "Fr", "msg": "Au revoir", "Time": "2014-05-26t17 : 58:32.837z "," V ": 0}
You can see the Bunyan merge the language references into the log results. Take a look at the following:
Log.info (user, ' registered ');
Log.info ({user:user}, ' registered ');
which produces:
{"Name": "MyApp", "hostname": "Pwony-2", "pid": 14837, "level": "Alex", "email": "[email protected]", "MSG" : "Registered", "Time": "2014-05-26t18:27:43.530z", "V": 0}
{"Name": "MyApp", "hostname": "Pwony-2", "pid": 14912, "level": +, "user": {"username": "Alex", "email": "[email protected ] "}," MSG ":" Registered "," Time ":" 2014-05-26t18:28:19.874z "," V ": 0}
After processing through the Punyan pipeline:
[2014-05-26t18:28:42.455z] info:myapp/14943 on Pwony-2: Registered (Username=alex,[email protected])
[2014-05-26t18:28:42.457z] Info:myapp/14943on pwony-2:registered
user:{
"username": "Alex",
"Email": "[email protected]"
}
When we use the sub-log (child loggers), the beauty of this approach will be revealed.
Child Loggers Bunyan has a sub-log concept, which agrees to specify a log instance for one of your application's subcomponents.

And that is. Creating a new log instance makes it possible to process additional fields.


Child log through Log.child (...) Method is created. This provides great convenience for logging system, request, and simple functions for component logs of different scopes.
If you want to write the request ID into all the logs in the scope of the request, you can bind the log associations together.


varbunyan= require (' Bunyan ');
var log = Bunyan.createlogger ({name: ' MyApp '});

App.use (function (req, res,next) {
Req.log=log.child ({reqid:uuid ()});
Next ();
});

App.get ('/', function (req, res) {
Req.log.info ({User: ...});
});
The Req.log log instance will always pass its context to the Log.child () function and all might call the merge, output such as the following:
{"Name": "MyApp", "hostname": "Pwony-2", "pid": 14837, "level": +, "reqId": "Xxxx-xx-xxxx", "User": "[email protected]", " Time ":" 2014-05-26t18:27:43.530z "," V ": 0}
The serializer (serializers) Bunyan has two problems when formatting the entire object:
1 Circular references (Circular references). Winston is smarter here and will detect what happens in the loop.
2 Redundant data (unwanted noises). I think the object in Bunyan is the first place. So very easy to form a habit to dump objects directly into the log.
To deal with these two problems, Bunyan has a serializer concept, which is basically a conversion function that converts an object to the output format of a partial field:
function Reqserializer (req) {
return{
Method:req.method,
Url:req.url,
Headers:req.headers
}
}

var log = Bunyan.createlogger ({name: ' MyApp ', Serializers:{req:reqserializer}});
Log.info ({req:req});
This simply records the method, URL, and header fields of the request that we are interested in.


The concept of stream (Streams) Bunyan Stream is the same as the concept of transfer (transporters) in Winston – Send your logs to some places for display and storage.
Bunyan uses a writable stream interface with some additional properties.


A Bunyan logger instance has one or more streams that specify the flow options:
var Log=bunyan.createlogger ({
Name: "Foo",
streams:[
{
Stream:process.stderr,
Level: "Debug"
},
...
]

});

How to choose Winston and Bunyan are very mature two frameworks, Winston has a strong community support, and Bunyan makes the log further system analysis processing is very convenient. The key factor in how the two systems are chosen is "Is it easy to integrate with your application system"

by Iefreer-founder of techbrood.com

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.