Winston JSON Formatter
A json and console formatter for winston@3.
Quickstart:
const createLogger transports = ;const configuredFormatter = ; // Create Logger and configure options. const logger = ;const options = service: 'test-service' logger: 'Winston-JSON-Formatter' version: '1.0.0' typeFormat: 'json'; // set winston logger format and print logs.loggerformat = ; // Availale log levelslogger;logger;logger;logger;logger;logger; // Error Objects can be logged directlyerr = 'Heroic BSoD'logger
Configuration
ENV
NODE_ENV=dev
||NODE_ENV=development
=>options.typeFormat=console
- Otherwise,
options.typeFormat=json
(default)
Option | Default | Type |
---|---|---|
options.typeFormat |
Default: 'json' | Enum: 'console', 'json' |
options.hostname |
Default: os.hostname() |
String |
options.logger |
Default: 'application-logger' | String |
options.node_env |
Default: ENV NODE_ENV |
String |
options.service |
String | |
options.version |
String |
Test
yarn installyarn testyarn test:coverage
Output
Json logging
> const err = 'error message';> const err.code = 'SOME_CODE';> // The colon and space here are recommended due to this bug in winston (https://github.com/winstonjs/winston/issues/1634)> logger; "service": "test-service" "logger": "Winston-JSON-Formatter" "hostname": "host" "level": "info" "msg": "log message: error message" "meta": "service": "version": "1.0.0" "node_env": "" "logger": "time": "2018-11-28T02:52:06.700Z" "event": "code": "SOME_CODE" "err": // There should be a member `"name": "Error"` here if it were not for winston bug (https://github.com/winstonjs/winston/issues/1635) "stack": "THE_ERROR_STACK" > const err = 'error message';> const err.code = 'SOME_CODE';> logger; "service": "test-service" "logger": "Winston-JSON-Formatter" "hostname": "host" "level": "info" "msg": "message: error message" "meta": "service": "version": "1.0.0" "node_env": "" "logger": "time": "2018-11-28T02:52:06.700Z" "event": "foo": "bar" "baz": "qux" "code": "SOME_CODE" "err": "name": "Error" "stack": "THE_ERROR_STACK"
Console logging
# Generally, the format is: # timestamp level message # stack_if_one_exists # { # other: 'members-if-they-exist' # } # > logger.info("log message") ------------------------------------------------------------------ 2019-04-12T02:10:11.091Z info log message # ---- # > logger.info(new Error("error message")) ------------------------------------------------------------------ 2019-04-12T02:10:11.094Z info error message Error: error message at Object.<anonymous> at Module._compile at Object.Module._extensions..js at Module.load at tryModuleLoad at Function.Module._load at Function.Module.runMain at startup at bootstrap_node.js:625:3 # ---- # > logger.info("message", new Error("error message")) ------------------------------------------------------------------ 2019-04-12T02:10:11.095Z info log messageerror message Error: error message at Object.<anonymous> at Module._compile at Object.Module._extensions..js at Module.load at tryModuleLoad at Function.Module._load at Function.Module.runMain at startup at bootstrap_node.js:625:3 # ---- # > logger.info({a: "a", b: "b"}) ------------------------------------------------------------------ 2019-04-12T02:10:11.095Z info # ---- # > logger.info(new Error("error message"), { a: "a", b: "b" } ) ------------------------------------------------------------------ 2019-04-12T02:10:11.096Z info error message Error: error message at Object.<anonymous> at Module._compile at Object.Module._extensions..js at Module.load at tryModuleLoad at Function.Module._load at Function.Module.runMain at startup at bootstrap_node.js:625:3 # ---- # Logging an object that is not an Error that does have a member named stack will confuse the logger. This is unavoidable. # > logger.info({ name: "some name", stack: "some stack" }) ------------------------------------------------------------------ 2019-04-12T02:10:11.096Z info some stack # ---- # Logging an object that is not an Error that does have a member named stack will confuse the logger. This is unavoidable. # > logger.info("message", { name: "some name", stack: "some stack" }) ------------------------------------------------------------------ 2019-04-12T02:10:11.096Z info message some stack # ---- # > logger.info("message", { a: "a", b: "b" }) ------------------------------------------------------------------ 2019-04-12T02:10:11.096Z info message # ---- # > const err = new Error("error message"); err.code = "SOME_CODE"; logger.info("message", new Error("error message")) ------------------------------------------------------------------ 2019-04-12T02:10:11.097Z info messageError A Error: Error A at Object.<anonymous> at Module._compile at Object.Module._extensions..js at Module.load at tryModuleLoad at Function.Module._load at Function.Module.runMain at startup at bootstrap_node.js:625:3 # ---- # > const err = new Error("error message"); err.code = "SOME_CODE"; logger.info(new Error("error message")) ------------------------------------------------------------------ 2019-04-12T02:10:11.097Z info Error A Error: Error A at Object.<anonymous> at Module._compile at Object.Module._extensions..js at Module.load at tryModuleLoad at Function.Module._load at Function.Module.runMain at startup at bootstrap_node.js:625:3 # ---- # > const err = new Error("error message"); err.code = "SOME_CODE"; logger.info(new Error("error message"), { a: "a", b: "b" } ) ------------------------------------------------------------------ 2019-04-12T02:10:11.097Z info Error A Error: Error A at Object.<anonymous> at Module._compile at Object.Module._extensions..js at Module.load at tryModuleLoad at Function.Module._load at Function.Module.runMain at startup at bootstrap_node.js:625:3
Miscellaneous Goodies
This repo also provides a function to check if a particular log level is greater than the currently configured log level.
Example useage:
// In some service... // config.jsmoduleexports = logLevel: 'info' // FancyCustomError.jsconst config = const logLevelGte = { super thismessage = message if thisrootErrorThatCouldHaveSecretStuffInIt = rootErrorThatCouldHaveSecretStuffInIt } // someFile.jslogger // When the configured log level is 'info', you would see logged only 'Error creating user', but when the configured log level is 'debug', you would see all the guts and glory.