Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid stringify if no stream will log the message. #274

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions lib/bunyan.js
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ Logger.prototype._emit = function (rec, noemit) {

// Stringify the object. Attempt to warn/recover on error.
var str;
if (noemit || this.haveNonRawStreams) {
if (noemit || (this.haveNonRawStreams && requiredLogLevel(rec.level, this.streams))) {
try {
str = JSON.stringify(rec, safeCycles()) + '\n';
} catch (e) {
Expand Down Expand Up @@ -860,12 +860,31 @@ Logger.prototype._emit = function (rec, noemit) {
rec.msg, s.type, s.level, level, rec);
s.stream.write(s.raw ? rec : str);
}
};
}

return str;
}


/**
* Returns true if any of the configured streams is equal or lower than the desired log level
* @param level {Number} Log level value
* @param streams {Array} List of streams
*/
function requiredLogLevel(level, streams) {
var i;

for (i = 0; i < streams.length; i++) {
var s = streams[i];
if (s.level <= level) {
return true;
}
}

return false;
}


/**
* Build a log emitter function for level minLevel. I.e. this is the
* creator of `log.info`, `log.error`, etc.
Expand Down