import { Factory , MethodFactory , UnknownFactoryLevels } from './MethodFactory' ; import { PrefixFactory , PrefixFactoryOptions } from './PrefixFactory' ; type SomeLevel = number | string ; export interface LogLevelIndex { [ key ] : any ; } export interface LogLevelOptions { factory ? : Factory ; id ? : string ; level ? : number | string ; name ? : string ; prefix ? : PrefixFactoryOptions ; } const defaults = { factory : void 0 , level : 'warn' , name : ( + new Date ( ) ) . toString ( ) , prefix : void 0 } ; export class LogLevel implements LogLevelIndex { [ key ] : any ; public name : string ; public type : string ; private currentLevel ! : UnknownFactoryLevels [ keyof UnknownFactoryLevels ] ; private methodFactory : Factory | undefined ; private options : LogLevelOptions ; constructor ( options ) { this . type = 'LogLevel' ; this . options = Object . assign ( { } , defaults , options ) ; this . methodFactory = options . factory ; if ( ! ( this as any ) . methodFactory ) { const factory = options . prefix ? new PrefixFactory ( this as any , options . prefix ) : new MethodFactory ( this as any ) ; this . methodFactory = factory as any ; } if ( ! this . methodFactory ! . logger ) { this . methodFactory ! . logger = this ; } this . name = options . name || '<unknown>' ; this . level = this . options . level ?? 'trace' ; } get factory ( ) { return this . methodFactory as Factory ; } set factory ( factory ) { factory . logger = this ; this . methodFactory = factory ; this . methodFactory . replaceMethods ( this . level ) ; } disable ( ) { this . level = this . levels . SILENT ; } enable ( ) { this . level = this . levels . TRACE ; } get level ( ) : number { return this . currentLevel ; } set level ( logLevel ) { const level = this . methodFactory ! . distillLevel ( logLevel ) ; if ( level === false || level == null ) { throw new RangeError ( `loglevelnext: setLevel() called with invalid level: ${ logLevel }` ) ; } this . currentLevel = level ; this . methodFactory ! . replaceMethods ( level ) ; if ( typeof console === 'undefined' ) { process . stdout . write ( 'loglevelnext: console is undefined. The log will produce no output.\n' ) ; } else if ( level > this . levels . SILENT ) { console . warn ( `The log level has been set to a value greater than 'silent'. The log will produce no output.` ) ; } } get levels ( ) { return this . methodFactory ! . levels ; } }