import printBuffer , { LogEntry , LogLevel } from './core' ; import { timer } from './util/helpers' ; import defaults from './defaults' ; export interface Options { level ? : LogLevel ; duration ? : boolean ; timestamp ? : boolean ; colors ? : object ; logger ? : object ; logErrors ? : boolean | Function ; collapsed ? : Function ; predicate ? : Function ; diff ? : boolean ; diffPredicate ? : Function ; stateTransformer ? : Function ; actionTransformer ? : Function ; errorTransformer ? : Function ; } namespace param { Options } options param { LogLevel } options . level param { boolean } options . duration param { boolean } options . timestamp param { Object } options . colors param { Object } options . logger param { Function } options . logErrors param { Function } options . collapsed param { Function } options . predicate param { Function } options . stateTransformer param { Function } options . actionTransformer param { Function } options . errorTransformer returns { Function } const createLogger = ( options ? ) => { const loggerOptions = { ... defaults , ... options } ; const { logger , stateTransformer , errorTransformer , predicate , logErrors , diffPredicate , } = loggerOptions ; if ( typeof logger === 'undefined' ) { return ( next ) => ( ) => next ( null ) ; } const logBuffer = [ ] ; return ( next ) => ( { prevState , nextState , action , } ) => { if ( typeof predicate === 'function' && ! predicate ( { prevState , nextState } , action ) ) { return next ( action ) ; } const logEntry = { } ; logBuffer . push ( logEntry ) ; logEntry . started = timer . now ( ) ; logEntry . startedTime = new Date ( ) ; logEntry . prevState = stateTransformer ( prevState ) ; logEntry . action = action ; let returnedValue ; if ( logErrors ) { try { returnedValue = next ( action ) ; } catch ( e ) { logEntry . error = errorTransformer ( e ) ; } } else { returnedValue = next ( action ) ; } logEntry . took = timer . now ( ) - logEntry . started ; logEntry . nextState = stateTransformer ( nextState ) ; const diff = loggerOptions . diff && typeof diffPredicate === 'function' ? diffPredicate ( nextState , action ) : loggerOptions . diff ; printBuffer ( logBuffer , { ... loggerOptions , diff } ) ; logBuffer . length = 0 ; if ( logEntry . error ) throw logEntry . error ; return returnedValue ; } ; } ; const defaultLogger = createLogger ( ) ; export { defaults , createLogger , defaultLogger as logger } ; export default defaultLogger ;