Skip to content

Commit ee51803

Browse files
committed
collapsed can take function
1 parent ad4dc0a commit ee51803

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

README.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ Logger **must be** last middleware in chain, otherwise it will log thunk and pro
2424
__createLogger(options?: Object)__
2525

2626
### Options
27-
#### __collapsed (Boolean)__
28-
Is group collapsed?
29-
30-
*Default: `false`*
3127

3228
#### __level (String)__
3329
Level of `console`. `warn`, `error`, `info` or [else](https://developer.mozilla.org/en/docs/Web/API/console).
@@ -60,6 +56,11 @@ Receives `getState` function for accessing current store state and `action` obj
6056

6157
*Default: `null` (always log)*
6258

59+
#### __collapsed (getState: Function, action: Object): boolean__
60+
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.
61+
62+
*Default: `false`*
63+
6364
##### Examples:
6465
###### log only in dev mode
6566
```javascript
@@ -71,13 +72,26 @@ createLogger({
7172
```
7273

7374
###### log everything except actions with type `AUTH_REMOVE_TOKEN`
74-
7575
```javascript
7676
createLogger({
7777
predicate: (getState, action) => action.type !== AUTH_REMOVE_TOKEN
7878
});
7979
```
8080

81+
###### collapse all actions
82+
```javascript
83+
createLogger({
84+
collapsed: true
85+
});
86+
```
87+
88+
###### collapse actions with type `FORM_CHANGE`
89+
```javascript
90+
createLogger({
91+
collapsed: (getState, action) => action.type === FORM_CHANGE
92+
});
93+
```
94+
8195
###### transform Immutable objects into JSON
8296
```javascript
8397
createLogger({

src/createLogger.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ function createLogger(options = {}) {
4545
const actionType = String(action.type);
4646
const message = `action ${actionType}${formattedTime}${formattedDuration}`;
4747

48-
if (collapsed) {
48+
const isCollapsed = (typeof collapsed === 'function') ?
49+
collapsed(getState, action) :
50+
collapsed;
51+
52+
if (isCollapsed) {
4953
try {
5054
console.groupCollapsed(message);
5155
} catch (e) {

0 commit comments

Comments
 (0)