import { LogLevel } from './LogLevel' ; import { MethodFactory } from './MethodFactory' ; export interface PrefixTemplateOptions { level : string ; logger : LogLevel ; } export type PrefixTemplateFn = ( options : PrefixTemplateOptions ) => string ; export interface PrefixFactoryOptions { [ key ] : PrefixTemplateFn | string | undefined ; level ? : PrefixTemplateFn ; name ? : PrefixTemplateFn ; template ? : string ; time ? : PrefixTemplateFn ; } const defaults = { level : ( opts ) => `[${ opts . level }]` , name : ( opts ) => opts . logger . name , template : '{{time}} {{level}} ' , time : ( ) => new Date ( ) . toTimeString ( ) . split ( ' ' ) [ 0 ] } ; export class PrefixFactory extends MethodFactory { private options : PrefixFactoryOptions ; constructor ( logger ? , options ? ) { super ( logger ) ; this . options = Object . assign ( { } , defaults , options ) ; } interpolate ( level ) { return this . options . template ! . replace ( /{{([^{}]*)}}/g , ( stache , prop ) => { const fn = this . options [ prop ] ; if ( typeof fn === 'function' ) { return fn ( { level , logger : this . logger } ) ; } return stache ; } ) ; } make ( methodName ) { const og = super . make ( methodName ) ; return ( ... args ) => { const output = this . interpolate ( methodName ) ; const [ first ] = args ; if ( typeof first === 'string' ) { args [ 0 ] = output + first ; } else { args . unshift ( output ) ; } og ( ... args ) ; } ; } }