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

fixing not parsed bunyan records when there's a newline-less log #683

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
33 changes: 14 additions & 19 deletions bin/bunyan
Original file line number Diff line number Diff line change
Expand Up @@ -734,22 +734,22 @@ function handleLogLine(file, line, opts, stylize) {
// Emit non-JSON lines immediately.
var rec;
if (!line) {
if (!opts.strict) emit(line + '\n');
if (!opts.strict) emit(line);
return;
} else if (line[0] !== '{') {
if (!opts.strict) emit(line + '\n'); // not JSON
if (!opts.strict) emit(line); // not JSON
return;
} else {
try {
rec = JSON.parse(line);
} catch (e) {
if (!opts.strict) emit(line + '\n');
if (!opts.strict) emit(line);
return;
}
}

if (!isValidRecord(rec)) {
if (!opts.strict) emit(line + '\n');
if (!opts.strict) emit(line);
return;
}

Expand Down Expand Up @@ -784,7 +784,7 @@ function emitRecord(rec, line, opts, stylize) {
// If 'res', show the response.
// If 'err' and 'err.stack' then show that.
if (!isValidRecord(rec)) {
return emit(line + '\n');
return emit(line);
}

delete rec.v;
Expand Down Expand Up @@ -1111,7 +1111,7 @@ function emitRecord(rec, line, opts, stylize) {
/* JSSTYLED */
// <http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/SimpleLayout.html>
if (!isValidRecord(rec)) {
return emit(line + '\n');
return emit(line);
}
emit(format('%s - %s\n',
upperNameFromLevel[rec.level] || 'LVL' + rec.level,
Expand Down Expand Up @@ -1141,32 +1141,27 @@ function emit(s) {
* @param callback {Function} `function ()`
*/
function processStdin(opts, stylize, callback) {
var leftover = ''; // Left-over partial line from last chunk.
var stdin = process.stdin;
stdin.resume();
stdin.setEncoding('utf8');
stdin.on('data', function (chunk) {
var lines = chunk.split(/\r\n|\n/);
var length = lines.length;
if (length === 1) {
leftover += lines[0];
handleLogLine(null, lines[0], opts, stylize);
return;
}

if (length > 1) {
handleLogLine(null, leftover + lines[0], opts, stylize);
handleLogLine(null, lines[0] + '\n', opts, stylize);
}
leftover = lines.pop();

length -= 1;
for (var i = 1; i < length; i++) {
handleLogLine(null, lines[i], opts, stylize);
handleLogLine(null, lines[i] + '\n', opts, stylize);
}
});
stdin.on('end', function () {
if (leftover) {
handleLogLine(null, leftover, opts, stylize);
leftover = '';
}
callback();
});
}
Expand Down Expand Up @@ -1313,12 +1308,12 @@ function processPids(opts, stylize, callback) {
return;
}
if (length > 1) {
handleLogLine(null, leftover + lines[0], opts, stylize);
handleLogLine(null, leftover + lines[0] + '\n', opts, stylize);
}
leftover = lines.pop();
length -= 1;
for (var i = 1; i < length; i++) {
handleLogLine(null, lines[i], opts, stylize);
handleLogLine(null, lines[i] + '\n', opts, stylize);
}
});

Expand Down Expand Up @@ -1390,12 +1385,12 @@ function processFile(file, opts, stylize, callback) {
}

if (length > 1) {
handleLogLine(file, leftover + lines[0], opts, stylize);
handleLogLine(file, leftover + lines[0] + '\n', opts, stylize);
}
leftover = lines.pop();
length -= 1;
for (var i = 1; i < length; i++) {
handleLogLine(file, lines[i], opts, stylize);
handleLogLine(file, lines[i] + '\n', opts, stylize);
}
});

Expand Down