Skip to content

Commit

Permalink
Custom logging on CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
Neamar committed Nov 20, 2014
1 parent b284480 commit f7f1952
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 9 deletions.
51 changes: 48 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 @@ -484,6 +487,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 +635,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 +1051,18 @@ 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;
emit(opts.template ? opts.template(rec) : 'No template defined. Use --template.' +'\n');
break;
default:
throw new Error('unknown output mode: '+opts.outputMode);
}
Expand Down
19 changes: 13 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,26 @@
"bin": {
"bunyan": "./bin/bunyan"
},

"repository": {
"type": "git",
"url": "git://github.com/trentm/node-bunyan.git"
},
"engines": ["node >=0.8.0"],
"keywords": ["log", "logging", "log4j", "json", "bunyan"],

"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 All @@ -27,7 +35,6 @@
"verror": "1.3.3",
"vasync": "1.4.3"
},

"scripts": {
"test": "make test"
}
Expand Down

0 comments on commit f7f1952

Please sign in to comment.