@@ -29,11 +29,9 @@ interface LogOptions<T extends (...arg: any) => any> {
2929 4 ?: ( ( arg : Parameters < T > [ 4 ] ) => unknown ) | string | false ;
3030 [ key : number ] : ( ( ( arg : any ) => unknown ) | string | false ) | undefined ;
3131 } ;
32- condition ?( ...args : Parameters < T > ) : boolean ;
3332 enter ?( ...args : Parameters < T > ) : string ;
3433 exit ?: ( ( result : PromiseType < ReturnType < T > > ) => string ) | boolean ;
3534 prefix ?( context : LogContext , ...args : Parameters < T > ) : string ;
36- sanitize ?( key : string , value : any ) : any ;
3735 logThreshold ?: number ;
3836 scoped ?: boolean ;
3937 singleLine ?: boolean ;
@@ -56,23 +54,19 @@ type PromiseType<T> = T extends Promise<infer U> ? U : T;
5654
5755export function log < T extends ( ...arg : any ) => any > ( options ?: LogOptions < T > , debug = false ) {
5856 let overrides : LogOptions < T > [ 'args' ] | undefined ;
59- let conditionFn : LogOptions < T > [ 'condition' ] | undefined ;
6057 let enterFn : LogOptions < T > [ 'enter' ] | undefined ;
6158 let exitFn : LogOptions < T > [ 'exit' ] | undefined ;
6259 let prefixFn : LogOptions < T > [ 'prefix' ] | undefined ;
63- let sanitizeFn : LogOptions < T > [ 'sanitize' ] | undefined ;
6460 let logThreshold : NonNullable < LogOptions < T > [ 'logThreshold' ] > = 0 ;
6561 let scoped : NonNullable < LogOptions < T > [ 'scoped' ] > = false ;
6662 let singleLine : NonNullable < LogOptions < T > [ 'singleLine' ] > = false ;
6763 let timed : NonNullable < LogOptions < T > [ 'timed' ] > = true ;
6864 if ( options != null ) {
6965 ( {
7066 args : overrides ,
71- condition : conditionFn ,
7267 enter : enterFn ,
7368 exit : exitFn ,
7469 prefix : prefixFn ,
75- sanitize : sanitizeFn ,
7670 logThreshold = 0 ,
7771 scoped = true ,
7872 singleLine = false ,
@@ -89,9 +83,7 @@ export function log<T extends (...arg: any) => any>(options?: LogOptions<T>, deb
8983 scoped = true ;
9084 }
9185
92- const logFn = debug ? Logger . debug . bind ( Logger ) : Logger . log . bind ( Logger ) ;
93- const warnFn = Logger . warn . bind ( Logger ) ;
94- const errorFn = Logger . error . bind ( Logger ) ;
86+ const logFn : ( message : string , ...params : any [ ] ) => void = debug ? Logger . debug : Logger . log ;
9587
9688 return ( target : any , key : string , descriptor : PropertyDescriptor & Record < string , any > ) => {
9789 let fn : Function | undefined ;
@@ -105,38 +97,33 @@ export function log<T extends (...arg: any) => any>(options?: LogOptions<T>, deb
10597 }
10698 if ( fn == null || fnKey == null ) throw new Error ( 'Not supported' ) ;
10799
108- const parameters = getParameters ( fn ) ;
100+ const debugging = Logger . isDebugging ;
101+ const parameters = overrides !== false ? getParameters ( fn ) : [ ] ;
109102
110103 descriptor [ fnKey ] = function ( this : any , ...args : Parameters < T > ) {
111- const scopeId = getNextLogScopeId ( ) ;
112-
113- if (
114- ( ! Logger . isDebugging && ! Logger . enabled ( 'debug' ) && ! ( Logger . enabled ( 'info' ) && ! debug ) ) ||
115- ( conditionFn != null && ! conditionFn ( ...args ) )
116- ) {
117- return fn ! . apply ( this , args ) ;
104+ if ( ! debugging && ! Logger . enabled ( debug ? 'debug' : 'info' ) ) {
105+ return ;
118106 }
119107
120- let instanceName : string ;
121- if ( this != null ) {
122- instanceName = getLoggableName ( this ) ;
123- if ( this . constructor ?. [ LogInstanceNameFn ] ) {
124- instanceName = target . constructor [ LogInstanceNameFn ] ( this , instanceName ) ;
125- }
126- } else {
127- instanceName = emptyStr ;
128- }
108+ const scopeId = getNextLogScopeId ( ) ;
109+
110+ const instanceName =
111+ this != null
112+ ? this . constructor ?. [ LogInstanceNameFn ] ?.( this , getLoggableName ( this ) ) ?? getLoggableName ( this )
113+ : undefined ;
129114
130- let prefix = `${ scoped ? `[${ scopeId . toString ( 16 ) . padStart ( 5 ) } ] ` : emptyStr } ${
131- instanceName ? `${ instanceName } .` : emptyStr
132- } ${ key } `;
115+ let prefix = instanceName
116+ ? scoped
117+ ? `[${ scopeId . toString ( 16 ) . padStart ( 5 ) } ] ${ instanceName } .${ key } `
118+ : `${ instanceName } .${ key } `
119+ : key ;
133120
134121 if ( prefixFn != null ) {
135122 prefix = prefixFn (
136123 {
137124 id : scopeId ,
138125 instance : this ,
139- instanceName : instanceName ,
126+ instanceName : instanceName ?? emptyStr ,
140127 name : key ,
141128 prefix : prefix ,
142129 } ,
@@ -157,7 +144,7 @@ export function log<T extends (...arg: any) => any>(options?: LogOptions<T>, deb
157144 loggableParams = emptyStr ;
158145
159146 if ( ! singleLine ) {
160- logFn ( `${ prefix } ${ enter } ` ) ;
147+ logFn . call ( Logger , `${ prefix } ${ enter } ` ) ;
161148 }
162149 } else {
163150 loggableParams = '' ;
@@ -190,20 +177,14 @@ export function log<T extends (...arg: any) => any>(options?: LogOptions<T>, deb
190177 loggableParams += ', ' ;
191178 }
192179
193- paramLogValue = Logger . toLoggable ( paramValue , sanitizeFn ) ;
180+ paramLogValue = Logger . toLoggable ( paramValue ) ;
194181 }
195182
196183 loggableParams += paramName ? `${ paramName } =${ paramLogValue } ` : paramLogValue ;
197184 }
198185
199186 if ( ! singleLine ) {
200- logFn (
201- `${ prefix } ${ enter } ${
202- loggableParams && ( debug || Logger . enabled ( 'debug' ) || Logger . isDebugging )
203- ? `(${ loggableParams } )`
204- : emptyStr
205- } `,
206- ) ;
187+ logFn . call ( Logger , loggableParams ? `${ prefix } ${ enter } (${ loggableParams } )` : `${ prefix } ${ enter } ` ) ;
207188 }
208189 }
209190
@@ -215,11 +196,15 @@ export function log<T extends (...arg: any) => any>(options?: LogOptions<T>, deb
215196 if ( singleLine ) {
216197 Logger . error (
217198 ex ,
218- `${ prefix } ${ enter } ${ loggableParams ? ` (${ loggableParams } )` : emptyStr } ` ,
219- `failed ${ scope ?. exitDetails ? scope . exitDetails : emptyStr } ${ timing } `,
199+ loggableParams ? `${ prefix } ${ enter } (${ loggableParams } )` : ` ${ prefix } ${ enter } `,
200+ scope ?. exitDetails ? `failed ${ scope . exitDetails } ${ timing } ` : `failed ${ timing } `,
220201 ) ;
221202 } else {
222- Logger . error ( ex , prefix , `failed${ scope ?. exitDetails ? scope . exitDetails : emptyStr } ${ timing } ` ) ;
203+ Logger . error (
204+ ex ,
205+ prefix ,
206+ scope ?. exitDetails ? `failed${ scope . exitDetails } ${ timing } ` : `failed${ timing } ` ,
207+ ) ;
223208 }
224209
225210 if ( scoped ) {
@@ -242,7 +227,7 @@ export function log<T extends (...arg: any) => any>(options?: LogOptions<T>, deb
242227 if ( start != null ) {
243228 duration = getDurationMilliseconds ( start ) ;
244229 if ( duration > slowCallWarningThreshold ) {
245- exitLogFn = warnFn ;
230+ exitLogFn = Logger . warn ;
246231 timing = ` [*${ duration } ms] (slow)` ;
247232 } else {
248233 exitLogFn = logFn ;
@@ -266,28 +251,28 @@ export function log<T extends (...arg: any) => any>(options?: LogOptions<T>, deb
266251 }
267252 } else if ( scope ?. exitFailed ) {
268253 exit = scope . exitFailed ;
269- exitLogFn = errorFn ;
254+ exitLogFn = Logger . error ;
270255 } else {
271256 exit = 'completed' ;
272257 }
273258
274259 if ( singleLine ) {
275260 if ( logThreshold === 0 || duration ! > logThreshold ) {
276- exitLogFn (
277- `${ prefix } ${ enter } ${
278- loggableParams && ( debug || Logger . enabled ( 'debug' ) || Logger . isDebugging )
279- ? `(${ loggableParams } )`
280- : emptyStr
281- } ${ exit } ${ scope ?. exitDetails ? scope . exitDetails : emptyStr } ${ timing } `,
261+ exitLogFn . call (
262+ Logger ,
263+ loggableParams
264+ ? `${ prefix } ${ enter } (${ loggableParams } ) ${ exit } ${
265+ scope ?. exitDetails || emptyStr
266+ } ${ timing } `
267+ : `${ prefix } ${ enter } ${ exit } ${ scope ?. exitDetails || emptyStr } ${ timing } ` ,
282268 ) ;
283269 }
284270 } else {
285- exitLogFn (
286- `${ prefix } ${
287- loggableParams && ( debug || Logger . enabled ( 'debug' ) || Logger . isDebugging )
288- ? `(${ loggableParams } )`
289- : emptyStr
290- } ${ exit } ${ scope ?. exitDetails ? scope . exitDetails : emptyStr } ${ timing } `,
271+ exitLogFn . call (
272+ Logger ,
273+ loggableParams
274+ ? `${ prefix } (${ loggableParams } ) ${ exit } ${ scope ?. exitDetails || emptyStr } ${ timing } `
275+ : `${ prefix } ${ exit } ${ scope ?. exitDetails || emptyStr } ${ timing } ` ,
291276 ) ;
292277 }
293278
@@ -297,8 +282,7 @@ export function log<T extends (...arg: any) => any>(options?: LogOptions<T>, deb
297282 } ;
298283
299284 if ( result != null && isPromise ( result ) ) {
300- const promise = result . then ( logResult ) ;
301- promise . catch ( logError ) ;
285+ result . then ( logResult , logError ) ;
302286 } else {
303287 logResult ( result ) ;
304288 }
0 commit comments