Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ Logger **must be** last middleware in chain, otherwise it will log thunk and pro
__createLogger(options?: Object)__

### Options
#### __collapsed (Boolean)__
Is group collapsed?

*Default: `false`*

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

*Default: `null` (always log)*

#### __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`*

##### Examples:
###### log only in dev mode
```javascript
Expand All @@ -71,13 +72,26 @@ createLogger({
```

###### log everything except actions with type `AUTH_REMOVE_TOKEN`

```javascript
createLogger({
predicate: (getState, action) => action.type !== AUTH_REMOVE_TOKEN
});
```

###### collapse all actions
```javascript
createLogger({
collapsed: true
});
```

###### collapse actions with type `FORM_CHANGE`
```javascript
createLogger({
collapsed: (getState, action) => action.type === FORM_CHANGE
});
```

###### transform Immutable objects into JSON
```javascript
createLogger({
Expand Down
12 changes: 8 additions & 4 deletions src/createLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const timer = typeof performance !== `undefined` ? performance : Date;
* @property {object} options - options for logger
* @property {string} level - console[level]
* @property {boolean} collapsed - is group collapsed?
* @property {bool} predicate - condition which resolves logger behavior
* @property {boolean} predicate - condition which resolves logger behavior
*/

function createLogger(options = {}) {
Expand Down Expand Up @@ -45,7 +45,11 @@ function createLogger(options = {}) {
const actionType = String(action.type);
const message = `action ${actionType}${formattedTime}${formattedDuration}`;

if (collapsed) {
const isCollapsed = (typeof collapsed === 'function') ?
collapsed(getState, action) :
collapsed;

if (isCollapsed) {
try {
console.groupCollapsed(message);
} catch (e) {
Expand All @@ -54,7 +58,7 @@ function createLogger(options = {}) {
} else {
try {
console.group(message);
} catch(e) {
} catch (e) {
console.log(message);
}
}
Expand All @@ -71,7 +75,7 @@ function createLogger(options = {}) {

try {
console.groupEnd();
} catch(e) {
} catch (e) {
console.log('—— log end ——');
}

Expand Down
4 changes: 2 additions & 2 deletions src/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function logger({ getState }) {

try {
console.group(message);
} catch(e) {
} catch (e) {
console.log(message);
}

Expand All @@ -27,7 +27,7 @@ function logger({ getState }) {

try {
console.groupEnd();
} catch(e) {
} catch (e) {
console.log('—— log end ——');
}

Expand Down