@mirekpluta/jg-logger
v1.0.8
Published
Preconfigured winston logger
Downloads
13
Readme
jg-logger
Small utility providing logger that configures winston logger for usage on local (dev) environment where readability of logs is critical as well as production, dockerized environments where logs are being processed by tools.
Installation
Simply execute
npm install @mirekpluta/jg-logger
Configuration
Logger is being configured with config npm package, therefore it requires json config files to be present in config
directory.
Example config file:
{
"logger": {
"logLevel": "debug",
"format": "pretty",
"defaultMeta": {
"serviceName": "test"
}
}
}
Explanation
- logLevel - log level as specified in
winston
library (error, warn, info, verbose, debug, silly) - format
- pretty - to be used when running locally, it displays colorized log with timestamps, padded levels and json stringified metadata + potential stacktrace in case of logged exception
- json - it'll display each log entry as json object including timestamp and stacktrace in case of errors
- defaultMeta - default metadata that will be included to all logs from the project
Functionality
- both human friendly (pretty) and tools friendly (json) logs format (see Configuration above).
- catches and logs all uncought errors (it will prevent application from exiting)
- provides errorLoggingMiddleware express middleware to be registered at the very end with express app, that logs all rejected promises
- uses express-async-errors package that will handle all rejected promises from middleware async functions
- adds logger property to global object for easy access throughout entire application
Usage
Import early: - logger should be imported as early in the project as possible, so that it can be initialized
logger.errorLoggingMiddleware - express middleware that should be registered to express app as the last one
use like you would use winston logger - configured logger is just winston logger, so entire functionality of it is available.