nestjs-pino
Advanced tools
Comparing version 4.1.0-alpha.17152f to 4.1.0-alpha.f16a6d
@@ -64,4 +64,4 @@ "use strict"; | ||
configure(consumer) { | ||
const { exclude, forRoutes = DEFAULT_ROUTES, pinoHttp, useExisting, } = this.params; | ||
const middlewares = createLoggerMiddlewares(pinoHttp || {}, useExisting); | ||
const { exclude, forRoutes = DEFAULT_ROUTES, pinoHttp, useExisting, assignResponse, } = this.params; | ||
const middlewares = createLoggerMiddlewares(pinoHttp || {}, useExisting, assignResponse); | ||
if (exclude) { | ||
@@ -85,5 +85,5 @@ consumer | ||
], LoggerModule); | ||
function createLoggerMiddlewares(params, useExisting = false) { | ||
function createLoggerMiddlewares(params, useExisting = false, assignResponse = false) { | ||
if (useExisting) { | ||
return [bindLoggerMiddlewareFactory(useExisting)]; | ||
return [bindLoggerMiddlewareFactory(useExisting, assignResponse)]; | ||
} | ||
@@ -96,16 +96,20 @@ const middleware = (0, pino_http_1.pinoHttp)(...(Array.isArray(params) ? params : [params])); | ||
// pinoHttp has two overloads, each of them takes those types | ||
return [middleware, bindLoggerMiddlewareFactory(useExisting)]; | ||
return [middleware, bindLoggerMiddlewareFactory(useExisting, assignResponse)]; | ||
} | ||
function bindLoggerMiddlewareFactory(useExisting) { | ||
return function bindLoggerMiddleware(req, _res, next) { | ||
function bindLoggerMiddlewareFactory(useExisting, assignResponse) { | ||
return function bindLoggerMiddleware(req, res, next) { | ||
let log = req.log; | ||
let resLog = assignResponse ? res.log : undefined; | ||
if (!useExisting && req.allLogs) { | ||
log = req.allLogs[req.allLogs.length - 1]; | ||
} | ||
if (assignResponse && !useExisting && res.allLogs) { | ||
resLog = res.allLogs[res.allLogs.length - 1]; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore: run requires arguments for next but should not because it can | ||
// be called without arguments | ||
storage_1.storage.run(new storage_1.Store(log), next); | ||
storage_1.storage.run(new storage_1.Store(log, resLog), next); | ||
}; | ||
} | ||
//# sourceMappingURL=LoggerModule.js.map |
{ | ||
"name": "nestjs-pino", | ||
"version": "4.1.0-alpha.17152f", | ||
"version": "4.1.0-alpha.f16a6d", | ||
"description": "Pino logger for NestJS", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -49,2 +49,8 @@ import { MiddlewareConfigProxy, ModuleMetadata } from '@nestjs/common/interfaces'; | ||
renameContext?: string; | ||
/** | ||
* Optional parameter to also assign the response logger during calls to | ||
* `PinoLogger.assign`. By default, `assign` does not impact response logs | ||
* (e.g.`Request completed`). | ||
*/ | ||
assignResponse?: boolean; | ||
} | ||
@@ -51,0 +57,0 @@ export interface LoggerModuleAsyncParams extends Pick<ModuleMetadata, 'imports' | 'providers'> { |
@@ -87,2 +87,3 @@ "use strict"; | ||
assign(fields) { | ||
var _a; | ||
const store = storage_1.storage.getStore(); | ||
@@ -93,2 +94,3 @@ if (!store) { | ||
store.logger = store.logger.child(fields); | ||
(_a = store.responseLogger) === null || _a === void 0 ? void 0 : _a.setBindings(fields); | ||
} | ||
@@ -95,0 +97,0 @@ call(method, ...args) { |
@@ -440,3 +440,3 @@ <p align="center"> | ||
Due to the [limitation](https://github.com/pinojs/pino-http/issues/30) of the underlying `pino-http` `PinoLogger.assign` cannot extend `Request completed` logs. | ||
By default, this does not extend `Request completed` logs. Set the `assignResponse` parameter to `true` to also enrich response logs automatically emitted by `pino-http`. | ||
@@ -443,0 +443,0 @@ ## Change pino params at runtime |
@@ -5,4 +5,5 @@ import { AsyncLocalStorage } from 'async_hooks'; | ||
logger: Logger; | ||
constructor(logger: Logger); | ||
responseLogger?: Logger | undefined; | ||
constructor(logger: Logger, responseLogger?: Logger | undefined); | ||
} | ||
export declare const storage: AsyncLocalStorage<Store>; |
@@ -6,4 +6,5 @@ "use strict"; | ||
class Store { | ||
constructor(logger) { | ||
constructor(logger, responseLogger) { | ||
this.logger = logger; | ||
this.responseLogger = responseLogger; | ||
} | ||
@@ -10,0 +11,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
58061
624