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

Log layer field is always [NODEJS] #67

Closed lxdv163 closed 3 years ago

lxdv163 commented 3 years ago

Hi, I want to use this lib in our Nodejs application. We use @sap/logging before. I found an issue from test, It seems new lib can't set a loggerCategory for each log. So the "layer" property in log always shows: [NODEJS] What I want is layer property can show which node js file is writing the log because our project has many node js files. If I can see the node js file name from layer, it's very helpful for our debug and monitoring.

current logger is: {"component_type":"application","component_id":"57e998ea-5b1b-4297-9f6f-c20cece7f93f","component_name":"mobile-approuter","component_instance":0,"layer":"[NODEJS]","organization_name":"org1","organization_id":"1070e5be-6b4d-4ae1-928b-75c5e909e9d2","space_name":"space1","space_id":"2a177026-846f-4a9d-92bf-d80b1c38fe95","container_id":"10.0.138.80","logger":"nodejs-logger","source_instance":0,"written_at":"2020-11-27T08:18:23.474Z","written_ts":1606465103474000000,"level":"warn","correlation_id":"cbc2654f-1c35-45d0-96fc-f32efac20986","tenant_id":"cbc2654f-1c35","tenant_subdomain":"-","request_id":"","msg":"warn working","type":"log"}

I want log like this: {"component_type":"application","component_id":"57e998ea-5b1b-4297-9f6f-c20cece7f93f","component_name":"mobile-approuter","component_instance":0,"layer":"/mobile-approuter/cookie-handler","organization_name":"org1","organization_id":"1070e5be-6b4d-4ae1-928b-75c5e909e9d2","space_name":"space1","space_id":"2a177026-846f-4a9d-92bf-d80b1c38fe95","container_id":"10.0.138.80","logger":"nodejs-logger","source_instance":0,"written_at":"2020-11-27T08:18:23.474Z","written_ts":1606465103474000000,"level":"warn","correlation_id":"cbc2654f-1c35-45d0-96fc-f32efac20986","tenant_id":"cbc2654f-1c35","tenant_subdomain":"-","request_id":"","msg":"warn working","type":"log"}

nicklas-dohrn commented 3 years ago

Hey, you can replace the layer at any point yourself, when you use the logging format

log.info("test message",{"layer":"/mobile-approuter/cookie-handler"})

I know, that this is not the answer you were looking for, but I am not sure if it is easy to figure out, which file the log message was triggered from.

I will investigate if there is a feasible way to get better layer tracing in nodejs, and hope that the quick fix i described above will suffice for now.

lxdv163 commented 3 years ago

Hi nicklas, Thanks to your reply. Do you mean set the layer value to custom fields?

nicklas-dohrn commented 3 years ago

Hey, Sorry for the late reply, I had a talk with my team and we do not advice to reuse layer in this way. Yes you can always use a second custom field like "file-layer" to track the position where an error occurred.

nicklas-dohrn commented 3 years ago

Hey, I found that the functionality i described did not work with application-logging due to a bugged implementation, I fixed it for v6.5.4 After testing it with my dev setup, I found another usecase, which might fit your case even better, so much so, that I think it is sufficiently supported already:

//You can create a sublogger with specific settings like so:
log = require("cf-nodejs-logging-support");
sublogger = log.createLogger({"layer":"sub-logger-layer"});

sublogger.info("contains layer everytime")
//resulting log:
//{"msg":"contains layer everytime", ...,"layer":"sub-logger-layer")
nicklas-dohrn commented 3 years ago

Closed due to beeing already supported

lxdv163 commented 3 years ago

hi, Thanks for your fix, I use v6.5.4 test, it works. But it seems request logger not work. my code like: var log = require('cf-nodejs-logging-support'); var sublogger = req.logger.createLogger( { "layer": "cookie-handler" } ); sublogger.info("nodejs log test"); From the log info, layer value is [NODEJS]

nicklas-dohrn commented 3 years ago

Hey, From what I see from my tests, the function you described works as intended. The req.logger context is only correctly valid, while the req is still in progress. the sub logger may also break after the req is finished.