Skip to content

Commit c2bdb00

Browse files
author
Eugene Rodionov
committed
Merge pull request #18 from webpro/master
Add timestamp option + pad hours/minutes
2 parents d107621 + 5eb79c0 commit c2bdb00

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ Transform state before print. Eg. convert Immutable object to plain JSON.
4747

4848
*Default: identity function*
4949

50+
#### __timestamp (Boolean)__
51+
Print timestamp with each action?
52+
53+
*Default: `true`*
54+
5055

5156
##### Examples:
5257
###### log only in dev mode

build/createLogger.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, '__esModule', {
4+
value: true
5+
});
6+
var pad = function pad(num) {
7+
return ('0' + num).slice(-2);
8+
};
9+
110
/**
211
* Creates logger with followed options
312
*
@@ -8,11 +17,6 @@
817
* @property {bool} predicate - condition which resolves logger behavior
918
*/
1019

11-
'use strict';
12-
13-
Object.defineProperty(exports, '__esModule', {
14-
value: true
15-
});
1620
function createLogger() {
1721
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
1822

@@ -28,6 +32,8 @@ function createLogger() {
2832
var transformer = _options$transformer === undefined ? function (state) {
2933
return state;
3034
} : _options$transformer;
35+
var _options$timestamp = options.timestamp;
36+
var timestamp = _options$timestamp === undefined ? true : _options$timestamp;
3137

3238
var console = logger || window.console;
3339

@@ -44,9 +50,13 @@ function createLogger() {
4450
var prevState = transformer(getState());
4551
var returnValue = next(action);
4652
var nextState = transformer(getState());
47-
var time = new Date();
53+
var formattedTime = '';
54+
if (timestamp) {
55+
var time = new Date();
56+
formattedTime = ' @ ' + time.getHours() + ':' + pad(time.getMinutes()) + ':' + pad(time.getSeconds());
57+
}
4858
var actionType = String(action.type);
49-
var message = 'action ' + actionType + ' @ ' + time.getHours() + ':' + time.getMinutes() + ':' + time.getSeconds();
59+
var message = 'action ' + actionType + formattedTime;
5060

5161
if (collapsed) {
5262
try {

src/createLogger.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const pad = num => ('0' + num).slice(-2);
2+
13
/**
24
* Creates logger with followed options
35
*
@@ -10,7 +12,7 @@
1012

1113
function createLogger(options = {}) {
1214
return ({ getState }) => (next) => (action) => {
13-
const { level, collapsed, predicate, logger, transformer = state => state} = options;
15+
const { level, collapsed, predicate, logger, transformer = state => state, timestamp = true} = options;
1416
const console = logger || window.console;
1517

1618
// exit if console undefined
@@ -26,9 +28,13 @@ function createLogger(options = {}) {
2628
const prevState = transformer(getState());
2729
const returnValue = next(action);
2830
const nextState = transformer(getState());
29-
const time = new Date();
31+
let formattedTime = '';
32+
if (timestamp) {
33+
const time = new Date();
34+
formattedTime = ` @ ${time.getHours()}:${pad(time.getMinutes())}:${pad(time.getSeconds())}`;
35+
}
3036
const actionType = String(action.type);
31-
const message = `action ${actionType} @ ${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}`;
37+
const message = `action ${actionType}${formattedTime}`;
3238

3339
if (collapsed) {
3440
try {

0 commit comments

Comments
 (0)