2012-01-31 00:07:08 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
//
|
|
|
|
// bunyan -- filter and pretty-print JSON logs, like Bunyan logs.
|
|
|
|
//
|
|
|
|
// See <https://github.com/trentm/node-bunyan>.
|
|
|
|
//
|
|
|
|
|
2012-06-05 06:31:10 +00:00
|
|
|
var VERSION = "0.8.1";
|
2012-01-31 00:07:08 +00:00
|
|
|
|
|
|
|
var util = require('util');
|
|
|
|
var pathlib = require('path');
|
|
|
|
var vm = require('vm');
|
2012-02-04 01:05:13 +00:00
|
|
|
var http = require('http');
|
2012-02-24 05:19:28 +00:00
|
|
|
var fs = require('fs');
|
2012-01-31 00:07:08 +00:00
|
|
|
var warn = console.warn;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---- globals and constants
|
|
|
|
|
|
|
|
// Output modes.
|
|
|
|
var OM_PAUL = 1;
|
|
|
|
var OM_JSON = 2;
|
|
|
|
var OM_INSPECT = 3;
|
|
|
|
var OM_SIMPLE = 4;
|
|
|
|
var OM_FROM_NAME = {
|
|
|
|
"paul": OM_PAUL,
|
|
|
|
"json": OM_JSON,
|
|
|
|
"inspect": OM_INSPECT,
|
|
|
|
"simple": OM_SIMPLE
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Levels
|
2012-02-06 23:23:51 +00:00
|
|
|
var TRACE = 10;
|
|
|
|
var DEBUG = 20;
|
|
|
|
var INFO = 30;
|
|
|
|
var WARN = 40;
|
|
|
|
var ERROR = 50;
|
|
|
|
var FATAL = 60;
|
2012-01-31 00:07:08 +00:00
|
|
|
|
|
|
|
var levelFromName = {
|
2012-01-31 22:43:13 +00:00
|
|
|
'trace': TRACE,
|
2012-01-31 00:07:08 +00:00
|
|
|
'debug': DEBUG,
|
|
|
|
'info': INFO,
|
|
|
|
'warn': WARN,
|
|
|
|
'error': ERROR,
|
|
|
|
'fatal': FATAL
|
|
|
|
};
|
2012-02-06 23:23:51 +00:00
|
|
|
var nameFromLevel = {};
|
|
|
|
var upperNameFromLevel = {};
|
2012-02-23 05:03:03 +00:00
|
|
|
var upperPaddedNameFromLevel = {};
|
2012-02-06 23:23:51 +00:00
|
|
|
Object.keys(levelFromName).forEach(function (name) {
|
|
|
|
var lvl = levelFromName[name];
|
|
|
|
nameFromLevel[lvl] = name;
|
|
|
|
upperNameFromLevel[lvl] = name.toUpperCase();
|
2012-02-23 05:03:03 +00:00
|
|
|
upperPaddedNameFromLevel[lvl] = (
|
|
|
|
name.length === 4 ? ' ' : '') + name.toUpperCase();
|
2012-02-06 23:23:51 +00:00
|
|
|
});
|
2012-01-31 00:07:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---- support functions
|
|
|
|
|
|
|
|
function getVersion() {
|
|
|
|
return VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var format = util.format;
|
|
|
|
if (!format) {
|
|
|
|
// If not node 0.6, then use its `util.format`:
|
|
|
|
// <https://github.com/joyent/node/blob/master/lib/util.js#L22>:
|
|
|
|
var inspect = util.inspect;
|
|
|
|
var formatRegExp = /%[sdj%]/g;
|
|
|
|
format = function format(f) {
|
|
|
|
if (typeof f !== 'string') {
|
|
|
|
var objects = [];
|
|
|
|
for (var i = 0; i < arguments.length; i++) {
|
|
|
|
objects.push(inspect(arguments[i]));
|
|
|
|
}
|
|
|
|
return objects.join(' ');
|
|
|
|
}
|
2012-02-10 05:07:01 +00:00
|
|
|
|
2012-01-31 00:07:08 +00:00
|
|
|
var i = 1;
|
|
|
|
var args = arguments;
|
|
|
|
var len = args.length;
|
|
|
|
var str = String(f).replace(formatRegExp, function(x) {
|
|
|
|
if (i >= len) return x;
|
|
|
|
switch (x) {
|
|
|
|
case '%s': return String(args[i++]);
|
|
|
|
case '%d': return Number(args[i++]);
|
|
|
|
case '%j': return JSON.stringify(args[i++]);
|
|
|
|
case '%%': return '%';
|
|
|
|
default:
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
for (var x = args[i]; i < len; x = args[++i]) {
|
|
|
|
if (x === null || typeof x !== 'object') {
|
|
|
|
str += ' ' + x;
|
|
|
|
} else {
|
|
|
|
str += ' ' + inspect(x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-02-02 05:33:18 +00:00
|
|
|
function indent(s) {
|
|
|
|
return ' ' + s.split(/\r?\n/).join('\n ')
|
|
|
|
}
|
2012-01-31 00:07:08 +00:00
|
|
|
|
2012-02-08 23:18:07 +00:00
|
|
|
function objCopy(obj) {
|
|
|
|
if (obj === null) {
|
|
|
|
return null;
|
|
|
|
} else if (Array.isArray(obj)) {
|
|
|
|
return obj.slice();
|
|
|
|
} else {
|
|
|
|
var copy = {};
|
|
|
|
Object.keys(obj).forEach(function (k) {
|
|
|
|
copy[k] = obj[k];
|
|
|
|
});
|
|
|
|
return copy;
|
|
|
|
}
|
2012-01-31 00:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function printHelp() {
|
|
|
|
util.puts("Usage:");
|
2012-02-24 05:19:28 +00:00
|
|
|
util.puts(" bunyan [OPTIONS] [FILE ...]");
|
2012-01-31 00:07:08 +00:00
|
|
|
util.puts("");
|
|
|
|
util.puts("Pipe in your JSON log file content (e.g. bunyan logs) for nicer ");
|
|
|
|
util.puts("output");
|
|
|
|
util.puts("");
|
|
|
|
util.puts("Options:");
|
|
|
|
util.puts(" -h, --help print this help info and exit");
|
|
|
|
util.puts(" --version print version of this command and exit");
|
|
|
|
util.puts("");
|
2012-02-23 05:03:03 +00:00
|
|
|
util.puts(" --color Colorize output. Defaults to try if output");
|
|
|
|
util.puts(" stream is a TTY.");
|
2012-02-23 21:00:53 +00:00
|
|
|
util.puts(" --no-color Force no coloring (e.g. terminal doesn't support it)");
|
2012-02-02 05:33:18 +00:00
|
|
|
util.puts(" -o, --output MODE");
|
|
|
|
util.puts(" Specify an output mode/format. One of");
|
|
|
|
util.puts(" paul: (the default) pretty")
|
2012-01-31 00:07:08 +00:00
|
|
|
util.puts(" json: JSON output, 2-space indent");
|
|
|
|
util.puts(" json-N: JSON output, N-space indent, e.g. 'json-4'");
|
|
|
|
util.puts(" inspect: node.js `util.inspect` output");
|
|
|
|
util.puts(" -j shortcut for `-o json`");
|
|
|
|
util.puts("");
|
2012-02-24 05:19:28 +00:00
|
|
|
util.puts("See <https://github.com/trentm/node-bunyan> for more complete docs.");
|
2012-01-31 00:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the command-line options and arguments into an object.
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* 'args': [...] // arguments
|
|
|
|
* 'help': true, // true if '-h' option given
|
|
|
|
* // etc.
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* @return {Object} The parsed options. `.args` is the argument list.
|
|
|
|
* @throws {Error} If there is an error parsing argv.
|
|
|
|
*/
|
|
|
|
function parseArgv(argv) {
|
|
|
|
var parsed = {
|
|
|
|
args: [],
|
|
|
|
help: false,
|
2012-02-23 05:03:03 +00:00
|
|
|
color: process.stdout.isTTY,
|
2012-01-31 00:07:08 +00:00
|
|
|
outputMode: OM_PAUL,
|
|
|
|
jsonIndent: 2
|
|
|
|
};
|
2012-02-10 05:07:01 +00:00
|
|
|
|
2012-01-31 00:07:08 +00:00
|
|
|
// Turn '-iH' into '-i -H', except for argument-accepting options.
|
|
|
|
var args = argv.slice(2); // drop ['node', 'scriptname']
|
|
|
|
var newArgs = [];
|
|
|
|
var optTakesArg = {'d': true, 'o': true};
|
|
|
|
for (var i = 0; i < args.length; i++) {
|
|
|
|
if (args[i].charAt(0) === "-" && args[i].charAt(1) !== '-' && args[i].length > 2) {
|
|
|
|
var splitOpts = args[i].slice(1).split("");
|
|
|
|
for (var j = 0; j < splitOpts.length; j++) {
|
|
|
|
newArgs.push('-' + splitOpts[j])
|
|
|
|
if (optTakesArg[splitOpts[j]]) {
|
|
|
|
var optArg = splitOpts.slice(j+1).join("");
|
|
|
|
if (optArg.length) {
|
|
|
|
newArgs.push(optArg);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
newArgs.push(args[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
args = newArgs;
|
|
|
|
|
|
|
|
endOfOptions = false;
|
|
|
|
while (args.length > 0) {
|
|
|
|
var arg = args.shift();
|
|
|
|
switch(arg) {
|
|
|
|
case "--":
|
|
|
|
endOfOptions = true;
|
|
|
|
break;
|
|
|
|
case "-h": // display help and exit
|
|
|
|
case "--help":
|
|
|
|
parsed.help = true;
|
|
|
|
break;
|
|
|
|
case "--version":
|
|
|
|
parsed.version = true;
|
|
|
|
break;
|
2012-02-23 05:03:03 +00:00
|
|
|
case "--color":
|
|
|
|
parsed.color = true;
|
2012-01-31 00:07:08 +00:00
|
|
|
break;
|
2012-02-23 21:00:53 +00:00
|
|
|
case "--no-color":
|
|
|
|
parsed.color = false;
|
|
|
|
break;
|
2012-01-31 00:07:08 +00:00
|
|
|
case "-o":
|
|
|
|
case "--output":
|
|
|
|
var name = args.shift();
|
|
|
|
var idx = name.lastIndexOf('-');
|
|
|
|
if (idx !== -1) {
|
|
|
|
var indent = Number(name.slice(idx+1));
|
|
|
|
if (! isNaN(indent)) {
|
|
|
|
parsed.jsonIndent = indent;
|
|
|
|
name = name.slice(0, idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parsed.outputMode = OM_FROM_NAME[name];
|
|
|
|
if (parsed.outputMode === undefined) {
|
|
|
|
throw new Error("unknown output mode: '"+name+"'");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "-j": // output with JSON.stringify
|
|
|
|
parsed.outputMode = OM_JSON;
|
|
|
|
break;
|
|
|
|
default: // arguments
|
|
|
|
if (!endOfOptions && arg.length > 0 && arg[0] === '-') {
|
|
|
|
throw new Error("unknown option '"+arg+"'");
|
|
|
|
}
|
|
|
|
parsed.args.push(arg);
|
|
|
|
break;
|
2012-02-10 05:07:01 +00:00
|
|
|
}
|
2012-01-31 00:07:08 +00:00
|
|
|
}
|
|
|
|
//TODO: '--' handling and error on a first arg that looks like an option.
|
|
|
|
|
|
|
|
return parsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function isInteger(s) {
|
|
|
|
return (s.search(/^-?[0-9]+$/) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-23 05:03:03 +00:00
|
|
|
// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
|
|
|
|
// Suggested colors (some are unreadable in common cases):
|
|
|
|
// - Good: cyan, yellow (limited use), grey, bold, green, magenta, red
|
|
|
|
// - Bad: blue (not visible on cmd.exe)
|
|
|
|
var colors = {
|
|
|
|
'bold' : [1, 22],
|
|
|
|
'italic' : [3, 23],
|
|
|
|
'underline' : [4, 24],
|
|
|
|
'inverse' : [7, 27],
|
|
|
|
'white' : [37, 39],
|
|
|
|
'grey' : [90, 39],
|
|
|
|
'black' : [30, 39],
|
|
|
|
'blue' : [34, 39],
|
|
|
|
'cyan' : [36, 39],
|
|
|
|
'green' : [32, 39],
|
|
|
|
'magenta' : [35, 39],
|
|
|
|
'red' : [31, 39],
|
|
|
|
'yellow' : [33, 39]
|
|
|
|
};
|
|
|
|
|
|
|
|
function stylizeWithColor(str, color) {
|
2012-02-24 05:19:28 +00:00
|
|
|
if (!str)
|
|
|
|
return '';
|
2012-02-23 05:03:03 +00:00
|
|
|
var codes = colors[color];
|
|
|
|
if (codes) {
|
|
|
|
return '\033[' + codes[0] + 'm' + str +
|
|
|
|
'\033[' + codes[1] + 'm';
|
|
|
|
} else {
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function stylizeWithoutColor(str, color) {
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-24 05:19:28 +00:00
|
|
|
/**
|
|
|
|
* Is this a valid Bunyna log record.
|
|
|
|
*/
|
|
|
|
function isValidRecord(rec) {
|
|
|
|
if (rec.v == null ||
|
|
|
|
rec.level == null ||
|
|
|
|
rec.name == null ||
|
|
|
|
rec.hostname == null ||
|
|
|
|
rec.pid == null ||
|
|
|
|
rec.time == null ||
|
|
|
|
rec.msg == null) {
|
|
|
|
// Not valid Bunyan log.
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-31 00:07:08 +00:00
|
|
|
/**
|
|
|
|
* Print out a single result, considering input options.
|
|
|
|
*/
|
2012-02-23 05:03:03 +00:00
|
|
|
function handleLogLine(line, opts, stylize) {
|
2012-01-31 00:07:08 +00:00
|
|
|
// Handle non-JSON lines.
|
|
|
|
var rec;
|
|
|
|
if (!line) {
|
|
|
|
return emit(line + '\n');
|
|
|
|
} else if (line[0] !== '{') {
|
|
|
|
return emit(line + '\n'); // not JSON
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
rec = JSON.parse(line);
|
|
|
|
} catch(e) {
|
|
|
|
return emit(line + '\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (opts.outputMode) {
|
|
|
|
case OM_PAUL:
|
2012-02-10 05:07:01 +00:00
|
|
|
// [time] LEVEL: name[/component]/pid on hostname (src): msg* (extras...)
|
2012-02-08 23:18:07 +00:00
|
|
|
// msg*
|
|
|
|
// --
|
|
|
|
// long and multi-line extras
|
|
|
|
// ...
|
|
|
|
// If 'msg' is single-line, then it goes in the top line.
|
2012-02-02 05:33:18 +00:00
|
|
|
// If 'req', show the request.
|
|
|
|
// If 'res', show the response.
|
|
|
|
// If 'err' and 'err.stack' then show that.
|
2012-02-24 05:19:28 +00:00
|
|
|
if (!isValidRecord(rec)) {
|
|
|
|
return emit(line + '\n');
|
|
|
|
}
|
|
|
|
|
2012-02-08 23:18:07 +00:00
|
|
|
delete rec.v;
|
|
|
|
|
2012-02-23 05:03:03 +00:00
|
|
|
var time = stylize('[' + rec.time + ']', 'XXX');
|
2012-02-08 23:18:07 +00:00
|
|
|
delete rec.time;
|
|
|
|
|
2012-02-06 23:13:50 +00:00
|
|
|
var nameStr = rec.name;
|
2012-02-08 23:18:07 +00:00
|
|
|
delete rec.name;
|
|
|
|
|
2012-01-31 00:07:08 +00:00
|
|
|
if (rec.component) {
|
2012-02-06 23:13:50 +00:00
|
|
|
nameStr += '/' + rec.component;
|
2012-01-31 00:07:08 +00:00
|
|
|
}
|
2012-02-08 23:18:07 +00:00
|
|
|
delete rec.component;
|
|
|
|
|
2012-02-10 05:07:01 +00:00
|
|
|
nameStr += '/' + rec.pid;
|
|
|
|
delete rec.pid;
|
|
|
|
|
2012-02-23 05:03:03 +00:00
|
|
|
var level = (upperPaddedNameFromLevel[rec.level] || "LVL" + rec.level);
|
|
|
|
if (opts.color) {
|
|
|
|
var colorFromLevel = {
|
|
|
|
10: 'grey', // TRACE
|
|
|
|
20: 'grey', // DEBUG
|
|
|
|
30: 'cyan', // INFO
|
|
|
|
40: 'magenta', // WARN
|
|
|
|
50: 'red', // ERROR
|
|
|
|
60: 'inverse', // FATAL
|
|
|
|
}
|
|
|
|
level = stylize(level, colorFromLevel[rec.level])
|
|
|
|
}
|
2012-02-08 23:18:07 +00:00
|
|
|
delete rec.level;
|
|
|
|
|
2012-02-06 04:33:57 +00:00
|
|
|
var src = "";
|
|
|
|
if (rec.src && rec.src.file) {
|
|
|
|
var s = rec.src;
|
|
|
|
if (s.func) {
|
|
|
|
src = format(" (%s:%d in %s)", s.file, s.line, s.func);
|
|
|
|
} else {
|
|
|
|
src = format(" (%s:%d)", s.file, s.line);
|
|
|
|
}
|
2012-04-10 22:32:33 +00:00
|
|
|
src = stylize(src, 'green');
|
2012-02-06 04:33:57 +00:00
|
|
|
}
|
2012-02-08 23:18:07 +00:00
|
|
|
delete rec.src;
|
2012-02-10 05:07:01 +00:00
|
|
|
|
2012-02-08 23:18:07 +00:00
|
|
|
var hostname = rec.hostname;
|
|
|
|
delete rec.hostname;
|
|
|
|
|
2012-01-31 00:45:25 +00:00
|
|
|
var extras = [];
|
2012-02-02 05:33:18 +00:00
|
|
|
var details = [];
|
2012-02-08 23:18:07 +00:00
|
|
|
|
|
|
|
if (rec.req_id) {
|
|
|
|
extras.push("req_id=" + rec.req_id);
|
|
|
|
}
|
|
|
|
delete rec.req_id;
|
2012-02-10 05:07:01 +00:00
|
|
|
|
2012-02-08 23:18:07 +00:00
|
|
|
if (rec.latency) {
|
|
|
|
extras.push(rec.latency + "ms");
|
|
|
|
}
|
|
|
|
delete rec.latency;
|
|
|
|
|
2012-02-23 05:03:03 +00:00
|
|
|
var onelineMsg;
|
2012-01-31 00:07:08 +00:00
|
|
|
if (rec.msg.indexOf('\n') !== -1) {
|
2012-02-02 05:33:18 +00:00
|
|
|
onelineMsg = '';
|
2012-02-23 05:03:03 +00:00
|
|
|
details.push(indent(stylize(rec.msg, 'cyan')));
|
|
|
|
} else {
|
|
|
|
onelineMsg = ' ' + stylize(rec.msg, 'cyan');
|
2012-01-31 00:07:08 +00:00
|
|
|
}
|
2012-02-08 23:18:07 +00:00
|
|
|
delete rec.msg;
|
|
|
|
|
2012-02-02 05:33:18 +00:00
|
|
|
if (rec.req) {
|
|
|
|
var headers = rec.req.headers;
|
2012-02-04 01:05:13 +00:00
|
|
|
details.push(indent(format("%s %s HTTP/1.1\n%s", rec.req.method,
|
2012-02-02 05:33:18 +00:00
|
|
|
rec.req.url,
|
|
|
|
Object.keys(headers).map(
|
|
|
|
function (h) { return h + ': ' + headers[h]}).join('\n'))));
|
|
|
|
}
|
2012-02-08 23:18:07 +00:00
|
|
|
delete rec.req;
|
|
|
|
|
2012-02-08 23:27:14 +00:00
|
|
|
if (rec.client_req) {
|
|
|
|
var headers = rec.client_req.headers;
|
|
|
|
var hostHeaderLine = '';
|
|
|
|
if (rec.client_req.address) {
|
|
|
|
hostHeaderLine = 'Host: ' + rec.client_req.address;
|
|
|
|
if (rec.client_req.port)
|
|
|
|
hostHeaderLine += ':' + rec.client_req.port;
|
|
|
|
hostHeaderLine += '\n';
|
|
|
|
}
|
|
|
|
details.push(indent(format("%s %s HTTP/1.1\n%s%s", rec.client_req.method,
|
|
|
|
rec.client_req.url,
|
|
|
|
hostHeaderLine,
|
|
|
|
Object.keys(headers).map(
|
|
|
|
function (h) { return h + ': ' + headers[h]}).join('\n'))));
|
|
|
|
}
|
|
|
|
delete rec.client_req;
|
|
|
|
|
2012-02-02 05:33:18 +00:00
|
|
|
if (rec.res) {
|
2012-02-04 01:05:13 +00:00
|
|
|
var s = '';
|
|
|
|
if (rec.res.header) {
|
|
|
|
s += rec.res.header.trimRight()
|
|
|
|
} else if (rec.res.headers) {
|
|
|
|
if (rec.res.statusCode) {
|
|
|
|
s += format("HTTP/1.1 %s %s\n", rec.res.statusCode,
|
|
|
|
http.STATUS_CODES[rec.res.statusCode]);
|
|
|
|
}
|
|
|
|
var headers = rec.res.headers;
|
|
|
|
s += Object.keys(headers).map(
|
|
|
|
function (h) { return h + ': ' + headers[h]}).join('\n');
|
|
|
|
}
|
|
|
|
if (s) {
|
|
|
|
details.push(indent(s));
|
|
|
|
}
|
2012-02-02 05:33:18 +00:00
|
|
|
}
|
2012-02-08 23:18:07 +00:00
|
|
|
delete rec.res;
|
2012-02-10 05:07:01 +00:00
|
|
|
|
2012-02-02 05:33:18 +00:00
|
|
|
if (rec.err && rec.err.stack) {
|
|
|
|
details.push(indent(rec.err.stack))
|
2012-02-28 22:45:53 +00:00
|
|
|
delete rec.err;
|
2012-02-02 05:33:18 +00:00
|
|
|
}
|
2012-02-08 23:18:07 +00:00
|
|
|
|
|
|
|
var leftover = Object.keys(rec);
|
|
|
|
for (var i = 0; i < leftover.length; i++) {
|
|
|
|
var key = leftover[i];
|
|
|
|
var value = rec[key];
|
|
|
|
var type = typeof(value);
|
2012-02-12 00:34:40 +00:00
|
|
|
var stringified = false;
|
2012-02-08 23:18:07 +00:00
|
|
|
if (typeof(value) !== 'string') {
|
|
|
|
value = JSON.stringify(value, null, 2);
|
2012-02-12 00:34:40 +00:00
|
|
|
stringified = true;
|
2012-02-08 23:18:07 +00:00
|
|
|
}
|
|
|
|
if (value.indexOf('\n') !== -1 || value.length > 50) {
|
|
|
|
details.push(indent(key + ': ' + value))
|
2012-02-12 00:34:40 +00:00
|
|
|
} else if (!stringified && (value.indexOf(' ') != -1 ||
|
|
|
|
value.length === 0)) {
|
|
|
|
extras.push(key + '=' + JSON.stringify(value))
|
2012-02-08 23:18:07 +00:00
|
|
|
} else {
|
|
|
|
extras.push(key + '=' + value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-23 05:03:03 +00:00
|
|
|
extras = stylize(
|
|
|
|
(extras.length ? ' (' + extras.join(', ') + ')' : ''), 'grey');
|
|
|
|
details = stylize(
|
|
|
|
(details.length ? details.join('\n --\n') + '\n' : ''), 'grey');
|
|
|
|
emit(format("%s %s: %s on %s%s:%s%s\n%s",
|
2012-02-08 23:18:07 +00:00
|
|
|
time,
|
|
|
|
level,
|
2012-02-06 23:13:50 +00:00
|
|
|
nameStr,
|
2012-02-08 23:18:07 +00:00
|
|
|
hostname || "<no-hostname>",
|
2012-02-06 04:33:57 +00:00
|
|
|
src,
|
2012-02-02 05:33:18 +00:00
|
|
|
onelineMsg,
|
|
|
|
extras,
|
|
|
|
details));
|
2012-01-31 00:07:08 +00:00
|
|
|
break;
|
2012-02-02 05:33:18 +00:00
|
|
|
|
2012-01-31 00:07:08 +00:00
|
|
|
case OM_INSPECT:
|
|
|
|
emit(util.inspect(rec, false, Infinity, true) + '\n');
|
|
|
|
break;
|
2012-02-02 05:33:18 +00:00
|
|
|
|
2012-01-31 00:07:08 +00:00
|
|
|
case OM_JSON:
|
|
|
|
emit(JSON.stringify(rec, null, opts.jsonIndent) + '\n');
|
|
|
|
break;
|
2012-02-02 05:33:18 +00:00
|
|
|
|
2012-01-31 00:07:08 +00:00
|
|
|
case OM_SIMPLE:
|
|
|
|
// <http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/SimpleLayout.html>
|
2012-02-24 05:19:28 +00:00
|
|
|
if (!isValidRecord(rec)) {
|
|
|
|
return emit(line + '\n');
|
|
|
|
}
|
|
|
|
emit(format("%s - %s\n", upperNameFromLevel[rec.level] || "LVL" + rec.level,
|
2012-01-31 00:07:08 +00:00
|
|
|
rec.msg));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error("unknown output mode: "+opts.outputMode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var stdoutFlushed = true;
|
|
|
|
function emit(s) {
|
|
|
|
// TODO:PERF If this is try/catch is too slow (too granular): move up to
|
|
|
|
// mainline and be sure to only catch this particular error.
|
|
|
|
try {
|
|
|
|
stdoutFlushed = process.stdout.write(s);
|
|
|
|
} catch (e) {
|
|
|
|
// Handle any exceptions in stdout writing in the "error" event above.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
process.stdout.on("error", function (err) {
|
|
|
|
if (err.code === "EPIPE") {
|
|
|
|
// Pass. See <https://github.com/trentm/json/issues/9>.
|
|
|
|
} else {
|
|
|
|
warn(err)
|
|
|
|
drainStdoutAndExit(1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A hacked up version of "process.exit" that will first drain stdout
|
|
|
|
* before exiting. *WARNING: This doesn't stop event processing.* IOW,
|
|
|
|
* callers have to be careful that code following this call isn't
|
|
|
|
* accidentally executed.
|
|
|
|
*
|
|
|
|
* In node v0.6 "process.stdout and process.stderr are blocking when they
|
|
|
|
* refer to regular files or TTY file descriptors." However, this hack might
|
|
|
|
* still be necessary in a shell pipeline.
|
|
|
|
*/
|
|
|
|
function drainStdoutAndExit(code) {
|
|
|
|
process.stdout.on('drain', function () {
|
|
|
|
process.exit(code);
|
|
|
|
});
|
|
|
|
if (stdoutFlushed) {
|
|
|
|
process.exit(code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-24 05:19:28 +00:00
|
|
|
/**
|
|
|
|
* Process all input from stdin.
|
|
|
|
*
|
|
|
|
* @params opts {Object} Bunyan options object.
|
|
|
|
* @param stylize {Function} Output stylize function to use.
|
|
|
|
* @param callback {Function} `function ()`
|
|
|
|
*/
|
|
|
|
function processStdin(opts, stylize, callback) {
|
2012-01-31 00:07:08 +00:00
|
|
|
var leftover = ""; // Left-over partial line from last chunk.
|
2012-02-24 05:19:28 +00:00
|
|
|
var stdin = process.stdin;
|
|
|
|
stdin.resume();
|
2012-01-31 00:07:08 +00:00
|
|
|
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];
|
|
|
|
return;
|
|
|
|
}
|
2012-02-10 05:07:01 +00:00
|
|
|
|
2012-01-31 00:07:08 +00:00
|
|
|
if (length > 1) {
|
2012-02-23 05:03:03 +00:00
|
|
|
handleLogLine(leftover + lines[0], opts, stylize);
|
2012-01-31 00:07:08 +00:00
|
|
|
}
|
|
|
|
leftover = lines.pop();
|
|
|
|
length -= 1;
|
|
|
|
for (var i=1; i < length; i++) {
|
2012-02-23 05:03:03 +00:00
|
|
|
handleLogLine(lines[i], opts, stylize);
|
2012-01-31 00:07:08 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
stdin.on('end', function () {
|
|
|
|
if (leftover) {
|
2012-02-23 05:03:03 +00:00
|
|
|
handleLogLine(leftover, opts, stylize);
|
2012-01-31 00:07:08 +00:00
|
|
|
leftover = '';
|
|
|
|
}
|
2012-02-24 05:19:28 +00:00
|
|
|
callback();
|
2012-01-31 00:07:08 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-02-24 05:19:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Process all input from the given log file.
|
|
|
|
*
|
|
|
|
* @param file {String} Log file path to process.
|
|
|
|
* @params opts {Object} Bunyan options object.
|
|
|
|
* @param stylize {Function} Output stylize function to use.
|
|
|
|
* @param callback {Function} `function ()`
|
|
|
|
*/
|
|
|
|
function processFile(file, opts, stylize, callback) {
|
|
|
|
var stream = fs.createReadStream(file, {encoding: 'utf8'});
|
|
|
|
|
|
|
|
stream.on('error', function (err) {
|
|
|
|
callback(err);
|
|
|
|
})
|
|
|
|
|
|
|
|
var leftover = ''; // Left-over partial line from last chunk.
|
|
|
|
stream.on('data', function (chunk) {
|
|
|
|
var lines = chunk.split(/\r\n|\n/);
|
|
|
|
var length = lines.length;
|
|
|
|
if (length === 1) {
|
|
|
|
leftover += lines[0];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length > 1) {
|
|
|
|
handleLogLine(leftover + lines[0], opts, stylize);
|
|
|
|
}
|
|
|
|
leftover = lines.pop();
|
|
|
|
length -= 1;
|
|
|
|
for (var i=1; i < length; i++) {
|
|
|
|
handleLogLine(lines[i], opts, stylize);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
stream.on('end', function () {
|
|
|
|
if (leftover) {
|
|
|
|
handleLogLine(leftover, opts, stylize);
|
|
|
|
leftover = '';
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* From node async module.
|
|
|
|
*/
|
|
|
|
function asyncForEachSeries(arr, iterator, callback) {
|
|
|
|
if (!arr.length) {
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
var completed = 0;
|
|
|
|
var iterate = function () {
|
|
|
|
iterator(arr[completed], function (err) {
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
callback = function () {};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
completed += 1;
|
|
|
|
if (completed === arr.length) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
iterate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
iterate();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---- mainline
|
|
|
|
|
|
|
|
function main(argv) {
|
|
|
|
var opts;
|
|
|
|
try {
|
|
|
|
opts = parseArgv(argv);
|
|
|
|
} catch (e) {
|
|
|
|
warn("bunyan: error: %s", e.message)
|
|
|
|
return drainStdoutAndExit(1);
|
|
|
|
}
|
|
|
|
if (opts.help) {
|
|
|
|
printHelp();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (opts.version) {
|
|
|
|
util.puts("bunyan " + getVersion());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var stylize = (opts.color ? stylizeWithColor : stylizeWithoutColor);
|
|
|
|
|
|
|
|
var retval = 0;
|
|
|
|
if (opts.args.length > 0) {
|
|
|
|
var files = opts.args;
|
|
|
|
var i = 0;
|
|
|
|
asyncForEachSeries(files,
|
|
|
|
function (file, next) {
|
|
|
|
processFile(file, opts, stylize, function (err) {
|
|
|
|
if (err) {
|
|
|
|
warn('bunyan: %s', err.message);
|
|
|
|
retval += 1;
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function (err) {
|
|
|
|
if (err) {
|
|
|
|
warn("bunyan: unexpected error: %s", err.stack || err);
|
|
|
|
return drainStdoutAndExit(1);
|
|
|
|
}
|
|
|
|
process.exit(retval);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
processStdin(opts, stylize, function () {
|
|
|
|
process.exit(retval);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-31 00:07:08 +00:00
|
|
|
if (require.main === module) {
|
|
|
|
// HACK guard for <https://github.com/trentm/json/issues/24>.
|
|
|
|
// We override the `process.stdout.end` guard that core node.js puts in
|
|
|
|
// place. The real fix is that `.end()` shouldn't be called on stdout
|
2012-02-24 05:19:28 +00:00
|
|
|
// in node core. Node v0.6.9 fixes that. Only guard for v0.6.0..v0.6.8.
|
2012-01-31 00:07:08 +00:00
|
|
|
var nodeVer = process.versions.node.split('.').map(Number);
|
|
|
|
if ([0,6,0] <= nodeVer && nodeVer <= [0,6,8]) {
|
|
|
|
var stdout = process.stdout;
|
|
|
|
stdout.end = stdout.destroy = stdout.destroySoon = function() {
|
|
|
|
/* pass */
|
|
|
|
};
|
|
|
|
}
|
2012-02-10 05:07:01 +00:00
|
|
|
|
2012-01-31 00:07:08 +00:00
|
|
|
main(process.argv);
|
|
|
|
}
|