What is redux-logger?
redux-logger is a middleware for Redux that logs actions and state changes to the console, making it easier to debug and understand the flow of data in your application.
What are redux-logger's main functionalities?
Basic Logging
This feature logs every action and the resulting new state to the console. It helps developers see what actions are being dispatched and how the state is changing in response.
const { createStore, applyMiddleware } = require('redux');
const logger = require('redux-logger').createLogger();
const reducer = (state = {}, action) => state;
const store = createStore(reducer, applyMiddleware(logger));
store.dispatch({ type: 'TEST_ACTION' });
Collapsed Logging
This feature collapses the log entries for each action, making the console output more compact and easier to navigate, especially when dealing with a large number of actions.
const { createStore, applyMiddleware } = require('redux');
const { createLogger } = require('redux-logger');
const logger = createLogger({ collapsed: true });
const reducer = (state = {}, action) => state;
const store = createStore(reducer, applyMiddleware(logger));
store.dispatch({ type: 'TEST_ACTION' });
Predicate Logging
This feature allows you to filter which actions get logged based on a predicate function. In this example, actions of type 'IGNORED_ACTION' will not be logged.
const { createStore, applyMiddleware } = require('redux');
const { createLogger } = require('redux-logger');
const logger = createLogger({ predicate: (getState, action) => action.type !== 'IGNORED_ACTION' });
const reducer = (state = {}, action) => state;
const store = createStore(reducer, applyMiddleware(logger));
store.dispatch({ type: 'TEST_ACTION' });
store.dispatch({ type: 'IGNORED_ACTION' });
Other packages similar to redux-logger
redux-devtools-extension
redux-devtools-extension is a package that integrates Redux with the Redux DevTools browser extension. It provides a more visual and interactive way to inspect actions and state changes, including time-travel debugging, which is not available in redux-logger.
redux-immutable-state-invariant
redux-immutable-state-invariant is a middleware that logs warnings when the state is mutated. While it doesn't log actions and state changes like redux-logger, it helps catch bugs related to state mutations, which can be a complementary tool for debugging Redux applications.
Logger for Redux
![Build Status](https://travis-ci.org/fcomb/redux-logger.svg?branch=master)
![redux-logger](http://i.imgur.com/LDgv4tp.png)
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 3 functions: 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.
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