diff --git a/src/system/decorators/log.ts b/src/system/decorators/log.ts index 9d4bacc..c6f30b9 100644 --- a/src/system/decorators/log.ts +++ b/src/system/decorators/log.ts @@ -44,34 +44,34 @@ export function logName(fn: (c: T, name: string) => string) { export function debug( options: { - args?: boolean | { [arg: string]: (arg: any) => string }; - condition?(this: any, ...args: any[]): boolean; + args?: false | { [arg: string]: (arg: any) => string | false }; + condition?(...args: any[]): boolean; correlate?: boolean; - enter?(this: any, ...args: any[]): string; - exit?(this: any, result: any): string; - prefix?(this: any, context: LogContext, ...args: any[]): string; - sanitize?(this: any, key: string, value: any): any; + enter?(...args: any[]): string; + exit?(result: any): string; + prefix?(context: LogContext, ...args: any[]): string; + sanitize?(key: string, value: any): any; timed?: boolean; - } = { args: true, timed: true } + } = { timed: true } ) { return log({ debug: true, ...options }); } export function log( options: { - args?: boolean | { [arg: number]: (arg: any) => string }; - condition?(this: any, ...args: any[]): boolean; + args?: false | { [arg: number]: ((arg: any) => string | false) }; + condition?(...args: any[]): boolean; correlate?: boolean; debug?: boolean; - enter?(this: any, ...args: any[]): string; - exit?(this: any, result: any): string; - prefix?(this: any, context: LogContext, ...args: any[]): string; - sanitize?(this: any, key: string, value: any): any; + enter?(...args: any[]): string; + exit?(result: any): string; + prefix?(context: LogContext, ...args: any[]): string; + sanitize?(key: string, value: any): any; singleLine?: boolean; timed?: boolean; - } = { args: true, timed: true } + } = { timed: true } ) { - options = { args: true, timed: true, ...options }; + options = { timed: true, ...options }; const logFn = options.debug ? Logger.debug.bind(Logger) : Logger.log.bind(Logger); @@ -136,7 +136,7 @@ export function log( const enter = options.enter != null ? options.enter(...args) : ''; let loggableParams: string; - if (!options.args || args.length === 0) { + if (options.args === false || args.length === 0) { loggableParams = ''; if (!options.singleLine) { @@ -144,17 +144,25 @@ export function log( } } else { + const argFns = typeof options.args === 'object' ? options.args : undefined; + let argFn; + let loggable; loggableParams = args .map((v: any, index: number) => { const p = parameters[index]; - const loggable = - typeof options.args === 'object' && options.args[index] - ? options.args[index](v) - : Logger.toLoggable(v, options.sanitize); + argFn = argFns !== undefined ? argFns[index] : undefined; + if (argFn !== undefined) { + loggable = argFn(v); + if (loggable === false) return undefined; + } + else { + loggable = Logger.toLoggable(v, options.sanitize); + } return p ? `${p}=${loggable}` : loggable; }) + .filter(Boolean) .join(', '); if (!options.singleLine) {