redux-logger vs redux-devtools-extension vs redux-devtools
Redux 相关工具包
redux-loggerredux-devtools-extensionredux-devtools类似的npm包:
Redux 相关工具包

这些工具包为 Redux 状态管理提供了调试和日志记录功能,帮助开发者更好地理解和控制应用程序的状态变化。通过可视化工具和日志记录,开发者能够追踪状态的变化,识别潜在问题,从而提高开发效率和应用的可维护性。它们各自有不同的功能和使用场景,适用于不同的开发需求。

npm下载趋势
3 年
GitHub Stars 排名
统计详情
npm包名称
下载量
Stars
大小
Issues
发布时间
License
redux-logger1,010,8705,741-589 年前MIT
redux-devtools-extension887,17613,487-2655 年前MIT
redux-devtools67,50214,303-2305 年前MIT
功能对比: redux-logger vs redux-devtools-extension vs redux-devtools

调试功能

  • redux-logger:

    redux-logger 通过在控制台中记录每个动作及其对应的状态变化,帮助开发者快速了解应用的状态流动,适合于简单的调试需求。

  • redux-devtools-extension:

    redux-devtools-extension 作为浏览器扩展,提供了直观的用户界面,允许开发者通过图形化界面查看和修改状态,支持时间旅行和状态快照功能,便于快速定位问题。

  • redux-devtools:

    redux-devtools 提供了强大的调试功能,可以实时查看 Redux 状态树的变化,支持时间旅行调试,允许开发者回溯到之前的状态,极大地提高了调试效率。

集成方式

  • redux-logger:

    redux-logger 作为中间件集成在 Redux 流程中,简单易用,适合于需要快速记录状态变化的开发者。

  • redux-devtools-extension:

    redux-devtools-extension 直接集成于浏览器,提供更流畅的用户体验,适合于开发过程中频繁调试的开发者。

  • redux-devtools:

    redux-devtools 作为独立工具,可以与任何 Redux 应用集成,适合于需要跨浏览器或跨平台调试的场景。

性能影响

  • redux-logger:

    redux-logger 可能会对性能产生一定影响,尤其是在状态变化频繁的情况下,因为它会记录每个动作和状态变化,适合于开发环境使用,而不建议在生产环境中使用。

  • redux-devtools-extension:

    redux-devtools-extension 的性能影响也较小,但在大量状态变化时可能会导致浏览器的性能下降,特别是在低性能设备上。

  • redux-devtools:

    redux-devtools 的性能影响较小,主要在于调试时的状态快照和时间旅行功能,通常不会对应用的性能产生显著影响。

使用场景

  • redux-logger:

    适用于简单的应用或开发阶段,帮助开发者快速了解状态变化,适合于不需要复杂调试功能的场景。

  • redux-devtools-extension:

    适用于使用 Chrome 或 Firefox 浏览器的开发者,提供更好的用户体验和调试功能,适合于需要实时监控状态变化的应用。

  • redux-devtools:

    适用于需要深入调试和分析 Redux 状态的复杂应用,尤其是在开发阶段,能够帮助开发者快速定位问题。

学习曲线

  • redux-logger:

    redux-logger 的使用非常简单,作为中间件集成到 Redux 中,适合于快速上手和简单调试需求的开发者。

  • redux-devtools-extension:

    redux-devtools-extension 的使用也比较简单,开发者只需安装浏览器扩展并进行简单配置即可使用,适合于所有级别的开发者。

  • redux-devtools:

    redux-devtools 的学习曲线相对平缓,开发者只需了解基本的 Redux 状态管理即可上手,适合于初学者。

如何选择: redux-logger vs redux-devtools-extension vs redux-devtools
  • redux-logger:

    选择 redux-logger 如果你需要一个简单的中间件来记录 Redux 状态的变化,适合于快速调试和查看状态变化的历史记录。

  • redux-devtools-extension:

    选择 redux-devtools-extension 如果你希望在 Chrome 或 Firefox 浏览器中使用 Redux DevTools 扩展,提供更好的集成和用户体验,适合需要实时监控状态变化的开发者。

  • redux-devtools:

    选择 redux-devtools 如果你需要一个独立的调试工具,可以在浏览器中使用,适合于对 Redux 状态进行深入分析和调试。

redux-logger的README

Logger for Redux

npm npm Build Status

redux-logger

Table of contents

Install

npm i --save redux-logger

Usage

import { applyMiddleware, createStore } from 'redux';

// Logger with default options
import logger from 'redux-logger'
const store = createStore(
  reducer,
  applyMiddleware(logger)
)

// Note passing middleware as the third argument requires redux@>=3.1.0

Or you can create your own logger with custom options:

import { applyMiddleware, createStore } from 'redux';
import { createLogger } from 'redux-logger'

const logger = createLogger({
  // ...options
});

