import loglevel from 'loglevel' ; import { isDefinedAndNotNull } from './utils' ; type LogLevelMethodNames = 'debug' | 'info' | 'warn' | 'error' ; export type Logger = Record < LogLevelMethodNames , loglevel . LoggingMethod > ; let loggerId = 0 ; export default function logger ( { logger , level , } = { } ) { const levels = [ 'debug' , 'info' , 'warn' , 'error' ] ; const loglevelLogger = loglevel . getLogger ( String ( ( loggerId += 1 ) ) ) ; if ( isDefinedAndNotNull ( level ) ) loglevelLogger . setLevel ( level ) ; const log = { debug : ( ... msg ) : void => loglevelLogger . debug ( ... msg ) , info : ( ... msg ) : void => loglevelLogger . info ( ... msg ) , warn : ( ... msg ) : void => loglevelLogger . warn ( ... msg ) , error : ( ... msg ) : void => loglevelLogger . error ( ... msg ) , } ; if ( isDefinedAndNotNull ( logger ) ) { levels . forEach ( ( loggerLevel ) => { if ( logger [ loggerLevel ] !== undefined ) { log [ loggerLevel ] = ( ... msg ) : void => logger [ loggerLevel ] ( ... msg ) ; } } ) ; } return log ; }