Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@jarvisaoieong/redux-logger
Advanced tools
npm i --save redux-logger
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).
redux-logger
exposes single constructor function for creating logger middleware.
createLogger(options?: Object) => LoggerMiddleware
Level of console
. warn
, error
, info
or else.
Default: log
Print duration of each action?
Default: false
Print timestamp with each action?
Default: true
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
Implementation of the console
API. Useful if you are using a custom, wrapped version of console
.
Default: window.console
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
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
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)
Transform state before print. Eg. convert Immutable object to plain JSON.
Default: identity function
Transform action before print. Eg. convert Immutable object to plain JSON.
Default: identity function
Transform error before print.
Default: identity function
import thunk from 'redux-thunk';
const middlewares = [thunk];
if (process.env.NODE_ENV === `development`) {
const createLogger = require(`redux-logger`);
const logger = createLogger();
middlewares.push(logger);
}
const store = compose(applyMiddleware(...middlewares))(createStore)(reducer);
AUTH_REMOVE_TOKEN
createLogger({
predicate: (getState, action) => action.type !== AUTH_REMOVE_TOKEN
});
FORM_CHANGE
createLogger({
collapsed: (getState, action) => action.type === FORM_CHANGE
});
const stateTransformer = (state) => {
let newState = {};
if (typeof state === "object" && state !== null && Object.keys(state).length) {
for (var i of Object.keys(state)) {
if (Immutable.Iterable.isIterable(state[i])) {
newState[i] = state[i].toJS();
} else {
newState[i] = stateTransformer(state[i]);
}
}
} else {
newState = state;
}
return newState;
}
const logger = createLogger({
stateTransformer,
});
Thanks to @smashercosmo
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
});
MIT
FAQs
Logger for redux
We found that @jarvisaoieong/redux-logger demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.