Skip to content

Commit

Permalink
Merge pull request #18 from webpro/master
Browse files Browse the repository at this point in the history
Add timestamp option + pad hours/minutes
  • Loading branch information
Eugene Rodionov committed Aug 24, 2015
2 parents d107621 + 5eb79c0 commit c2bdb00
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ Transform state before print. Eg. convert Immutable object to plain JSON.

*Default: identity function*

#### __timestamp (Boolean)__
Print timestamp with each action?

*Default: `true`*


##### Examples:
###### log only in dev mode
Expand Down
24 changes: 17 additions & 7 deletions build/createLogger.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
'use strict';

Object.defineProperty(exports, '__esModule', {
value: true
});
var pad = function pad(num) {
return ('0' + num).slice(-2);
};

/**
* Creates logger with followed options
*
Expand All @@ -8,11 +17,6 @@
* @property {bool} predicate - condition which resolves logger behavior
*/

'use strict';

Object.defineProperty(exports, '__esModule', {
value: true
});
function createLogger() {
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];

Expand All @@ -28,6 +32,8 @@ function createLogger() {
var transformer = _options$transformer === undefined ? function (state) {
return state;
} : _options$transformer;
var _options$timestamp = options.timestamp;
var timestamp = _options$timestamp === undefined ? true : _options$timestamp;

var console = logger || window.console;

Expand All @@ -44,9 +50,13 @@ function createLogger() {
var prevState = transformer(getState());
var returnValue = next(action);
var nextState = transformer(getState());
var time = new Date();
var formattedTime = '';
if (timestamp) {
var time = new Date();
formattedTime = ' @ ' + time.getHours() + ':' + pad(time.getMinutes()) + ':' + pad(time.getSeconds());
}
var actionType = String(action.type);
var message = 'action ' + actionType + ' @ ' + time.getHours() + ':' + time.getMinutes() + ':' + time.getSeconds();
var message = 'action ' + actionType + formattedTime;

if (collapsed) {
try {
Expand Down
12 changes: 9 additions & 3 deletions src/createLogger.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const pad = num => ('0' + num).slice(-2);

/**
* Creates logger with followed options
*
Expand All @@ -10,7 +12,7 @@

function createLogger(options = {}) {
return ({ getState }) => (next) => (action) => {
const { level, collapsed, predicate, logger, transformer = state => state} = options;
const { level, collapsed, predicate, logger, transformer = state => state, timestamp = true} = options;
const console = logger || window.console;

// exit if console undefined
Expand All @@ -26,9 +28,13 @@ function createLogger(options = {}) {
const prevState = transformer(getState());
const returnValue = next(action);
const nextState = transformer(getState());
const time = new Date();
let formattedTime = '';
if (timestamp) {
const time = new Date();
formattedTime = ` @ ${time.getHours()}:${pad(time.getMinutes())}:${pad(time.getSeconds())}`;
}
const actionType = String(action.type);
const message = `action ${actionType} @ ${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}`;
const message = `action ${actionType}${formattedTime}`;

if (collapsed) {
try {
Expand Down

0 comments on commit c2bdb00

Please sign in to comment.