Compare commits

...

1 Commits

Author SHA1 Message Date
Rahul Padigela 0ec8c532c4 perf: defer log processing if not log level 2016-06-16 16:34:20 -07:00
1 changed files with 18 additions and 15 deletions

View File

@ -434,26 +434,28 @@ class RequestLogger {
}); });
return; return;
} }
const fields = objectCopy({}, this.fields, logFields || {}); // removes object references
const endFlag = isEnd || false; const _logFields = logFields ?
JSON.parse(JSON.stringify(logFields)) : {};
/* /*
* using Date.now() as it's faster than new Date(). logstash component * using Date.now() as it's faster than new Date(). logstash component
* uses this field to generate ISO 8601 timestamp * uses this field to generate ISO 8601 timestamp
*/ */
if (fields.time === undefined) { if (_logFields.time === undefined) {
fields.time = Date.now(); _logFields.time = Date.now();
} }
_logFields.req_id = serializeUids(this.uids);
fields.req_id = serializeUids(this.uids); const endFlag = isEnd || false;
if (endFlag) { if (endFlag) {
this.elapsedTime = process.hrtime(this.startTime); this.elapsedTime = process.hrtime(this.startTime);
fields.elapsed_ms = this.elapsedTime[0] * 1000 _logFields.elapsed_ms = this.elapsedTime[0] * 1000
+ this.elapsedTime[1] / 1000000; + this.elapsedTime[1] / 1000000;
} }
// const fields = objectCopy({}, this.fields, _logFields || {});
const logEntry = { const logEntry = {
level, level,
fields, fields: _logFields,
msg, msg,
}; };
this.entries.push(logEntry); this.entries.push(logEntry);
@ -484,24 +486,25 @@ class RequestLogger {
* @returns {undefined} * @returns {undefined}
*/ */
doLogIO(logEntry) { doLogIO(logEntry) {
const fields = objectCopy({}, this.fields, logEntry.fields);
switch (logEntry.level) { switch (logEntry.level) {
case 'trace': case 'trace':
this.sLogger.trace(logEntry.fields, logEntry.msg); this.sLogger.trace(fields, logEntry.msg);
break; break;
case 'debug': case 'debug':
this.sLogger.debug(logEntry.fields, logEntry.msg); this.sLogger.debug(fields, logEntry.msg);
break; break;
case 'info': case 'info':
this.sLogger.info(logEntry.fields, logEntry.msg); this.sLogger.info(fields, logEntry.msg);
break; break;
case 'warn': case 'warn':
this.sLogger.warn(logEntry.fields, logEntry.msg); this.sLogger.warn(fields, logEntry.msg);
break; break;
case 'error': case 'error':
this.sLogger.error(logEntry.fields, logEntry.msg); this.sLogger.error(fields, logEntry.msg);
break; break;
case 'fatal': case 'fatal':
this.sLogger.fatal(logEntry.fields, logEntry.msg); this.sLogger.fatal(fields, logEntry.msg);
break; break;
default: default:
throw new Error(`Unexpected log level: ${logEntry.level}`); throw new Error(`Unexpected log level: ${logEntry.level}`);