2012-02-02 05:33:18 +00:00
// Example logging an error:
var http = require ( 'http' ) ;
var Logger = require ( '../lib/bunyan' ) ;
2012-02-04 08:08:37 +00:00
var util = require ( 'util' ) ;
2012-02-02 05:33:18 +00:00
var log = new Logger ( {
2012-02-06 23:13:50 +00:00
name : "myserver" ,
2012-02-02 05:33:18 +00:00
serializers : {
2012-02-04 08:08:37 +00:00
err : Logger . stdSerializers . err , // <--- use this
2012-02-02 05:33:18 +00:00
}
} ) ;
try {
throw new TypeError ( "boom" ) ;
} catch ( err ) {
log . warn ( { err : err } , "operation went boom: %s" , err ) // <--- here
}
2012-02-06 17:10:11 +00:00
log . info ( new TypeError ( "how about this?" ) ) // <--- alternatively this
try {
throw "boom string" ;
} catch ( err ) {
log . error ( err )
}
2012-02-02 05:33:18 +00:00
/ *
$ node err . js | . . / bin / bunyan - j
{
2012-02-06 23:13:50 +00:00
"name" : "myserver" ,
2012-02-02 05:33:18 +00:00
"hostname" : "banana.local" ,
"err" : {
"stack" : "TypeError: boom\n at Object.<anonymous> (/Users/trentm/tm/node-bunyan/examples/err.js:15:9)\n at Module._compile (module.js:411:26)\n at Object..js (module.js:417:10)\n at Module.load (module.js:343:31)\n at Function._load (module.js:302:12)\n at Array.0 (module.js:430:10)\n at EventEmitter._tickCallback (node.js:126:26)" ,
2012-02-04 08:08:37 +00:00
"name" : "TypeError" ,
2012-02-02 05:33:18 +00:00
"message" : "boom"
} ,
"level" : 4 ,
"msg" : "operation went boom: TypeError: boom" ,
"time" : "2012-02-02T04:42:53.206Z" ,
"v" : 0
}
$ node err . js | . . / bin / bunyan
[ 2012 - 02 - 02 T05 : 02 : 39.412 Z ] WARN : myserver on banana . local : operation went boom : TypeError : boom
TypeError : boom
at Object . < anonymous > ( / U s e r s / t r e n t m / t m / n o d e - b u n y a n / e x a m p l e s / e r r . j s : 1 5 : 9 )
at Module . _compile ( module . js : 411 : 26 )
at Object . . js ( module . js : 417 : 10 )
at Module . load ( module . js : 343 : 31 )
at Function . _load ( module . js : 302 : 12 )
at Array . 0 ( module . js : 430 : 10 )
at EventEmitter . _tickCallback ( node . js : 126 : 26 )
* /