JSPM

  • Created
  • Published
  • Downloads 853100
  • Score
    100M100P100Q176035F
  • License MIT

Logger for redux

Package Exports

  • redux-logger

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (redux-logger) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Logger for redux

Build Status

logger

Install

npm i --save redux-logger

Usage

import thunk from 'redux-thunk';
import promise from 'redux-promise';
import createLogger from 'redux-logger';

const logger = createLogger();
const createStoreWithMiddleware = applyMiddleware(thunk, promise, logger)(createStore);
const store = createStoreWithMiddleware(reducer);

Logger must be last middleware in chain, otherwise it will log thunk and promise, not actual actions (#20).

API

redux-logger exposes single constructor function for creating logger middleware.

createLogger(options?: Object)

Options

level (String)

Level of console. warn, error, info or else.

Default: console.log

logger (Object)

Implementation of the console API. Useful if you are using a custom, wrapped version of console.

Default: window.console

timestamp (Boolean)

Print timestamp with each action?

Default: true

duration (Boolean)

Print duration of each action?

Default: true

transformer (Function)

Transform state before print. Eg. convert Immutable object to plain JSON.

Default: identity function

predicate (getState: Function, action: Object): boolean

If specified this function will be called before each action is processed with this middleware. Receives getState function for accessing current store state and action object as parameters. Returns true if action should be logged, false otherwise.

Default: null (always log)

collapsed (getState: Function, action: Object): boolean

Takes a boolean or optionally a function that receives getState function for accessing current store state and action object as parameters. Returns true if the log group should be collapsed, false otherwise.

Default: false

Examples:
log only in dev mode
const __DEV__ = true;

createLogger({
  predicate: (getState, action) => __DEV__
});
log everything except actions with type AUTH_REMOVE_TOKEN
createLogger({
  predicate: (getState, action) => action.type !== AUTH_REMOVE_TOKEN
});
collapse all actions
createLogger({
  collapsed: true
});
collapse actions with type FORM_CHANGE
createLogger({
  collapsed: (getState, action) => action.type === FORM_CHANGE
});
transform Immutable objects into JSON
createLogger({
  transformer: (state) => {
    var newState = {};
    for (var i of Object.keys(state)) {
      if (Immutable.Iterable.isIterable(state[i])) {
        newState[i] = state[i].toJS();
      } else {
        newState[i] = state[i];
      }
    };
    return newState;
  }
});

License

MIT