some jsstyle'ing

This commit is contained in:
Trent Mick 2012-04-27 17:23:29 -07:00
parent a442418f2d
commit 11b91cadd4

View file

@ -4,13 +4,13 @@
* The bunyan logging library for node.js. * The bunyan logging library for node.js.
*/ */
var VERSION = "0.7.1"; var VERSION = '0.7.1';
// Bunyan log format version. This becomes the 'v' field on all log records. // Bunyan log format version. This becomes the 'v' field on all log records.
// `0` is until I release a version "1.0.0" of node-bunyan. Thereafter, // `0` is until I release a version '1.0.0' of node-bunyan. Thereafter,
// starting with `1`, this will be incremented if there is any backward // starting with `1`, this will be incremented if there is any backward
// incompatible change to the log record format. Details will be in // incompatible change to the log record format. Details will be in
// "CHANGES.md" (the change log). // 'CHANGES.md' (the change log).
var LOG_VERSION = 0; var LOG_VERSION = 0;
@ -62,8 +62,9 @@ if (!format) {
var i = 1; var i = 1;
var args = arguments; var args = arguments;
var len = args.length; var len = args.length;
var str = String(f).replace(formatRegExp, function(x) { var str = String(f).replace(formatRegExp, function (x) {
if (i >= len) return x; if (i >= len)
return x;
switch (x) { switch (x) {
case '%s': return String(args[i++]); case '%s': return String(args[i++]);
case '%d': return Number(args[i++]); case '%d': return Number(args[i++]);
@ -95,7 +96,7 @@ function getCaller3Info() {
var savePrepare = Error.prepareStackTrace; var savePrepare = Error.prepareStackTrace;
Error.stackTraceLimit = 3; Error.stackTraceLimit = 3;
Error.captureStackTrace(this, getCaller3Info); Error.captureStackTrace(this, getCaller3Info);
Error.prepareStackTrace = function(_, stack) { Error.prepareStackTrace = function (_, stack) {
var caller = stack[2]; var caller = stack[2];
obj.file = caller.getFileName(); obj.file = caller.getFileName();
obj.line = caller.getLineNumber(); obj.line = caller.getLineNumber();
@ -155,7 +156,7 @@ var levelFromName = {
}; };
function resolveLevel(nameOrNum) { function resolveLevel(nameOrNum) {
var level = (typeof(nameOrNum) === 'string' var level = (typeof (nameOrNum) === 'string'
? levelFromName[nameOrNum] ? levelFromName[nameOrNum]
: nameOrNum); : nameOrNum);
if (! (TRACE <= level && level <= FATAL)) { if (! (TRACE <= level && level <= FATAL)) {
@ -172,13 +173,13 @@ function resolveLevel(nameOrNum) {
* Create a Logger instance. * Create a Logger instance.
* *
* @param options {Object} See documentation for full details. At minimum * @param options {Object} See documentation for full details. At minimum
* this must include a "name" string key. Configuration keys: * this must include a 'name' string key. Configuration keys:
* - streams: specify the logger output streams. This is an array of * - streams: specify the logger output streams. This is an array of
* objects of the form: * objects of the form:
* { * {
* "level": "info", // optional, "info" default * 'level': 'info', // optional, "info" default
* "stream": process.stdout, // "stream" or "path" is required * 'stream': process.stdout, // 'stream' or "path" is required
* "closeOnExit": false // optional, default depends * 'closeOnExit': false // optional, default depends
* } * }
* See README.md for full details. * See README.md for full details.
* - `level`: set the level for a single output stream (cannot be used * - `level`: set the level for a single output stream (cannot be used
@ -232,7 +233,7 @@ function Logger(options, _childOptions, _childSimple) {
if (options.streams && !Array.isArray(options.streams)) { if (options.streams && !Array.isArray(options.streams)) {
throw new TypeError('invalid options.streams: must be an array') throw new TypeError('invalid options.streams: must be an array')
} }
if (options.serializers && (typeof(options.serializers) !== 'object' if (options.serializers && (typeof (options.serializers) !== 'object'
|| Array.isArray(options.serializers))) { || Array.isArray(options.serializers))) {
throw new TypeError('invalid options.serializers: must be an object') throw new TypeError('invalid options.serializers: must be an object')
} }
@ -290,9 +291,9 @@ function Logger(options, _childOptions, _childSimple) {
var type = s.type; var type = s.type;
if (!s.type) { if (!s.type) {
if (s.stream) { if (s.stream) {
s.type = "stream"; s.type = 'stream';
} else if (s.path) { } else if (s.path) {
s.type = "file" s.type = 'file'
} }
} }
@ -306,12 +307,12 @@ function Logger(options, _childOptions, _childSimple) {
} }
switch (s.type) { switch (s.type) {
case "stream": case 'stream':
if (!s.closeOnExit) { if (!s.closeOnExit) {
s.closeOnExit = false; s.closeOnExit = false;
} }
break; break;
case "file": case 'file':
if (!s.stream) { if (!s.stream) {
s.stream = fs.createWriteStream(s.path, s.stream = fs.createWriteStream(s.path,
{flags: 'a', encoding: 'utf8'}); {flags: 'a', encoding: 'utf8'});
@ -337,7 +338,7 @@ function Logger(options, _childOptions, _childSimple) {
} }
Object.keys(serializers).forEach(function (field) { Object.keys(serializers).forEach(function (field) {
var serializer = serializers[field]; var serializer = serializers[field];
if (typeof(serializer) !== "function") { if (typeof (serializer) !== 'function') {
throw new TypeError(format( throw new TypeError(format(
'invalid serializer for "%s" field: must be a function', field)); 'invalid serializer for "%s" field: must be a function', field));
} else { } else {
@ -349,7 +350,7 @@ function Logger(options, _childOptions, _childSimple) {
// Handle *config* options. // Handle *config* options.
if (options.stream) { if (options.stream) {
addStream({ addStream({
type: "stream", type: 'stream',
stream: options.stream, stream: options.stream,
closeOnExit: false, closeOnExit: false,
level: (options.level ? resolveLevel(options.level) : INFO) level: (options.level ? resolveLevel(options.level) : INFO)
@ -358,7 +359,7 @@ function Logger(options, _childOptions, _childSimple) {
options.streams.forEach(addStream); options.streams.forEach(addStream);
} else if (!parent) { } else if (!parent) {
addStream({ addStream({
type: "stream", type: 'stream',
stream: process.stdout, stream: process.stdout,
closeOnExit: false, closeOnExit: false,
level: (options.level ? resolveLevel(options.level) : INFO) level: (options.level ? resolveLevel(options.level) : INFO)
@ -370,7 +371,7 @@ function Logger(options, _childOptions, _childSimple) {
if (options.src) { if (options.src) {
this.src = true; this.src = true;
} }
xxx("Logger: ", self) xxx('Logger: ', self)
// Fields. // Fields.
// These are the default fields for log records (minus the attributes // These are the default fields for log records (minus the attributes
@ -402,13 +403,13 @@ function Logger(options, _childOptions, _childSimple) {
* Create a child logger, typically to add a few log record fields. * Create a child logger, typically to add a few log record fields.
* *
* This can be useful when passing a logger to a sub-component, e.g. a * This can be useful when passing a logger to a sub-component, e.g. a
* "wuzzle" component of your service: * 'wuzzle' component of your service:
* *
* var wuzzleLog = log.child({component: "wuzzle"}) * var wuzzleLog = log.child({component: 'wuzzle'})
* var wuzzle = new Wuzzle({..., log: wuzzleLog}) * var wuzzle = new Wuzzle({..., log: wuzzleLog})
* *
* Then log records from the wuzzle code will have the same structure as * Then log records from the wuzzle code will have the same structure as
* the app log, *plus the component="wuzzle" field*. * the app log, *plus the component='wuzzle' field*.
* *
* @param options {Object} Optional. Set of options to apply to the child. * @param options {Object} Optional. Set of options to apply to the child.
* All of the same options for a new Logger apply here. Notes: * All of the same options for a new Logger apply here. Notes:
@ -419,7 +420,7 @@ function Logger(options, _childOptions, _childSimple) {
* @param simple {Boolean} Optional. Set to true to assert that `options` * @param simple {Boolean} Optional. Set to true to assert that `options`
* (a) only add fields (no config) and (b) no serialization handling is * (a) only add fields (no config) and (b) no serialization handling is
* required for them. IOW, this is a fast path for frequent child * required for them. IOW, this is a fast path for frequent child
* creation. See "tools/timechild.js" for numbers. * creation. See 'tools/timechild.js' for numbers.
*/ */
Logger.prototype.child = function (options, simple) { Logger.prototype.child = function (options, simple) {
return new Logger(this, options || {}, simple); return new Logger(this, options || {}, simple);
@ -429,7 +430,7 @@ Logger.prototype.child = function (options, simple) {
///** ///**
// * Close this logger. // * Close this logger.
// * // *
// * This closes streams (that it owns, as per "endOnClose" attributes on // * This closes streams (that it owns, as per 'endOnClose' attributes on
// * streams), etc. Typically you **don't** need to bother calling this. // * streams), etc. Typically you **don't** need to bother calling this.
// */ // */
//Logger.prototype.close = function () { //Logger.prototype.close = function () {
@ -439,7 +440,7 @@ Logger.prototype.child = function (options, simple) {
// if (!this._isSimpleChild) { // if (!this._isSimpleChild) {
// self.streams.forEach(function (s) { // self.streams.forEach(function (s) {
// if (s.endOnClose) { // if (s.endOnClose) {
// xxx("closing stream s:", s); // xxx('closing stream s:', s);
// s.stream.end(); // s.stream.end();
// s.endOnClose = false; // s.endOnClose = false;
// } // }
@ -458,7 +459,7 @@ Logger.prototype.child = function (options, simple) {
* *
* Set Usage: * Set Usage:
* log.level(INFO) // set all streams to level INFO * log.level(INFO) // set all streams to level INFO
* log.level("info") // can use "info" et al aliases * log.level('info') // can use 'info' et al aliases
*/ */
Logger.prototype.level = function level(value) { Logger.prototype.level = function level(value) {
if (value === undefined) { if (value === undefined) {
@ -482,12 +483,12 @@ Logger.prototype.level = function level(value) {
* *
* // Returns a level of the identified stream. * // Returns a level of the identified stream.
* log.levels(0) -> TRACE // level of stream at index 0 * log.levels(0) -> TRACE // level of stream at index 0
* log.levels("foo") // level of stream with name "foo" * log.levels('foo') // level of stream with name 'foo'
* *
* Set Usage: * Set Usage:
* log.levels(0, INFO) // set level of stream 0 to INFO * log.levels(0, INFO) // set level of stream 0 to INFO
* log.levels(0, "info") // can use "info" et al aliases * log.levels(0, 'info') // can use 'info' et al aliases
* log.levels("foo", WARN) // set stream named "foo" to WARN * log.levels('foo', WARN) // set stream named 'foo' to WARN
* *
* Stream names: When streams are defined, they can optionally be given * Stream names: When streams are defined, they can optionally be given
* a name. For example, * a name. For example,
@ -501,7 +502,7 @@ Logger.prototype.level = function level(value) {
* ... * ...
* *
* @param name {String|Number} The stream index or name. * @param name {String|Number} The stream index or name.
* @param value {Number|String} The level value (INFO) or alias ("info"). * @param value {Number|String} The level value (INFO) or alias ('info').
* If not given, this is a 'get' operation. * If not given, this is a 'get' operation.
* @throws {Error} If there is no stream with the given name. * @throws {Error} If there is no stream with the given name.
*/ */
@ -615,7 +616,7 @@ Logger.prototype._emit = function (rec) {
obj[k] = recFields[k]; obj[k] = recFields[k];
}); });
} }
xxx("Record:", rec) xxx('Record:', rec)
obj.msg = format.apply(this, rec[3]); obj.msg = format.apply(this, rec[3]);
if (!obj.time) { if (!obj.time) {
obj.time = (new Date()); obj.time = (new Date());
@ -632,8 +633,8 @@ Logger.prototype._emit = function (rec) {
str = JSON.stringify(obj) + '\n'; str = JSON.stringify(obj) + '\n';
} catch (e) { } catch (e) {
var src = ((obj.src && obj.src.file) ? obj.src : getCaller3Info()); var src = ((obj.src && obj.src.file) ? obj.src : getCaller3Info());
var emsg = format("bunyan: ERROR: could not stringify log record from " var emsg = format('bunyan: ERROR: could not stringify log record from '
+ "%s:%d: %s", src.file, src.line, e); + '%s:%d: %s', src.file, src.line, e);
var eobj = objCopy(rec[0]); var eobj = objCopy(rec[0]);
eobj.bunyanMsg = emsg; eobj.bunyanMsg = emsg;
eobj.msg = obj.msg; eobj.msg = obj.msg;
@ -647,7 +648,7 @@ Logger.prototype._emit = function (rec) {
} }
} }
this.streams.forEach(function(s) { this.streams.forEach(function (s) {
if (s.level <= level) { if (s.level <= level) {
xxx('writing log rec "%s" to "%s" stream (%d <= %d)', obj.msg, s.type, xxx('writing log rec "%s" to "%s" stream (%d <= %d)', obj.msg, s.type,
s.level, level); s.level, level);