diff --git a/lib/index.js b/lib/index.js index b46f865..d58ab35 100644 --- a/lib/index.js +++ b/lib/index.js @@ -11,14 +11,14 @@ const logger = pino({ } }); -const ns = createNamespace(NAMESPACE); +const namespace = createNamespace(NAMESPACE); const getRequestId = () => { - return ns ? ns.get('requestId') : null; + return namespace ? namespace.get('requestId') : null; }; const setRequestId = (id = uuid()) => { - ns.set('requestId', id); + namespace.set('requestId', id); return id; } @@ -42,24 +42,24 @@ const expressMiddleware = (req, res, next) => { `Started ${method} ${url} with params: ${params}, query: ${query}, body: ${body}.` ); const begin = Date.now(); - const foo = (error, response) => { + const foo = namespace.bind((error, response) => { const end = Date.now(); const responseTime = error ? '-' : end - begin; const status = response.statusCode; info(`Ended ${method} ${url} with status: ${status} in ${responseTime} ms`); - }; + }); onFinished(res, foo); next(); }; const expressRequestIdMiddleware = (req, res, next) => { - ns.bindEmitter(req); - ns.bindEmitter(res); - return ns.run(() => { + namespace.bindEmitter(req); + namespace.bindEmitter(res); + return namespace.run(() => { const requestId = req.headers['x-request-id'] || uuid(); setRequestId(requestId); next(); }); }; -module.exports = { logger: { ...logger, info, error }, expressMiddleware, expressRequestIdMiddleware, getRequestId, setRequestId }; +module.exports = { logger: { ...logger, info, error }, expressMiddleware, expressRequestIdMiddleware, getRequestId, setRequestId, namespace }; diff --git a/package.json b/package.json index a26b569..19d6883 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "express-wolox-logger", - "version": "0.0.2", + "version": "0.0.3", "description": "ExpressJS Wolox Logger", "main": "index.js", "scripts": {