const store = createStore(
  reducer,
  applyMiddleware(logger)
);

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

Options

{
  predicate, // if specified this function will be called before each action is processed with this middleware.
  collapsed, // 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.
  duration = false: Boolean, // print the duration of each action?
  timestamp = true: Boolean, // print the timestamp with each action?

  level = 'log': 'log' | 'console' | 'warn' | 'error' | 'info', // console's level
  colors: ColorsObject, // colors for title, prev state, action and next state: https://github.com/evgenyrodionov/redux-logger/blob/master/src/defaults.js#L12-L18
  titleFormatter, // Format the title used when logging actions.

  stateTransformer, // Transform state before print. Eg. convert Immutable object to plain JSON.
  actionTransformer, // Transform action before print. Eg. convert Immutable object to plain JSON.
  errorTransformer, // Transform error before print. Eg. convert Immutable object to plain JSON.

  logger = console: LoggerObject, // implementation of the `console` API.
  logErrors = true: Boolean, // should the logger catch, log, and re-throw errors?

  diff = false: Boolean, // (alpha) show diff between states?
  diffPredicate // (alpha) filter function for showing states diff, similar to `predicate`
}

Options description

level (String | Function | Object)

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

It can be a function (action: Object) => level: String.

It can be an object with level string for: prevState, action, nextState, error

It can be an object with getter functions: prevState, action, nextState, error. Useful if you want to print message based on specific state or action. Set any of them to false if you want to hide it.

  • prevState(prevState: Object) => level: String
  • action(action: Object) => level: String
  • nextState(nextState: Object) => level: String
  • error(error: Any, prevState: Object) => level: String

Default: log

duration (Boolean)

Print duration of each action?

Default: false

timestamp (Boolean)

Print timestamp with each action?

Default: true

colors (Object)

Object with color getter functions: title, prevState, action, nextState, error. Useful if you want to paint message based on specific state or action. Set any of them to false if you want to 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
  • error(error: Any, prevState: Object) => color: String

logger (Object)

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

Default: console

logErrors (Boolean)

Should the logger catch, log, and re-throw errors? This makes it clear which action triggered the error but makes "break on error" in dev tools harder to use, as it breaks on re-throw rather than the original throw location.

Default: true

collapsed = (getState: Function, action: Object, logEntry: 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: Object) => action

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

Default: identity function

errorTransformer = (error: Any) => error

Transform error before print.

Default: identity function

titleFormatter = (action: Object, time: String?, took: Number?) => title

Format the title used for each action.

Default: prints something like action @ ${time} ${action.type} (in ${took.toFixed(2)} ms)

diff (Boolean)

Show states diff.

Default: false

diffPredicate = (getState: Function, action: Object) => Boolean

Filter states diff for certain cases.

Default: undefined

Recipes

Log only in development

const middlewares = [];

if (process.env.NODE_ENV === `development`) {
  const { logger } = require(`redux-logger`);

  middlewares.push(logger);
}

const store = compose(applyMiddleware(...middlewares))(createStore)(reducer);

Log everything except actions with certain type

createLogger({
  predicate: (getState, action) => action.type !== AUTH_REMOVE_TOKEN
});

Collapse actions with certain type

createLogger({
  collapsed: (getState, action) => action.type === FORM_CHANGE
});

Collapse actions that don't have errors

createLogger({
  collapsed: (getState, action, logEntry) => !logEntry.error
});

Transform Immutable (without combineReducers)

import { Iterable } from 'immutable';

const stateTransformer = (state) => {
  if (Iterable.isIterable(state)) return state.toJS();
  else return state;
};

const logger = createLogger({
  stateTransformer,
});

Transform Immutable (with combineReducers)

const logger = 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;
  }
});

Log batched actions

Thanks to @smashercosmo

import { createLogger } from 'redux-logger';

const actionTransformer = action => {
  if (action.type === 'BATCHING_REDUCER.BATCH') {
    action.payload.type = action.payload.map(next => next.type).join(' => ');
    return action.payload;
  }

  return action;
};

const level = 'info';

const logger = {};

for (const method in console) {
  if (typeof console[method] === 'function') {
    logger[method] = console[method].bind(console);
  }
}

logger[level] = function levelFn(...args) {
  const lastArg = args.pop();

  if (Array.isArray(lastArg)) {
    return lastArg.forEach(item => {
      console[level].apply(console, [...args, item]);
    });
  }

  console[level].apply(console, arguments);
};

export default createLogger({
  level,
  actionTransformer,
  logger
});

To Do

  • Update eslint config to airbnb's
  • Clean up code, because it's very messy, to be honest
  • Write tests
  • Node.js support
  • React-native support

Feel free to create PR for any of those tasks!

Known issues

  • Performance issues in react-native (#32)

License

MIT