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

redux-logger

Install

npm i --save redux-logger

Usage

import { applyMiddleware, createStore } from 'redux';
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: log

duration (Boolean)

Print duration of each action?

Default: false

timestamp (Boolean)

Print timestamp with each action?

Default: true

colors (Object)

Object with 4 functions: title, prevState, action, nextState. Useful if you want paint message based on specific state or action. It also can be false if you want show plain message without colors.

  • title(action: Object) => color: String
  • prevState(prevState: Object) => color: String
  • action(action: Object) => color: String
  • nextState(nextState: Object) => color: String

logger (Object)

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

Default: window.console

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

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)

stateTransformer = (state: Object) => state

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

Default: identity function

actionTransformer = (action: Function) => action

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

Default: identity function

Examples:

log only in dev mode

createLogger({
  predicate: (getState, action) => process.env.NODE_ENV === `development`
});

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({
  stateTransformer: (state) => {
    let 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