@@ -14,13 +14,15 @@ import { initLoggerStreams } from './streams';
1414
1515export type PinoLogger = pino . BaseLogger ;
1616export type Level = pino . LevelWithSilent ;
17+ type PinoHooks = { logMethod ?: ( inputArgs : any , method : any ) => void } ;
1718
1819export interface Cosmas extends PinoLogger {
1920 warning : pino . LogFn ;
2021 options : CosmasOptions ;
2122 express : CosmasExpressMiddleware ;
2223 expressError : ErrorRequestHandler ;
2324 stream : Writable ;
25+ realHooks ?: PinoHooks ;
2426 ( childName : string ) : Cosmas ;
2527}
2628
@@ -52,7 +54,7 @@ const objEmpty = (obj: object) => Object.keys(obj).length === 0;
5254// The original version was pino-multistream 4.2.0 (commit bf7941f) - https://github.com/pinojs/pino-multi-stream/blob/bf7941f77661b6c14dd40840ff4a4db6897f08eb/multistream.js#L43
5355const maxLevelWrite : pino . WriteFn = function ( this : any , data : object ) : void {
5456 let stream ;
55- const metadata = Symbol . for ( ' pino.metadata' ) ;
57+ const metadata = pino . symbols . needsMetadataGsym ;
5658 const level = this . lastLevel ;
5759 const streams = this . streams ;
5860 for ( const dest of streams ) {
@@ -105,18 +107,26 @@ const initFormatters = (options: CosmasOptions & { loggerName?: string }) => {
105107} ;
106108
107109const initHooks = ( options : CosmasOptions & { loggerName ?: string } ) => {
108- const hooks : { logMethod ?: ( inputArgs : any , method : any ) => void } = { } ;
109- if ( ! options . loggerName ) return hooks ;
110-
110+ const realHooks : PinoHooks = {
111+ logMethod ( inputArgs , method ) {
112+ return method . apply ( this , inputArgs ) ;
113+ } ,
114+ } ;
115+ const hooks : PinoHooks = {
116+ logMethod ( inputArgs , method ) {
117+ return realHooks . logMethod . call ( this , inputArgs , method ) ;
118+ } ,
119+ } ;
120+ if ( ! options . loggerName ) return { hooks, realHooks } ;
111121 // always put logger name to message
112- hooks . logMethod = function ( inputArgs , method ) {
122+ realHooks . logMethod = function ( inputArgs , method ) {
113123 const text = inputArgs [ inputArgs . length - 1 ] ;
114124 if ( typeof text === 'string' || text instanceof String ) {
115125 inputArgs [ inputArgs . length - 1 ] = `[${ options . loggerName } ] ${ text } ` ;
116126 }
117127 return method . apply ( this , inputArgs ) ;
118128 } ;
119- return hooks ;
129+ return { hooks, realHooks } ;
120130} ;
121131
122132const defaultLogger = ( options : CosmasOptions & { loggerName ?: string } = { } ) : Cosmas => {
@@ -129,7 +139,7 @@ const defaultLogger = (options: CosmasOptions & { loggerName?: string } = {}): C
129139 const streams = initLoggerStreams ( defaultLevel , Object . assign ( { } , options , { messageKey } ) ) ;
130140
131141 const formatters = initFormatters ( options ) ;
132- const hooks = initHooks ( options ) ;
142+ const { hooks, realHooks } = initHooks ( options ) ;
133143
134144 options . ignoredHttpMethods = options . ignoredHttpMethods || [ 'OPTIONS' ] ;
135145 const logger = ( pino (
@@ -154,14 +164,15 @@ const defaultLogger = (options: CosmasOptions & { loggerName?: string } = {}): C
154164
155165 // Add maxLevel support to pino-multi-stream
156166 // This could be replaced with custom pass-through stream being passed to multistream, which would filter the messages
157- const loggerStream = ( logger as any ) [ ( pino as any ) . symbols . streamSym ] as any ;
167+ const loggerStream = ( logger as any ) [ pino . symbols . streamSym ] ;
158168 const streamMaxLevelWrite = maxLevelWrite . bind ( loggerStream ) ;
159169 loggerStream . write = ( chunk : any ) => {
160170 streamMaxLevelWrite ( chunk ) ;
161171 return true ;
162172 } ;
163173 return Object . assign ( logger , {
164174 options,
175+ realHooks,
165176 express : expressMiddleware . bind ( logger ) ,
166177 expressError : expressErrorMiddleware as any ,
167178 } ) ;
0 commit comments