Reputation: 507
I want to tell firstly it's my first time working on node. My node application does not use express framework or something like that. My app is deployed in containers under docker. I'm using the Winston module to store my application logs. This is how my logger.js looks:
const { createLogger, format, transports } = require('winston');
const { combine, json, splat, timestamp } = format;
const logLevel = process.env.LOG_LEVEL || 'info';
const logger = createLogger({
level: logLevel,
format: combine(timestamp(), splat(), json()),
defaultMeta: {
service: 'my-service',
'Transaction-Ref-Id': 'HERE SHOULD BE TXNREFID'},
transports: [new transports.Console()],
silent: process.env.NODE_ENV === 'test',
});
module.exports = logger;
I'm using defaultMeta
to have some default information in each log I print. Now I want to create a unique Id for each request I receive in my application and have that Id in all the methods and functions who are involved in the request.
The unique Id is being passed in the header and if the user does not pass it, I'm generating the unique Id. The problem is I don't know how to pass that unique Id to the defaultMeta
in logger.js
and have it in all the scope of the request.
I've researched about local-storage but I think is a bad practice because node is a server-side technology. I can't use express framework or cls-hooked or things like that.
Upvotes: 3
Views: 236