Last active
February 12, 2020 09:55
-
-
Save haishanh/fc34184cbe6d562300a3269602354de6 to your computer and use it in GitHub Desktop.
use bunyan-logger-factory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
/** | |
* on Mac you can use system builtin app Console to view syslog | |
* but logProto has to be 'sys' | |
* Note, Console only shows logs with level >= warn by default | |
* | |
* | |
* fatal 60 | |
* error 50 | |
* warn 40 | |
* info 30 | |
* debug 20 | |
* trace 10 | |
*/ | |
const loggerFactory = require("bunyan-logger-factory"); | |
const loggerFile = loggerFactory.init({ | |
logName: "filefoo", | |
logStream: "stdout" | |
}); | |
const loggerSyslog = loggerFactory.init({ | |
logName: "foo", | |
logStream: "SYSLOG", | |
logHost: "127.0.0.1", | |
logPort: 514, | |
// type LogProto = 'sys' | 'tcp' | |
logProto: "sys" | |
}); | |
const o = { | |
name: "foo", | |
num: 123, | |
createdAt: new Date(), | |
a: { | |
b: { | |
c: { | |
d: "d" | |
} | |
} | |
} | |
}; | |
const err = new Error("bar"); | |
const logger = loggerSyslog; | |
logger.warn("a%sc", 'b'); | |
// {"name":"foo","hostname":"juno.lan","pid":46680,"level":40,"msg":"abc","time":"2020-02-12T09:49:22.433Z","v":0} | |
////////// logger can expand object nicely if it's the first param | |
logger.warn(o, "xxx warn"); | |
// {"name":"foo","hostname":"juno.lan","pid":46227,"level":40,"num":123,"createdAt":"2020-02-12T09:44:49.654Z","a":{"b":{"c":{"d":"d"}}},"msg":"xxx warn","time":"2020-02-12T09:44:49.654Z","v":0} | |
logger.warn({ o }, "xxx warn"); | |
// {"name":"foo","hostname":"juno.lan","pid":46227,"level":40,"o":{"name":"foo","num":123,"createdAt":"2020-02-12T09:44:49.654Z","a":{"b":{"c":{"d":"d"}}}},"msg":"xxx warn","time":"2020-02-12T09:44:49.656Z","v":0} | |
logger.warn("xxx warn", o); // won't work | |
// {"name":"foo","hostname":"juno.lan","pid":46326,"level":40,"msg":"xxx warn {\n name: 'foo',\n num: 123,\n createdAt: 2020-02-12T09:45:53.104Z,\n a: { b: { c: [Object] } }\n}","time":"2020-02-12T09:45:53.110Z","v":0} | |
////////// "err" is a magic prop, "error" wont work | |
logger.warn({ err: err }, "xxx warn"); // set NODE_ENV to 'production' before creating logger to remove the stack trace in log | |
// {"name":"foo","hostname":"juno.lan","pid":46069,"level":40,"err":{"message":"bar","name":"Error","stack":"Error: bar\n at Object.<anonymous> (/Users/haishan/t/bunyan-logger-factory0/app.js:46:13)\n at Module._compile (internal/modules/cjs/loader.js:959:30)\n at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)\n at Module.load (internal/modules/cjs/loader.js:815:32)\n at Function.Module._load (internal/modules/cjs/loader.js:727:14)\n at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)\n at internal/main/run_main_module.js:17:11"},"msg":"xxx warn","time":"2020-02-12T09:43:03.403Z","v":0} | |
logger.warn({ error: err }, "xxx warn"); | |
// {"name":"foo","hostname":"juno.lan","pid":45929,"level":40,"error":{},"msg":"warn","time":"2020-02-12T09:41:24.598Z","v":0} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment