Skip to content

Commit

Permalink
feat: log duration of action
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugene Rodionov committed Sep 15, 2015
1 parent 72641c0 commit 567af7b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
17 changes: 14 additions & 3 deletions build/createLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ var pad = function pad(num) {
return ('0' + num).slice(-2);
};

// Use the new performance api to get better precision if available
var timer = typeof performance !== 'undefined' ? performance : Date;

/**
* Creates logger with followed options
*
* @namespace
* @propety {object} options - options for logger
* @property {object} options - options for logger
* @property {string} level - console[level]
* @propety {boolean} collapsed - is group collapsed?
* @property {boolean} collapsed - is group collapsed?
* @property {bool} predicate - condition which resolves logger behavior
*/

Expand All @@ -34,6 +37,8 @@ function createLogger() {
} : _options$transformer;
var _options$timestamp = options.timestamp;
var timestamp = _options$timestamp === undefined ? true : _options$timestamp;
var _options$duration = options.duration;
var duration = _options$duration === undefined ? false : _options$duration;

var console = logger || window.console;

Expand All @@ -48,15 +53,21 @@ function createLogger() {
}

var prevState = transformer(getState());
var started = timer.now();
var returnValue = next(action);
var took = timer.now() - started;
var nextState = transformer(getState());
var formattedTime = '';
if (timestamp) {
var time = new Date();
formattedTime = ' @ ' + time.getHours() + ':' + pad(time.getMinutes()) + ':' + pad(time.getSeconds());
}
var formattedDuration = '';
if (duration) {
formattedDuration = ' in ' + took.toFixed(2) + ' ms';
}
var actionType = String(action.type);
var message = 'action ' + actionType + formattedTime;
var message = 'action ' + actionType + formattedTime + formattedDuration;

if (collapsed) {
try {
Expand Down
8 changes: 4 additions & 4 deletions example/dist/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-logger",
"version": "1.0.6",
"version": "1.0.7",
"description": "Logger for redux",
"main": "build/index.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/createLogger.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const pad = num => ('0' + num).slice(-2);

// Use the new performance api to get better precision if available
const timer = typeof performance !== "undefined" ? performance : Date;
const timer = typeof performance !== `undefined` ? performance : Date;

/**
* Creates logger with followed options
Expand All @@ -15,7 +15,7 @@ const timer = typeof performance !== "undefined" ? performance : Date;

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

// exit if console undefined
Expand Down

0 comments on commit 567af7b

Please sign in to comment.