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

Add "custom" output for CLI #191

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
70 changes: 67 additions & 3 deletions bin/bunyan
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var child_process = require('child_process'),
exec = child_process.exec,
execFile = child_process.execFile;
var assert = require('assert');
var _ = null;

var nodeSpawnSupportsStdio = (
Number(process.version.split('.')[0]) >= 0 ||
Expand All @@ -44,14 +45,16 @@ var OM_INSPECT = 3;
var OM_SIMPLE = 4;
var OM_SHORT = 5;
var OM_BUNYAN = 6;
var OM_CUSTOM = 7;
var OM_FROM_NAME = {
'long': OM_LONG,
'paul': OM_LONG, /* backward compat */
'json': OM_JSON,
'inspect': OM_INSPECT,
'simple': OM_SIMPLE,
'short': OM_SHORT,
'bunyan': OM_BUNYAN
'bunyan': OM_BUNYAN,
'custom': OM_CUSTOM
};


Expand Down Expand Up @@ -225,6 +228,14 @@ function printHelp() {
p(' bunyan: 0 indented JSON, bunyan\'s native format');
p(' inspect: node.js `util.inspect` output');
p(' short: like "long", but more concise');
p(' custom: use custom template defined with --template');
p(' -t, --template TEMPLATE');
p(' Custom template (you need to set -o custom)');
p(' Let you define your own templates, using mustache-like syntax.');
p(' Keys from the current object will be the template free variable.');
p(' You can put arbitrary code between {{ }}.');
p(' Use _cS to start a color, and _cE to stop a color.');
p(' Example: {{ _cS(\'grey\') }}{{ req.username }}{{ _cE(\'grey\') }}');
p(' -j shortcut for `-o json`');
p(' -0 shortcut for `-o bunyan`');
p('');
Expand Down Expand Up @@ -484,6 +495,20 @@ function parseArgv(argv) {
throw new Error('unknown output mode: "'+name+'"');
}
break;
case '-t':
case '--template':
try {
_ = require('underscore');
} catch(e) {
throw new Error("you need to install 'underscore' to use the custom template.");
}
// Who want ERB? (not me)
// Let's go for mustache style parsing!
_.templateSettings = {
interpolate: /\{\{(.+?)\}\}/g
};
parsed.template = _.template(args.shift());
break;
case '-j': // output with JSON.stringify
parsed.outputMode = OM_JSON;
break;
Expand Down Expand Up @@ -618,17 +643,33 @@ function stylizeWithColor(str, color) {
return '';
var codes = colors[color];
if (codes) {
return '\033[' + codes[0] + 'm' + str +
'\033[' + codes[1] + 'm';
return stylizeWithColor.start(color) + str + stylizeWithColor.end(color);
} else {
return str;
}
}

stylizeWithColor.start = function(color) {
var codes = colors[color];
if (codes) {
return '\033[' + codes[0] + 'm';
}
};

stylizeWithColor.end = function(color) {
var codes = colors[color];
if (codes) {
return '\033[' + codes[1] + 'm';
}
};


function stylizeWithoutColor(str, color) {
return str;
}

stylizeWithoutColor.start = stylizeWithoutColor.end = function() {};


/**
* Is this a valid Bunyan log record.
Expand Down Expand Up @@ -1018,6 +1059,29 @@ function emitRecord(rec, line, opts, stylize) {
upperNameFromLevel[rec.level] || 'LVL' + rec.level,
rec.msg));
break;

case OM_CUSTOM:
if (!isValidRecord(rec)) {
return emit(line + '\n');
}

// Shortcut for "colorStart"
rec._cS = stylize.start;
// Shortcut for "colorEnd"
rec._cE = stylize.end;
// Shortcut for easy date access
rec._time = new Date(rec.time);

var out;
try {
out = opts.template ? opts.template(rec) : 'No template defined. Use --template.';
}
catch(e) {
// Unprocessable line, display default
return emit(line + '\n');
}
emit(out + '\n');
break;
default:
throw new Error('unknown output mode: '+opts.outputMode);
}
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
},
"engines": ["node >=0.8.0"],
"keywords": ["log", "logging", "log4j", "json", "bunyan"],

"// comment1": "'dtrace-provider' required for dtrace features",
"// comment2": "'mv' required for RotatingFileStream",
"// comment2": "'underscore' required for custom templating in Bunyan CLI",
"// comment3": "'mv' required for RotatingFileStream",
"optionalDependencies": {
"dtrace-provider": "~0.3 >0.3.0",
"underscore": "^1.7.0",
"mv": "~2"
},
"devDependencies": {
Expand Down