Logging which supports request tracking
npm install log-trackingNote that beginning version 1.1.0 Logger wrapper contains the following new methods:
#### GetTracking() ####
Returns the tracking info put into current session log.
Please find the sample of log4js configuration here.
*
#### startTracking(opt, callback) ####
Initializing of request tracking log
parameter opt unique marks the request. Can representing Number, String or Object.
- if opt represents Number or String, it will be used as unique 'reqId' value.
- if opt parameter represents http.IncommingMessage class (Request object), the unique reqId will be calculated as a MD5 hash of this object.
- if opt represents the custom Object, it should contains a unique value under 'reqId' key.
Moreover, the custom Object can contains the Request object under 'req' key. In this case the 'reqId' will be calculated as a MD5 hash.
The custom object can contains in addition any custom keys.They will be kept in log.
- the random number will be used as a 'reqId' in case of opt parameter missed.
parameter callback is standard callback(err, data)
*
>> // one of bellow listed definitions can be provided
>>global.log_config = 'existing log config path' //default path ('./properties/log4js.json') will be used if this definition omitted
>>global.log_config = require('log4j_config').log_conf; // logger configuration can be defined in the JS file directly.
>global.namespace_name = 'desired namespace name' //default name ('defaultNamespace') will be used if this definition omitted
>const logger = require('log-tracking');
>const nlogger = logger.getLogger('node_server');
>
>http.createServer((req, res) => {
> logger.startTracking(req, (err, data) => {...}
>}).listen(port, host);
>
#### Tests ####
The simple test-modules are located in the './test' folder.
'testLogTracker' creates the simple HTTP server and when you request it by url 'http://127.0.0.1:8080/' the answere should be 'OK'.
Correspondigly the log file should contains the log records with tracking info.
'testLogTrackerS' creates the simple HTTPS server and when you request it by url 'http://127.0.0.1:8443/' the answere should be 'OK'.
Note: './test' folder contains 'requester.js' and 'requestS' modules which provide several HTTP and HTTPS requests in parallel.
The generated log file content shold be like depicted below:
>[2018-07-30T16:00:48.080] [INFO] node_server - : >>>>>>>>>>>>
>[2018-07-30T16:00:48.085] [WARN] node_queue - {"user":"simon","reqId":"f5a36a13b4a8df29f2ececc334ad9e9e"}: start tracking err:null; data:Ok
>[2018-07-30T16:00:48.085] [INFO] node_server - {"user":"simon","reqId":"f5a36a13b4a8df29f2ececc334ad9e9e"}: !!! request finishing !!!
>[2018-07-30T16:00:48.087] [INFO] node_server - : >>>>>>>>>>>>
>[2018-07-30T16:00:48.088] [WARN] node_queue - {"user":"john","reqId":"f8d99cf89980dffa610d4218b40e2855"}: start tracking err:null; data:Ok
>[2018-07-30T16:00:48.089] [INFO] node_server - {"user":"john","reqId":"f8d99cf89980dffa610d4218b40e2855"}: !!! request finishing !!!
>[2018-07-30T16:00:49.093] [INFO] node_server - {"user":"simon","reqId":"f5a36a13b4a8df29f2ececc334ad9e9e"}: >>>> request finished
>[2018-07-30T16:00:49.099] [INFO] node_server - {"user":"john","reqId":"f8d99cf89980dffa610d4218b40e2855"}: >>>> request finished
>
#### References ####
- https://nodejs.org/dist/latest-v8.x/docs/api/async_hooks.html
- https://github.com/Jeff-Lewis/cls-hooked