SAP / cf-nodejs-logging-support

Node.js Logging Support for Cloud Foundry provides the creation of structured log messages and the collection of request metrics
https://sap.github.io/cf-nodejs-logging-support/
Apache License 2.0
43 stars 22 forks source link

CorrelationId not set when creating Child Logger #185

Closed chrsch-dev closed 1 year ago

chrsch-dev commented 1 year ago

Hi,

I discovered, that creating a new Child Logger doesn't work with the new version as before (v6). Before I used the createLogger() function to create a new logger and got automatically the correlation_id property set (code.

When doing the same with the current version of the library, the correlation_id is not set. Even calling the setCorrelationId() method of the logger doesn't work :(

christiand93 commented 1 year ago

Hi Christian,

thank you for reaching out. I can confirm that the correlation_id field is not auto-generated nor settable for message logs written in the global context.

In general, context values are stored in a RequestContext object attached to a logger instance. The RequestContext is primarily responsible for storing request related context values, which is why it is not set for loggers in global context.

However, I agree that having a common correlation_id for log messages can be beneficial, even if the messages are not related to a specific request. My proposal would be to allow enabling context values for global context loggers. Enabling the context feature for a logger could be achieved by calling a dedicated method, e.g., logger.enableContext(), which would also process the context configuration (= generate a correlation_id on the fly).

Secondly, contexts should be inherited whenever createLogger() is called. This would also ensure that child loggers inherit request values, e.g., request_id, from their parent request logger (req.logger).

I will discuss this approach with my colleagues and provide a PR/update asap.

Kind regards, Christian

chrsch-dev commented 1 year ago

That would be great. Our current scenario is the following: Our application receives an event from an Eventbus (thus no request context available at this time). It does something with the event and passes the information on to another application (using a rest request). To see the flow of the event from the beginning to the end we want to use the correlationId. With the previous version of the lib it was possible and I think we have to stay at this version until the new version has support for this.

Thanks for your fast feedback :)

Best regards,

Christian

christiand93 commented 1 year ago

https://github.com/SAP/cf-nodejs-logging-support/pull/186 is merged, the fix/feature is available in version 7.1.0.

chrsch-dev commented 1 year ago

Great, will check it after my vacation :)