Skip to content

Commit

Permalink
[refactor] rewrite the LOG.error function
Browse files Browse the repository at this point in the history
  • Loading branch information
rayshih committed Sep 17, 2014
1 parent 6039483 commit 26b2c15
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions lib/logger/index.js
Expand Up @@ -66,26 +66,41 @@ var config_logger = function ( logger ){
});
} : noop;

/**
* LOG.error( status, res, err ) // 1
* LOG.error( status, res, msg ) // 2
* LOG.error( status, msg, err ) // 3
* LOG.error( status, msg ) // 4
*/
error = config.error ? function ( status, res, msg ){
var _msg, id, time;

if( !msg ){
_msg = res;
}else{
_msg = UTILS.is( msg ) === 'error' ? msg.stack : msg;
var _msg;
var _res;
var _err;

// deal with third argument
if( msg ){
if( UTILS.is( msg ) === 'error' ){
_err = msg; // for 1 and 3
}else{
_msg = msg; // for 2
}
}

// deal with second argument
if( UTILS.is( res ) === 'string' ){
_msg = res;
_msg = res; // for 3, 4
}else{
_res = res; // for 1, 2
}

var data = {
status : status,
pid : pid,
msg : _msg
};

if( UTILS.is( res ) !== 'string' ){
if( _msg ) data.msg = _msg;
if( _err ) data.err = _err;
if( _res ){
data.id = res._id;
data.time = Date.now() - res._start;
}
Expand Down

0 comments on commit 26b2c15

Please sign in to comment.