From 4ad39fe8de84fa5409e839b864fd6f95b8953a58 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Thu, 9 Aug 2012 09:29:50 -0700 Subject: [PATCH] '--strict' option for CLI --- CHANGES.md | 3 ++- bin/bunyan | 23 +++++++++++++++++------ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 0593a67..8e2a777 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,7 +2,8 @@ ## bunyan 0.11.3 (not yet released) -(nothing yet) +- Add '--strict' option to `bunyan` CLI to suppress all but legal Bunyan JSON + log lines. By default non-JSON, and non-Bunyan lines are passed through. ## bunyan 0.11.2 diff --git a/bin/bunyan b/bin/bunyan index 28564fb..7d98c35 100755 --- a/bin/bunyan +++ b/bin/bunyan @@ -145,6 +145,8 @@ function printHelp() { console.log(" and only show those that resolve to a truish value."); console.log(" E.g. `-c 'pid == 123'`, `-c 'level == 50'`. You must"); console.log(" use the numeric values for filtering by level."); + console.log(" --strict Suppress all but legal Bunyan JSON log lines. By default"); + console.log(" non-JSON, and non-Bunyan lines are passed through."); console.log(""); console.log("Output options:"); console.log(" --color Colorize output. Defaults to try if output"); @@ -297,7 +299,8 @@ function parseArgv(argv) { outputMode: OM_PAUL, jsonIndent: 2, level: null, - conditions: null + conditions: null, + strict: false }; // Turn '-iH' into '-i -H', except for argument-accepting options. @@ -337,6 +340,9 @@ function parseArgv(argv) { case "--version": parsed.version = true; break; + case "--strict": + parsed.strict = true; + break; case "--color": parsed.color = true; break; @@ -465,19 +471,24 @@ function handleLogLine(file, line, opts, stylize) { // Emit non-JSON lines immediately. var rec; if (!line) { - return emit(line + '\n'); + if (!opts.strict) emit(line + '\n'); + return; } else if (line[0] !== '{') { - return emit(line + '\n'); // not JSON + if (!opts.strict) emit(line + '\n'); // not JSON + return; } else { try { rec = JSON.parse(line); } catch(e) { - return emit(line + '\n'); + if (!opts.strict) emit(line + '\n'); + return; } } - if (!isValidRecord(rec)) - return emitRecord(rec, line, opts, stylize); + if (!isValidRecord(rec)) { + if (!opts.strict) emitRecord(rec, line, opts, stylize); + return; + } if (!filterRecord(rec, opts)) return;