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 ( {
2013-03-29 00:42:32 +00:00
name : 'myserver' ,
serializers : {
err : Logger . stdSerializers . err , // <--- use this
}
2012-02-02 05:33:18 +00:00
} ) ;
try {
2013-03-29 00:42:32 +00:00
throw new TypeError ( 'boom' ) ;
2012-02-02 05:33:18 +00:00
} catch ( err ) {
2013-03-29 00:42:32 +00:00
log . warn ( { err : err } , 'operation went boom: %s' , err ) // <--- here
2012-02-02 05:33:18 +00:00
}
2012-06-05 06:19:39 +00:00
log . info ( new TypeError ( 'how about this?' ) ) // <--- alternatively this
2012-02-06 17:10:11 +00:00
try {
2013-03-29 00:42:32 +00:00
throw 'boom string' ;
2012-02-06 17:10:11 +00:00
} catch ( err ) {
2013-03-29 00:42:32 +00:00
log . error ( err )
2012-02-06 17:10:11 +00:00
}
2012-06-05 06:19:39 +00:00
/* BEGIN JSSTYLED */
/ * *
*
* $ node err . js | . . / bin / bunyan - j
* {
* "name" : "myserver" ,
* "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)" ,
* "name" : "TypeError" ,
* "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 )
*
* /
/* END JSSTYLED */