Skip to content

Instantly share code, notes, and snippets.

@smashercosmo
Last active April 17, 2019 13:06
Show Gist options
  • Save smashercosmo/6dc9c26ce1e2e3dafde5 to your computer and use it in GitHub Desktop.
Save smashercosmo/6dc9c26ce1e2e3dafde5 to your computer and use it in GitHub Desktop.
Batch actions logger
import createLogger from 'redux-logger';
const actionTransformer = action => {
if (action.type === 'BATCHING_REDUCER.BATCH') {
action.payload.type = action.payload.reduce((result, next) => {
const prefix = result ? result + ' => ' : '';
return prefix + next.type;
}, '');
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
});
import {enableBatching} from 'redux-batched-actions';
import {combineReducers} from 'redux';
import reducer1 from './reducer1';
import reducer2 from './reducer2';
import reducer3 from './reducer3';
const appReducer = combineReducers({
reducer1,
reducer2,
reducer3
});
export default enableBatching(appReducer);
import reducer from './reducers';
import {createStore, applyMiddleware, compose} from 'redux';
const middlewares = [];
if (process.env.NODE_ENV === 'development') {
const logger = require('./logger');
middlewares.push(logger);
}
const storeEnhancers = [applyMiddleware(...middlewares)];
export default const store = compose(...storeEnhancers)(createStore)(reducer);
@NickSevens
Copy link

This seems to be out there for a long time, but this is awesome! Thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment