Correlation Logger
Correlation logger is custom logger written to manage all types of logs in your microservices. It can log each request and reponse of the microservice. Additionally it can also be used to log error and info with correlationId.
Setup
Install
npm install --save correlation-logger
Add variable in '.env' file in root of your service
STORE_APP_NAME=your-app-name
ENV=production
value STORE_APP_NAME should not contain spaces.
Other supported ENV
s are test, staging, development.
Create logs folder and put it in .gitignore
mkdir logsecho logs/ >> .gitignore
It writes logs in logs/combined.log
.
How to use
To log Request and Response
Add the below lines to the microservice's server file ("app" in the code is an implied reference to an instance of Express()):
const requestLogger = ; ;
To add custom logging
const logger = ; // To log informationlogger; // to log errorlogger;
Other supported methods are -
- warn
- verbose
- debug
- silly
request
and request-promise
To forward correlation-id in This is needed if you wanna track a request flowing through several microservices.
// It returns a wrapper around `request` package with added default `correlation-id` headerconst request =
If you are using request-promise-native
package. Use below wrapper for default correlation-id
header
// It returns a wrapper around `request-promise-native` package with added default correlation-id headerconst requestPromiseNative =
Likewise to use wrapper of request-promise
package. Use below wrapper
// It returns a wrapper around `request-promise` package with added default correlation-id headerconst requestPromise =