Installations
npm install redux-logger
Releases
3.0.6 — fixes
Published on 17 May 2017
[BROKEN] 3.0.2 — chore: webpack → rollup
Published on 17 May 2017
3.0.1 — fixed bug with custom titleFormatter
Published on 30 Mar 2017
3.0.0 — breaking change with exports
Published on 23 Mar 2017
2.10.2 — fixed require()
Published on 21 Mar 2017
2.10.0 — improved readability
Published on 21 Mar 2017
Contributors
Developer
theaqua
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
No
Node Version
7.2.0
NPM Version
4.5.0
Statistics
5,760 Stars
294 Commits
320 Forks
61 Watching
2 Branches
49 Contributors
Updated on 26 Nov 2024
Bundle Size
10.20 kB
Minified
3.54 kB
Minified + Gzipped
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
264,864,537
Last day
-11%
153,780
Compared to previous day
Last week
-0.9%
856,988
Compared to previous week
Last month
9.9%
3,642,474
Compared to previous month
Last year
-1.3%
41,625,115
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Dev Dependencies
23
Logger for Redux
Now maintained by LogRocket!
LogRocket is a production Redux logging tool that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay Redux actions + state, network requests, console logs, and see a video of what the user saw.
For more informatiom about the future of redux-logger, check out the discussion here.
Table of contents
Install
npm i --save redux-logger
Typescript types are also available, via DefinitelyTyped:
npm i @types/redux-logger
Usage
1import { applyMiddleware, createStore } from 'redux'; 2 3// Logger with default options 4import logger from 'redux-logger' 5const store = createStore( 6 reducer, 7 applyMiddleware(logger) 8) 9 10// Note passing middleware as the third argument requires redux@>=3.1.0
Or you can create your own logger with custom options:
1import { applyMiddleware, createStore } from 'redux'; 2import { createLogger } from 'redux-logger' 3 4const logger = createLogger({ 5 // ...options 6}); 7 8const store = createStore( 9 reducer, 10 applyMiddleware(logger) 11);
Note: logger must be the last middleware in chain, otherwise it will log thunk and promise, not actual actions (#20).
Options
1{ 2 predicate, // if specified this function will be called before each action is processed with this middleware. 3 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. 4 duration = false: Boolean, // print the duration of each action? 5 timestamp = true: Boolean, // print the timestamp with each action? 6 7 level = 'log': 'log' | 'console' | 'warn' | 'error' | 'info', // console's level 8 colors: ColorsObject, // colors for title, prev state, action and next state: https://github.com/LogRocket/redux-logger/blob/master/src/defaults.js#L12-L18 9 titleFormatter, // Format the title used when logging actions. 10 11 stateTransformer, // Transform state before print. Eg. convert Immutable object to plain JSON. 12 actionTransformer, // Transform action before print. Eg. convert Immutable object to plain JSON. 13 errorTransformer, // Transform error before print. Eg. convert Immutable object to plain JSON. 14 15 logger = console: LoggerObject, // implementation of the `console` API. 16 logErrors = true: Boolean, // should the logger catch, log, and re-throw errors? 17 18 diff = false: Boolean, // (alpha) show diff between states? 19 diffPredicate // (alpha) filter function for showing states diff, similar to `predicate` 20}
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
1const middlewares = []; 2 3if (process.env.NODE_ENV === `development`) { 4 const { logger } = require(`redux-logger`); 5 6 middlewares.push(logger); 7} 8 9const store = compose(applyMiddleware(...middlewares))(createStore)(reducer);
Log everything except actions with certain type
1createLogger({ 2 predicate: (getState, action) => action.type !== AUTH_REMOVE_TOKEN 3});
Collapse actions with certain type
1createLogger({ 2 collapsed: (getState, action) => action.type === FORM_CHANGE 3});
Collapse actions that don't have errors
1createLogger({ 2 collapsed: (getState, action, logEntry) => !logEntry.error 3});
Transform Immutable (without combineReducers
)
1import { Iterable } from 'immutable'; 2 3const stateTransformer = (state) => { 4 if (Iterable.isIterable(state)) return state.toJS(); 5 else return state; 6}; 7 8const logger = createLogger({ 9 stateTransformer, 10});
Transform Immutable (with combineReducers
)
1const logger = createLogger({ 2 stateTransformer: (state) => { 3 let newState = {}; 4 5 for (var i of Object.keys(state)) { 6 if (Immutable.Iterable.isIterable(state[i])) { 7 newState[i] = state[i].toJS(); 8 } else { 9 newState[i] = state[i]; 10 } 11 }; 12 13 return newState; 14 } 15});
Log batched actions
Thanks to @smashercosmo
1import { createLogger } from 'redux-logger'; 2 3const actionTransformer = action => { 4 if (action.type === 'BATCHING_REDUCER.BATCH') { 5 action.payload.type = action.payload.map(next => next.type).join(' => '); 6 return action.payload; 7 } 8 9 return action; 10}; 11 12const level = 'info'; 13 14const logger = {}; 15 16for (const method in console) { 17 if (typeof console[method] === 'function') { 18 logger[method] = console[method].bind(console); 19 } 20} 21 22logger[level] = function levelFn(...args) { 23 const lastArg = args.pop(); 24 25 if (Array.isArray(lastArg)) { 26 return lastArg.forEach(item => { 27 console[level].apply(console, [...args, item]); 28 }); 29 } 30 31 console[level].apply(console, arguments); 32}; 33 34export default createLogger({ 35 level, 36 actionTransformer, 37 logger 38});
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
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 10/25 approved changesets -- score normalized to 4
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 15 are checked with a SAST tool
Reason
67 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-6chw-6frg-f759
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-832h-xg76-4gv6
- Warn: Project is vulnerable to: GHSA-cwfw-4gq5-mrqx
- Warn: Project is vulnerable to: GHSA-g95f-p29q-9xw4
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-mh2h-6j8q-x246
- Warn: Project is vulnerable to: GHSA-5q88-cjfq-g2mh / GHSA-xp63-6vf5-xf3v
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-9vvw-cc9w-f27h
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-h6ch-v84p-w6p9
- Warn: Project is vulnerable to: GHSA-pm9p-9926-w68m
- Warn: Project is vulnerable to: GHSA-9q64-mpxx-87fg
- Warn: Project is vulnerable to: GHSA-jc84-3g44-wf2q
- Warn: Project is vulnerable to: GHSA-4gmj-3p3h-gm8h
- Warn: Project is vulnerable to: GHSA-74fj-2j2h-c42q
- Warn: Project is vulnerable to: GHSA-pw2r-vq6v-hr8c
- Warn: Project is vulnerable to: GHSA-jchw-25xp-jwwc
- Warn: Project is vulnerable to: GHSA-cxjh-pqwp-8mfp
- Warn: Project is vulnerable to: GHSA-qh2h-chj9-jffq
- Warn: Project is vulnerable to: GHSA-q42p-pg8m-cqh6
- Warn: Project is vulnerable to: GHSA-w457-6q6x-cgp9
- Warn: Project is vulnerable to: GHSA-62gr-4qp9-h98f
- Warn: Project is vulnerable to: GHSA-f52g-6jhx-586p
- Warn: Project is vulnerable to: GHSA-2cf5-4w76-r9qv
- Warn: Project is vulnerable to: GHSA-3cqr-58rm-57f8
- Warn: Project is vulnerable to: GHSA-g9r4-xpmj-mj65
- Warn: Project is vulnerable to: GHSA-q2c6-c6pm-g3gh
- Warn: Project is vulnerable to: GHSA-765h-qjxv-5f44
- Warn: Project is vulnerable to: GHSA-f2jv-r9rf-7988
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
- Warn: Project is vulnerable to: GHSA-6x33-pw7p-hmpq
- Warn: Project is vulnerable to: GHSA-8j8c-7jfh-h6hx
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-282f-qqgm-c34q
- Warn: Project is vulnerable to: GHSA-6c8f-qphg-qjgp
- Warn: Project is vulnerable to: GHSA-fvqr-27wr-82fm
- Warn: Project is vulnerable to: GHSA-4xc9-xhrj-v574
- Warn: Project is vulnerable to: GHSA-x5rq-j2xg-h7qm
- Warn: Project is vulnerable to: GHSA-jf85-cpcp-j695
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m / GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-fhjf-83wg-r2j9
- Warn: Project is vulnerable to: GHSA-w9mr-4mfr-499f
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-gqgv-6jq5-jjj9
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-6g33-f262-xjp4
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-4g88-fppr-53pp
- Warn: Project is vulnerable to: GHSA-4jqc-8m5r-9rpr
- Warn: Project is vulnerable to: GHSA-4rq4-32rv-6wp6
- Warn: Project is vulnerable to: GHSA-64g7-mvw6-v9qj
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
- Warn: Project is vulnerable to: GHSA-p9pc-299p-vxgp
Score
2.6
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More