2012-01-30 06:26:47 +00:00
|
|
|
/*
|
2012-02-08 18:30:13 +00:00
|
|
|
* Copyright (c) 2012 Trent Mick. All rights reserved.
|
|
|
|
*
|
|
|
|
* The bunyan logging library for node.js.
|
2012-01-30 06:26:47 +00:00
|
|
|
*/
|
|
|
|
|
2012-11-13 08:19:42 +00:00
|
|
|
var VERSION = '0.16.7';
|
2012-02-02 17:11:45 +00:00
|
|
|
|
2012-01-30 06:26:47 +00:00
|
|
|
// Bunyan log format version. This becomes the 'v' field on all log records.
|
2012-04-28 00:23:29 +00:00
|
|
|
// `0` is until I release a version '1.0.0' of node-bunyan. Thereafter,
|
2012-01-30 06:26:47 +00:00
|
|
|
// starting with `1`, this will be incremented if there is any backward
|
|
|
|
// incompatible change to the log record format. Details will be in
|
2012-04-28 00:23:29 +00:00
|
|
|
// 'CHANGES.md' (the change log).
|
2012-01-31 00:07:08 +00:00
|
|
|
var LOG_VERSION = 0;
|
2012-01-30 06:26:47 +00:00
|
|
|
|
2012-01-30 20:01:15 +00:00
|
|
|
|
2012-01-31 22:49:53 +00:00
|
|
|
var xxx = function xxx(s) { // internal dev/debug logging
|
2012-02-01 06:36:06 +00:00
|
|
|
var args = ['XX' + 'X: '+s].concat(Array.prototype.slice.call(arguments, 1));
|
2012-01-30 06:26:47 +00:00
|
|
|
console.error.apply(this, args);
|
|
|
|
};
|
2012-01-31 22:49:53 +00:00
|
|
|
var xxx = function xxx() {}; // uncomment to turn of debug logging
|
2012-01-30 06:26:47 +00:00
|
|
|
|
2012-01-30 20:01:15 +00:00
|
|
|
|
2012-01-31 00:07:08 +00:00
|
|
|
var os = require('os');
|
2012-01-30 06:26:47 +00:00
|
|
|
var fs = require('fs');
|
|
|
|
var util = require('util');
|
2012-02-06 23:04:47 +00:00
|
|
|
var assert = require('assert');
|
2012-11-01 05:46:05 +00:00
|
|
|
try {
|
2012-11-02 20:36:46 +00:00
|
|
|
var dtrace = require('dtrace-provider');
|
2012-11-01 05:46:05 +00:00
|
|
|
} catch (e) {
|
|
|
|
dtrace = null;
|
|
|
|
}
|
2012-06-05 03:23:12 +00:00
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2012-01-30 06:26:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---- Internal support stuff
|
|
|
|
|
|
|
|
function objCopy(obj) {
|
2012-02-04 08:08:37 +00:00
|
|
|
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-30 06:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var format = util.format;
|
|
|
|
if (!format) {
|
2012-11-01 05:46:05 +00:00
|
|
|
// If node < 0.6, then use its `util.format`:
|
2012-01-30 06:26:47 +00:00
|
|
|
// <https://github.com/joyent/node/blob/master/lib/util.js#L22>:
|
|
|
|
var inspect = util.inspect;
|
|
|
|
var formatRegExp = /%[sdj%]/g;
|
|
|
|
format = function format(f) {
|
2012-04-28 00:31:46 +00:00
|
|
|
if (typeof (f) !== 'string') {
|
2012-01-30 06:26:47 +00:00
|
|
|
var objects = [];
|
|
|
|
for (var i = 0; i < arguments.length; i++) {
|
|
|
|
objects.push(inspect(arguments[i]));
|
|
|
|
}
|
|
|
|
return objects.join(' ');
|
|
|
|
}
|
2012-02-06 23:04:47 +00:00
|
|
|
|
2012-01-30 06:26:47 +00:00
|
|
|
var i = 1;
|
|
|
|
var args = arguments;
|
|
|
|
var len = args.length;
|
2012-04-28 00:23:29 +00:00
|
|
|
var str = String(f).replace(formatRegExp, function (x) {
|
|
|
|
if (i >= len)
|
|
|
|
return x;
|
2012-01-30 06:26:47 +00:00
|
|
|
switch (x) {
|
|
|
|
case '%s': return String(args[i++]);
|
|
|
|
case '%d': return Number(args[i++]);
|
2012-09-10 18:24:39 +00:00
|
|
|
case '%j': return JSON.stringify(args[i++], safeCycles());
|
2012-01-30 06:26:47 +00:00
|
|
|
case '%%': return '%';
|
|
|
|
default:
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
for (var x = args[i]; i < len; x = args[++i]) {
|
2012-04-28 00:31:46 +00:00
|
|
|
if (x === null || typeof (x) !== 'object') {
|
2012-01-30 06:26:47 +00:00
|
|
|
str += ' ' + x;
|
|
|
|
} else {
|
|
|
|
str += ' ' + inspect(x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-06 04:33:57 +00:00
|
|
|
/**
|
|
|
|
* Gather some caller info 3 stack levels up.
|
|
|
|
* See <http://code.google.com/p/v8/wiki/JavaScriptStackTraceApi>.
|
|
|
|
*/
|
|
|
|
function getCaller3Info() {
|
|
|
|
var obj = {};
|
|
|
|
var saveLimit = Error.stackTraceLimit;
|
|
|
|
var savePrepare = Error.prepareStackTrace;
|
|
|
|
Error.stackTraceLimit = 3;
|
|
|
|
Error.captureStackTrace(this, getCaller3Info);
|
2012-04-28 00:23:29 +00:00
|
|
|
Error.prepareStackTrace = function (_, stack) {
|
2012-02-06 04:33:57 +00:00
|
|
|
var caller = stack[2];
|
|
|
|
obj.file = caller.getFileName();
|
|
|
|
obj.line = caller.getLineNumber();
|
|
|
|
var func = caller.getFunctionName();
|
|
|
|
if (func)
|
|
|
|
obj.func = func;
|
|
|
|
};
|
|
|
|
this.stack;
|
|
|
|
Error.stackTraceLimit = saveLimit;
|
|
|
|
Error.prepareStackTrace = savePrepare;
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-07 05:34:04 +00:00
|
|
|
/**
|
|
|
|
* Warn about an bunyan processing error.
|
|
|
|
*
|
|
|
|
* If file/line are given, this makes an attempt to warn on stderr only once.
|
|
|
|
*
|
|
|
|
* @param msg {String} Message with which to warn.
|
|
|
|
* @param file {String} Optional. File path relevant for the warning.
|
|
|
|
* @param line {String} Optional. Line number in `file` path relevant for
|
|
|
|
* the warning.
|
|
|
|
*/
|
|
|
|
function _warn(msg, file, line) {
|
|
|
|
assert.ok(msg);
|
|
|
|
var key;
|
|
|
|
if (file && line) {
|
|
|
|
key = file + ':' + line;
|
|
|
|
if (_warned[key]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_warned[key] = true;
|
|
|
|
}
|
|
|
|
process.stderr.write(msg + '\n');
|
|
|
|
}
|
|
|
|
var _warned = {};
|
|
|
|
|
2012-02-06 04:33:57 +00:00
|
|
|
|
2012-01-30 06:26:47 +00:00
|
|
|
|
|
|
|
//---- 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-30 06:26:47 +00:00
|
|
|
|
|
|
|
var levelFromName = {
|
2012-01-31 22:43:13 +00:00
|
|
|
'trace': TRACE,
|
2012-01-30 06:26:47 +00:00
|
|
|
'debug': DEBUG,
|
|
|
|
'info': INFO,
|
|
|
|
'warn': WARN,
|
|
|
|
'error': ERROR,
|
|
|
|
'fatal': FATAL
|
|
|
|
};
|
|
|
|
|
2012-11-01 05:46:05 +00:00
|
|
|
// Dtrace probes.
|
2012-10-31 07:28:24 +00:00
|
|
|
var dtp = undefined;
|
2012-11-01 05:46:05 +00:00
|
|
|
var probes = dtrace && {};
|
2012-08-24 23:28:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve a level number, name (upper or lowercase) to a level number value.
|
|
|
|
*
|
|
|
|
* @api public
|
|
|
|
*/
|
2012-02-04 08:08:37 +00:00
|
|
|
function resolveLevel(nameOrNum) {
|
2012-04-28 00:23:29 +00:00
|
|
|
var level = (typeof (nameOrNum) === 'string'
|
2012-08-24 23:28:31 +00:00
|
|
|
? levelFromName[nameOrNum.toLowerCase()]
|
2012-01-30 22:28:02 +00:00
|
|
|
: nameOrNum);
|
2012-02-04 08:08:37 +00:00
|
|
|
if (! (TRACE <= level && level <= FATAL)) {
|
|
|
|
throw new Error('invalid level: ' + nameOrNum);
|
|
|
|
}
|
|
|
|
return level;
|
2012-01-30 22:28:02 +00:00
|
|
|
}
|
|
|
|
|
2012-01-30 06:26:47 +00:00
|
|
|
|
2012-02-01 06:36:06 +00:00
|
|
|
|
2012-01-30 06:26:47 +00:00
|
|
|
//---- Logger class
|
|
|
|
|
2012-02-01 06:36:06 +00:00
|
|
|
/**
|
|
|
|
* Create a Logger instance.
|
|
|
|
*
|
|
|
|
* @param options {Object} See documentation for full details. At minimum
|
2012-04-28 00:23:29 +00:00
|
|
|
* this must include a 'name' string key. Configuration keys:
|
2012-06-21 21:49:04 +00:00
|
|
|
* - `streams`: specify the logger output streams. This is an array of
|
|
|
|
* objects with these fields:
|
|
|
|
* - `type`: The stream type. See README.md for full details.
|
|
|
|
* Often this is implied by the other fields. Examples are
|
|
|
|
* "file", "stream" and "raw".
|
|
|
|
* - `level`: Defaults to "info".
|
|
|
|
* - `path` or `stream`: The specify the file path or writeable
|
|
|
|
* stream to which log records are written. E.g.
|
|
|
|
* `stream: process.stdout`.
|
|
|
|
* - `closeOnExit` (boolean): Optional. Default is true for a
|
|
|
|
* "file" stream when `path` is given, false otherwise.
|
2012-02-04 08:08:37 +00:00
|
|
|
* See README.md for full details.
|
|
|
|
* - `level`: set the level for a single output stream (cannot be used
|
|
|
|
* with `streams`)
|
|
|
|
* - `stream`: the output stream for a logger with just one, e.g.
|
|
|
|
* `process.stdout` (cannot be used with `streams`)
|
|
|
|
* - `serializers`: object mapping log record field names to
|
|
|
|
* serializing functions. See README.md for details.
|
2012-02-06 04:33:57 +00:00
|
|
|
* - `src`: Boolean (default false). Set true to enable 'src' automatic
|
|
|
|
* field with log call source info.
|
2012-02-04 08:08:37 +00:00
|
|
|
* All other keys are log record fields.
|
|
|
|
*
|
|
|
|
* An alternative *internal* call signature is used for creating a child:
|
2012-02-05 05:42:47 +00:00
|
|
|
* new Logger(<parent logger>, <child options>[, <child opts are simple>]);
|
|
|
|
*
|
|
|
|
* @param _childSimple (Boolean) An assertion that the given `_childOptions`
|
|
|
|
* (a) only add fields (no config) and (b) no serialization handling is
|
|
|
|
* required for them. IOW, this is a fast path for frequent child
|
|
|
|
* creation.
|
2012-02-01 06:36:06 +00:00
|
|
|
*/
|
2012-02-05 05:42:47 +00:00
|
|
|
function Logger(options, _childOptions, _childSimple) {
|
2012-02-01 06:36:06 +00:00
|
|
|
xxx('Logger start:', options)
|
2012-01-30 06:26:47 +00:00
|
|
|
if (! this instanceof Logger) {
|
2012-02-04 08:08:37 +00:00
|
|
|
return new Logger(options, _childOptions);
|
2012-01-30 06:26:47 +00:00
|
|
|
}
|
2012-02-06 23:04:47 +00:00
|
|
|
|
2012-02-04 08:08:37 +00:00
|
|
|
// Input arg validation.
|
|
|
|
var parent;
|
|
|
|
if (_childOptions !== undefined) {
|
|
|
|
parent = options;
|
|
|
|
options = _childOptions;
|
|
|
|
if (! parent instanceof Logger) {
|
|
|
|
throw new TypeError('invalid Logger creation: do not pass a second arg');
|
|
|
|
}
|
|
|
|
}
|
2012-01-30 20:01:15 +00:00
|
|
|
if (!options) {
|
2012-01-30 22:28:02 +00:00
|
|
|
throw new TypeError('options (object) is required');
|
|
|
|
}
|
2012-02-08 18:30:13 +00:00
|
|
|
if (!parent) {
|
|
|
|
if (!options.name) {
|
|
|
|
throw new TypeError('options.name (string) is required');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (options.name) {
|
|
|
|
throw new TypeError('invalid options.name: child cannot set logger name');
|
|
|
|
}
|
2012-02-06 23:13:50 +00:00
|
|
|
}
|
2012-10-23 05:30:00 +00:00
|
|
|
if (options.stream && options.streams) {
|
|
|
|
throw new TypeError('cannot mix "streams" and "stream" options');
|
2012-01-30 20:01:15 +00:00
|
|
|
}
|
2012-02-08 18:30:13 +00:00
|
|
|
if (options.streams && !Array.isArray(options.streams)) {
|
|
|
|
throw new TypeError('invalid options.streams: must be an array')
|
|
|
|
}
|
2012-04-28 00:31:46 +00:00
|
|
|
if (options.serializers && (typeof (options.serializers) !== 'object' ||
|
|
|
|
Array.isArray(options.serializers))) {
|
2012-02-08 18:30:13 +00:00
|
|
|
throw new TypeError('invalid options.serializers: must be an object')
|
|
|
|
}
|
2012-02-06 23:04:47 +00:00
|
|
|
|
2012-06-05 06:13:50 +00:00
|
|
|
EventEmitter.call(this);
|
|
|
|
|
2012-02-05 05:42:47 +00:00
|
|
|
// Fast path for simple child creation.
|
|
|
|
if (parent && _childSimple) {
|
2012-02-28 00:46:25 +00:00
|
|
|
// `_isSimpleChild` is a signal to stream close handling that this child
|
|
|
|
// owns none of its streams.
|
2012-02-05 05:42:47 +00:00
|
|
|
this._isSimpleChild = true;
|
|
|
|
|
2012-02-06 23:04:47 +00:00
|
|
|
this._level = parent._level;
|
2012-02-05 05:42:47 +00:00
|
|
|
this.streams = parent.streams;
|
|
|
|
this.serializers = parent.serializers;
|
2012-02-06 04:33:57 +00:00
|
|
|
this.src = parent.src;
|
2012-02-28 00:46:25 +00:00
|
|
|
var fields = this.fields = {};
|
|
|
|
var parentFieldNames = Object.keys(parent.fields);
|
|
|
|
for (var i = 0; i < parentFieldNames.length; i++) {
|
|
|
|
var name = parentFieldNames[i];
|
|
|
|
fields[name] = parent.fields[name];
|
|
|
|
}
|
2012-02-05 05:42:47 +00:00
|
|
|
var names = Object.keys(options);
|
|
|
|
for (var i = 0; i < names.length; i++) {
|
|
|
|
var name = names[i];
|
2012-02-28 00:46:25 +00:00
|
|
|
fields[name] = options[name];
|
2012-02-05 05:42:47 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-02-04 08:08:37 +00:00
|
|
|
// Null values.
|
|
|
|
var self = this;
|
|
|
|
if (parent) {
|
2012-02-06 23:04:47 +00:00
|
|
|
this._level = parent._level;
|
2012-02-04 08:08:37 +00:00
|
|
|
this.streams = [];
|
2012-02-05 05:42:47 +00:00
|
|
|
for (var i = 0; i < parent.streams.length; i++) {
|
|
|
|
var s = objCopy(parent.streams[i]);
|
2012-02-04 08:08:37 +00:00
|
|
|
s.closeOnExit = false; // Don't own parent stream.
|
2012-02-05 05:42:47 +00:00
|
|
|
this.streams.push(s);
|
|
|
|
}
|
2012-02-04 08:08:37 +00:00
|
|
|
this.serializers = objCopy(parent.serializers);
|
2012-02-06 04:33:57 +00:00
|
|
|
this.src = parent.src;
|
2012-02-04 08:08:37 +00:00
|
|
|
this.fields = objCopy(parent.fields);
|
2012-10-23 05:30:00 +00:00
|
|
|
if (options.level) {
|
|
|
|
this.level(options.level);
|
|
|
|
}
|
2012-01-30 06:26:47 +00:00
|
|
|
} else {
|
2012-02-06 23:04:47 +00:00
|
|
|
this._level = Number.POSITIVE_INFINITY;
|
2012-02-04 08:08:37 +00:00
|
|
|
this.streams = [];
|
|
|
|
this.serializers = null;
|
2012-02-06 04:33:57 +00:00
|
|
|
this.src = false;
|
2012-02-04 08:08:37 +00:00
|
|
|
this.fields = {};
|
2012-01-30 06:26:47 +00:00
|
|
|
}
|
2012-02-06 23:04:47 +00:00
|
|
|
|
2012-11-01 05:46:05 +00:00
|
|
|
if (!dtp && dtrace) {
|
2012-10-31 07:28:24 +00:00
|
|
|
dtp = dtrace.createDTraceProvider('bunyan');
|
|
|
|
|
|
|
|
for (var level in levelFromName) {
|
|
|
|
var probe;
|
|
|
|
|
2012-11-01 05:17:44 +00:00
|
|
|
probes[levelFromName[level]] = probe =
|
2012-10-31 07:28:24 +00:00
|
|
|
dtp.addProbe('log-' + level, 'char *');
|
|
|
|
|
|
|
|
// Explicitly add a reference to dtp to prevent it from being GC'd
|
|
|
|
probe.dtp = dtp;
|
|
|
|
}
|
|
|
|
|
|
|
|
dtp.enable();
|
|
|
|
}
|
|
|
|
|
2012-02-04 08:08:37 +00:00
|
|
|
// Helpers
|
|
|
|
function addStream(s) {
|
|
|
|
s = objCopy(s);
|
|
|
|
|
|
|
|
// Implicit 'type' from other args.
|
2012-02-24 17:13:39 +00:00
|
|
|
var type = s.type;
|
2012-02-04 08:08:37 +00:00
|
|
|
if (!s.type) {
|
|
|
|
if (s.stream) {
|
2012-04-28 00:23:29 +00:00
|
|
|
s.type = 'stream';
|
2012-02-04 08:08:37 +00:00
|
|
|
} else if (s.path) {
|
2012-04-28 00:23:29 +00:00
|
|
|
s.type = 'file'
|
2012-02-04 08:08:37 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-21 21:49:04 +00:00
|
|
|
s.raw = (s.type === 'raw'); // PERF: Allow for faster check in `_emit`.
|
2012-02-01 06:36:06 +00:00
|
|
|
|
2012-02-04 08:08:37 +00:00
|
|
|
if (s.level) {
|
|
|
|
s.level = resolveLevel(s.level);
|
2012-10-23 05:30:00 +00:00
|
|
|
} else if (options.level) {
|
|
|
|
s.level = resolveLevel(options.level);
|
2012-02-04 08:08:37 +00:00
|
|
|
} else {
|
2012-02-05 05:42:47 +00:00
|
|
|
s.level = INFO;
|
2012-02-04 08:08:37 +00:00
|
|
|
}
|
2012-02-06 23:04:47 +00:00
|
|
|
if (s.level < self._level) {
|
|
|
|
self._level = s.level;
|
2012-01-30 22:28:02 +00:00
|
|
|
}
|
|
|
|
|
2012-02-04 08:08:37 +00:00
|
|
|
switch (s.type) {
|
2012-04-28 00:23:29 +00:00
|
|
|
case 'stream':
|
2012-02-04 08:08:37 +00:00
|
|
|
if (!s.closeOnExit) {
|
|
|
|
s.closeOnExit = false;
|
2012-01-30 22:28:02 +00:00
|
|
|
}
|
2012-02-04 08:08:37 +00:00
|
|
|
break;
|
2012-04-28 00:23:29 +00:00
|
|
|
case 'file':
|
2012-02-04 08:08:37 +00:00
|
|
|
if (!s.stream) {
|
|
|
|
s.stream = fs.createWriteStream(s.path,
|
2012-06-05 06:13:50 +00:00
|
|
|
{flags: 'a', encoding: 'utf8'});
|
|
|
|
s.stream.on('error', function (err) {
|
|
|
|
self.emit('error', err, s);
|
|
|
|
});
|
2012-01-30 22:28:02 +00:00
|
|
|
if (!s.closeOnExit) {
|
2012-02-04 08:08:37 +00:00
|
|
|
s.closeOnExit = true;
|
2012-01-30 22:28:02 +00:00
|
|
|
}
|
2012-02-04 08:08:37 +00:00
|
|
|
} else {
|
|
|
|
if (!s.closeOnExit) {
|
|
|
|
s.closeOnExit = false;
|
2012-01-30 22:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-04 08:08:37 +00:00
|
|
|
break;
|
2012-06-21 21:49:04 +00:00
|
|
|
case 'raw':
|
|
|
|
if (!s.closeOnExit) {
|
|
|
|
s.closeOnExit = false;
|
|
|
|
}
|
|
|
|
break;
|
2012-02-04 08:08:37 +00:00
|
|
|
default:
|
|
|
|
throw new TypeError('unknown stream type "' + s.type + '"');
|
2012-01-30 22:28:02 +00:00
|
|
|
}
|
2012-02-04 08:08:37 +00:00
|
|
|
|
|
|
|
self.streams.push(s);
|
2012-01-30 22:28:02 +00:00
|
|
|
}
|
2012-02-04 08:08:37 +00:00
|
|
|
|
|
|
|
function addSerializers(serializers) {
|
|
|
|
if (!self.serializers) {
|
|
|
|
self.serializers = {};
|
|
|
|
}
|
|
|
|
Object.keys(serializers).forEach(function (field) {
|
|
|
|
var serializer = serializers[field];
|
2012-04-28 00:23:29 +00:00
|
|
|
if (typeof (serializer) !== 'function') {
|
2012-02-01 06:36:06 +00:00
|
|
|
throw new TypeError(format(
|
|
|
|
'invalid serializer for "%s" field: must be a function', field));
|
|
|
|
} else {
|
|
|
|
self.serializers[field] = serializer;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2012-02-04 08:08:37 +00:00
|
|
|
|
2012-10-23 05:30:00 +00:00
|
|
|
// Handle *config* options (i.e. options that are not just plain data
|
|
|
|
// for log records).
|
2012-02-04 08:08:37 +00:00
|
|
|
if (options.stream) {
|
|
|
|
addStream({
|
2012-04-28 00:23:29 +00:00
|
|
|
type: 'stream',
|
2012-02-04 08:08:37 +00:00
|
|
|
stream: options.stream,
|
|
|
|
closeOnExit: false,
|
2012-06-21 21:49:04 +00:00
|
|
|
level: (options.level ? resolveLevel(options.level) : INFO)
|
2012-02-04 08:08:37 +00:00
|
|
|
});
|
|
|
|
} else if (options.streams) {
|
|
|
|
options.streams.forEach(addStream);
|
2012-10-23 05:30:00 +00:00
|
|
|
} else if (parent && options.level) {
|
|
|
|
this.level(options.level);
|
2012-02-04 08:08:37 +00:00
|
|
|
} else if (!parent) {
|
|
|
|
addStream({
|
2012-04-28 00:23:29 +00:00
|
|
|
type: 'stream',
|
2012-02-04 08:08:37 +00:00
|
|
|
stream: process.stdout,
|
|
|
|
closeOnExit: false,
|
2012-06-21 21:49:04 +00:00
|
|
|
level: (options.level ? resolveLevel(options.level) : INFO)
|
2012-02-04 08:08:37 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
if (options.serializers) {
|
|
|
|
addSerializers(options.serializers);
|
|
|
|
}
|
2012-02-06 04:33:57 +00:00
|
|
|
if (options.src) {
|
|
|
|
this.src = true;
|
|
|
|
}
|
2012-04-28 00:23:29 +00:00
|
|
|
xxx('Logger: ', self)
|
2012-02-06 23:04:47 +00:00
|
|
|
|
2012-02-04 08:08:37 +00:00
|
|
|
// Fields.
|
|
|
|
// These are the default fields for log records (minus the attributes
|
|
|
|
// removed in this constructor). To allow storing raw log records
|
|
|
|
// (unrendered), `this.fields` must never be mutated. Create a copy for
|
|
|
|
// any changes.
|
|
|
|
var fields = objCopy(options);
|
|
|
|
delete fields.stream;
|
|
|
|
delete fields.level;
|
|
|
|
delete fields.streams;
|
|
|
|
delete fields.serializers;
|
2012-02-06 04:33:57 +00:00
|
|
|
delete fields.src;
|
2012-02-01 06:36:06 +00:00
|
|
|
if (this.serializers) {
|
2012-02-04 08:08:37 +00:00
|
|
|
this._applySerializers(fields);
|
2012-02-01 06:36:06 +00:00
|
|
|
}
|
2012-02-04 08:08:37 +00:00
|
|
|
if (!fields.hostname) {
|
|
|
|
fields.hostname = os.hostname();
|
2012-01-31 00:07:08 +00:00
|
|
|
}
|
2012-02-10 05:07:01 +00:00
|
|
|
if (!fields.pid) {
|
|
|
|
fields.pid = process.pid;
|
|
|
|
}
|
2012-02-04 08:08:37 +00:00
|
|
|
Object.keys(fields).forEach(function (k) {
|
|
|
|
self.fields[k] = fields[k];
|
|
|
|
});
|
2012-01-30 20:01:15 +00:00
|
|
|
}
|
|
|
|
|
2012-06-05 06:13:50 +00:00
|
|
|
util.inherits(Logger, EventEmitter);
|
|
|
|
|
2012-01-30 20:01:15 +00:00
|
|
|
|
|
|
|
/**
|
2012-02-04 08:08:37 +00:00
|
|
|
* Create a child logger, typically to add a few log record fields.
|
2012-01-30 20:01:15 +00:00
|
|
|
*
|
|
|
|
* This can be useful when passing a logger to a sub-component, e.g. a
|
2012-04-28 00:23:29 +00:00
|
|
|
* 'wuzzle' component of your service:
|
2012-01-30 20:01:15 +00:00
|
|
|
*
|
2012-04-28 00:23:29 +00:00
|
|
|
* var wuzzleLog = log.child({component: 'wuzzle'})
|
2012-01-30 20:01:15 +00:00
|
|
|
* var wuzzle = new Wuzzle({..., log: wuzzleLog})
|
|
|
|
*
|
|
|
|
* Then log records from the wuzzle code will have the same structure as
|
2012-04-28 00:23:29 +00:00
|
|
|
* the app log, *plus the component='wuzzle' field*.
|
2012-01-30 20:01:15 +00:00
|
|
|
*
|
2012-02-04 08:08:37 +00:00
|
|
|
* @param options {Object} Optional. Set of options to apply to the child.
|
|
|
|
* All of the same options for a new Logger apply here. Notes:
|
|
|
|
* - The parent's streams are inherited and cannot be removed in this
|
2012-10-23 05:30:00 +00:00
|
|
|
* call. Any given `streams` are *added* to the set inherited from
|
|
|
|
* the parent.
|
2012-02-04 08:08:37 +00:00
|
|
|
* - The parent's serializers are inherited, though can effectively be
|
|
|
|
* overwritten by using duplicate keys.
|
2012-10-23 05:30:00 +00:00
|
|
|
* - Can use `level` to set the level of the streams inherited from
|
|
|
|
* the parent. The level for the parent is NOT affected.
|
2012-02-05 05:42:47 +00:00
|
|
|
* @param simple {Boolean} Optional. Set to true to assert that `options`
|
|
|
|
* (a) only add fields (no config) and (b) no serialization handling is
|
|
|
|
* required for them. IOW, this is a fast path for frequent child
|
2012-04-28 00:23:29 +00:00
|
|
|
* creation. See 'tools/timechild.js' for numbers.
|
2012-01-30 20:01:15 +00:00
|
|
|
*/
|
2012-02-05 05:42:47 +00:00
|
|
|
Logger.prototype.child = function (options, simple) {
|
|
|
|
return new Logger(this, options || {}, simple);
|
2012-02-01 06:36:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-28 00:31:46 +00:00
|
|
|
/* BEGIN JSSTYLED */
|
|
|
|
/**
|
|
|
|
* Close this logger.
|
|
|
|
*
|
|
|
|
* This closes streams (that it owns, as per 'endOnClose' attributes on
|
|
|
|
* streams), etc. Typically you **don't** need to bother calling this.
|
|
|
|
Logger.prototype.close = function () {
|
|
|
|
if (this._closed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!this._isSimpleChild) {
|
|
|
|
self.streams.forEach(function (s) {
|
|
|
|
if (s.endOnClose) {
|
|
|
|
xxx('closing stream s:', s);
|
|
|
|
s.stream.end();
|
|
|
|
s.endOnClose = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this._closed = true;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
/* END JSSTYLED */
|
2012-02-04 08:08:37 +00:00
|
|
|
|
|
|
|
|
2012-02-06 23:04:47 +00:00
|
|
|
/**
|
|
|
|
* Get/set the level of all streams on this logger.
|
|
|
|
*
|
|
|
|
* Get Usage:
|
|
|
|
* // Returns the current log level (lowest level of all its streams).
|
|
|
|
* log.level() -> INFO
|
|
|
|
*
|
|
|
|
* Set Usage:
|
|
|
|
* log.level(INFO) // set all streams to level INFO
|
2012-04-28 00:23:29 +00:00
|
|
|
* log.level('info') // can use 'info' et al aliases
|
2012-02-06 23:04:47 +00:00
|
|
|
*/
|
|
|
|
Logger.prototype.level = function level(value) {
|
|
|
|
if (value === undefined) {
|
|
|
|
return this._level;
|
|
|
|
}
|
|
|
|
var newLevel = resolveLevel(value);
|
|
|
|
var len = this.streams.length;
|
|
|
|
for (var i = 0; i < len; i++) {
|
|
|
|
this.streams[i].level = newLevel;
|
|
|
|
}
|
|
|
|
this._level = newLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get/set the level of a particular stream on this logger.
|
|
|
|
*
|
|
|
|
* Get Usage:
|
|
|
|
* // Returns an array of the levels of each stream.
|
|
|
|
* log.levels() -> [TRACE, INFO]
|
|
|
|
*
|
|
|
|
* // Returns a level of the identified stream.
|
|
|
|
* log.levels(0) -> TRACE // level of stream at index 0
|
2012-04-28 00:23:29 +00:00
|
|
|
* log.levels('foo') // level of stream with name 'foo'
|
2012-02-06 23:04:47 +00:00
|
|
|
*
|
|
|
|
* Set Usage:
|
|
|
|
* log.levels(0, INFO) // set level of stream 0 to INFO
|
2012-04-28 00:23:29 +00:00
|
|
|
* log.levels(0, 'info') // can use 'info' et al aliases
|
|
|
|
* log.levels('foo', WARN) // set stream named 'foo' to WARN
|
2012-02-06 23:04:47 +00:00
|
|
|
*
|
|
|
|
* Stream names: When streams are defined, they can optionally be given
|
|
|
|
* a name. For example,
|
|
|
|
* log = new Logger({
|
|
|
|
* streams: [
|
|
|
|
* {
|
2012-04-28 00:31:46 +00:00
|
|
|
* name: 'foo',
|
2012-02-06 23:04:47 +00:00
|
|
|
* path: '/var/log/my-service/foo.log'
|
|
|
|
* level: 'trace'
|
|
|
|
* },
|
|
|
|
* ...
|
|
|
|
*
|
|
|
|
* @param name {String|Number} The stream index or name.
|
2012-04-28 00:23:29 +00:00
|
|
|
* @param value {Number|String} The level value (INFO) or alias ('info').
|
2012-02-06 23:04:47 +00:00
|
|
|
* If not given, this is a 'get' operation.
|
|
|
|
* @throws {Error} If there is no stream with the given name.
|
|
|
|
*/
|
|
|
|
Logger.prototype.levels = function levels(name, value) {
|
|
|
|
if (name === undefined) {
|
|
|
|
assert.equal(value, undefined);
|
2012-04-28 00:31:46 +00:00
|
|
|
return this.streams.map(
|
|
|
|
function (s) { return s.level });
|
2012-02-06 23:04:47 +00:00
|
|
|
}
|
|
|
|
var stream;
|
2012-04-28 00:31:46 +00:00
|
|
|
if (typeof (name) === 'number') {
|
2012-02-06 23:04:47 +00:00
|
|
|
stream = this.streams[name];
|
|
|
|
if (stream === undefined) {
|
|
|
|
throw new Error('invalid stream index: ' + name);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var len = this.streams.length;
|
|
|
|
for (var i = 0; i < len; i++) {
|
|
|
|
var s = this.streams[i];
|
|
|
|
if (s.name === name) {
|
|
|
|
stream = s;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!stream) {
|
|
|
|
throw new Error(format('no stream with name "%s"', name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (value === undefined) {
|
|
|
|
return stream.level;
|
|
|
|
} else {
|
|
|
|
var newLevel = resolveLevel(value);
|
|
|
|
stream.level = newLevel;
|
|
|
|
if (newLevel < this._level) {
|
|
|
|
this._level = newLevel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-01 06:36:06 +00:00
|
|
|
/**
|
|
|
|
* Apply registered serializers to the appropriate keys in the given fields.
|
|
|
|
*
|
|
|
|
* Pre-condition: This is only called if there is at least one serializer.
|
|
|
|
*
|
|
|
|
* @param fields (Object) The log record fields.
|
|
|
|
* @param keys (Array) Optional array of keys to which to limit processing.
|
|
|
|
*/
|
|
|
|
Logger.prototype._applySerializers = function (fields, keys) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
// Mapping of keys to potentially serialize.
|
|
|
|
var applyKeys = fields;
|
|
|
|
if (keys) {
|
|
|
|
applyKeys = {};
|
2012-04-28 00:31:46 +00:00
|
|
|
for (var i = 0; i < keys.length; i++) {
|
2012-02-01 06:36:06 +00:00
|
|
|
applyKeys[keys[i]] = true;
|
|
|
|
}
|
|
|
|
}
|
2012-02-06 23:04:47 +00:00
|
|
|
|
2012-02-01 06:36:06 +00:00
|
|
|
xxx('_applySerializers: applyKeys', applyKeys);
|
2012-02-06 23:04:47 +00:00
|
|
|
|
2012-02-01 06:36:06 +00:00
|
|
|
// Check each serializer against these (presuming number of serializers
|
|
|
|
// is typically less than number of fields).
|
|
|
|
Object.keys(this.serializers).forEach(function (name) {
|
|
|
|
if (applyKeys[name]) {
|
|
|
|
xxx('_applySerializers; apply to "%s" key', name)
|
|
|
|
try {
|
|
|
|
fields[name] = self.serializers[name](fields[name]);
|
|
|
|
} catch (err) {
|
2012-02-07 05:34:04 +00:00
|
|
|
_warn(format('bunyan: ERROR: This should never happen. '
|
2012-02-01 06:36:06 +00:00
|
|
|
+ 'This is a bug in <https://github.com/trentm/node-bunyan> or '
|
|
|
|
+ 'in this application. Exception from "%s" Logger serializer: %s',
|
|
|
|
name, err.stack || err));
|
|
|
|
fields[name] = format('(Error in Bunyan log "%s" serializer '
|
|
|
|
+ 'broke field. See stderr for details.)', name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2012-01-30 06:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A log record is a 4-tuple:
|
|
|
|
* [<default fields object>,
|
|
|
|
* <log record fields object>,
|
|
|
|
* <level integer>,
|
|
|
|
* <msg args array>]
|
|
|
|
* For Perf reasons, we only render this down to a single object when
|
|
|
|
* it is emitted.
|
|
|
|
*/
|
|
|
|
Logger.prototype._mkRecord = function (fields, level, msgArgs) {
|
|
|
|
var recFields = (fields ? objCopy(fields) : null);
|
|
|
|
return [this.fields, recFields, level, msgArgs];
|
|
|
|
}
|
|
|
|
|
2012-01-30 20:01:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Emit a log record.
|
|
|
|
*
|
|
|
|
* @param rec {log record}
|
2012-11-01 05:17:44 +00:00
|
|
|
* @param noemit {Boolean} Optional. Set to true to skip emission
|
|
|
|
* and just return the JSON string.
|
2012-01-30 20:01:15 +00:00
|
|
|
*/
|
2012-10-31 07:28:24 +00:00
|
|
|
Logger.prototype._emit = function (rec, noemit) {
|
2012-06-20 23:04:23 +00:00
|
|
|
var i;
|
|
|
|
|
|
|
|
// Lazily determine if this Logger has non-"raw" streams. If there are
|
|
|
|
// any, then we need to stringify the log record.
|
|
|
|
if (this.haveNonRawStreams === undefined) {
|
|
|
|
this.haveNonRawStreams = false;
|
|
|
|
for (i = 0; i < this.streams.length; i++) {
|
|
|
|
if (!this.streams[i].raw) {
|
|
|
|
this.haveNonRawStreams = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-07 05:34:04 +00:00
|
|
|
// Stringify the object. Attempt to warn/recover on error.
|
|
|
|
var str;
|
2012-11-01 05:17:44 +00:00
|
|
|
if (noemit || this.haveNonRawStreams) {
|
|
|
|
str = JSON.stringify(rec, safeCycles()) + '\n';
|
2012-02-07 05:34:04 +00:00
|
|
|
}
|
|
|
|
|
2012-10-31 07:28:24 +00:00
|
|
|
if (noemit)
|
|
|
|
return str;
|
|
|
|
|
2012-11-01 05:17:44 +00:00
|
|
|
var level = rec.level;
|
2012-06-20 23:04:23 +00:00
|
|
|
for (i = 0; i < this.streams.length; i++) {
|
|
|
|
var s = this.streams[i];
|
2012-01-30 22:28:02 +00:00
|
|
|
if (s.level <= level) {
|
2012-06-21 21:49:04 +00:00
|
|
|
xxx('writing log rec "%s" to "%s" stream (%d <= %d): %j',
|
2012-11-01 05:17:44 +00:00
|
|
|
rec.msg, s.type, s.level, level, rec);
|
|
|
|
s.stream.write(s.raw ? rec : str);
|
2012-01-30 22:28:02 +00:00
|
|
|
}
|
2012-06-20 23:04:23 +00:00
|
|
|
};
|
2012-10-31 07:28:24 +00:00
|
|
|
|
|
|
|
return str;
|
2012-01-30 06:26:47 +00:00
|
|
|
}
|
|
|
|
|
2012-10-24 16:04:57 +00:00
|
|
|
|
2012-01-30 20:40:57 +00:00
|
|
|
/**
|
2012-10-24 16:04:57 +00:00
|
|
|
* Build a log emitter function for level minLevel. I.e. this is the
|
|
|
|
* creator of `log.info`, `log.error`, etc.
|
2012-01-30 20:40:57 +00:00
|
|
|
*/
|
2012-10-24 16:04:57 +00:00
|
|
|
function mkLogEmitter(minLevel) {
|
2012-10-24 06:06:44 +00:00
|
|
|
return function () {
|
2012-10-31 07:28:24 +00:00
|
|
|
var log = this;
|
|
|
|
|
2012-11-01 05:17:44 +00:00
|
|
|
function mkRecord(args) {
|
2012-10-31 07:28:24 +00:00
|
|
|
if (args[0] instanceof Error) {
|
|
|
|
// `log.<level>(err, ...)`
|
|
|
|
fields = {err: errSerializer(args[0])};
|
|
|
|
if (args.length === 1) {
|
|
|
|
msgArgs = [fields.err.message];
|
|
|
|
} else {
|
|
|
|
msgArgs = Array.prototype.slice.call(args, 1);
|
|
|
|
}
|
|
|
|
} else if (typeof (args[0]) === 'string') { // `log.<level>(msg, ...)`
|
|
|
|
fields = null;
|
|
|
|
msgArgs = Array.prototype.slice.call(args);
|
|
|
|
} else if (Buffer.isBuffer(args[0])) { // `log.<level>(buf, ...)`
|
|
|
|
// Almost certainly an error, show `inspect(buf)`. See bunyan issue #35.
|
|
|
|
fields = null;
|
|
|
|
msgArgs = Array.prototype.slice.call(args);
|
|
|
|
msgArgs[0] = util.inspect(msgArgs[0]);
|
|
|
|
} else { // `log.<level>(fields, msg, ...)`
|
|
|
|
fields = args[0];
|
|
|
|
msgArgs = Array.prototype.slice.call(args, 1);
|
|
|
|
}
|
2012-11-01 05:17:44 +00:00
|
|
|
|
|
|
|
// Build up the record object.
|
|
|
|
var rec = objCopy(log.fields);
|
|
|
|
var level = rec.level = minLevel;
|
|
|
|
var recFields = (fields ? objCopy(fields) : null);
|
|
|
|
if (recFields) {
|
|
|
|
if (log.serializers) {
|
|
|
|
log._applySerializers(recFields);
|
|
|
|
}
|
|
|
|
Object.keys(recFields).forEach(function (k) {
|
|
|
|
rec[k] = recFields[k];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
rec.msg = format.apply(log, msgArgs);
|
|
|
|
if (!rec.time) {
|
|
|
|
rec.time = (new Date());
|
|
|
|
}
|
|
|
|
// Get call source info
|
|
|
|
if (log.src && !rec.src) {
|
|
|
|
rec.src = getCaller3Info()
|
|
|
|
}
|
|
|
|
rec.v = LOG_VERSION;
|
|
|
|
|
|
|
|
return rec;
|
2012-10-31 07:28:24 +00:00
|
|
|
};
|
|
|
|
|
2012-11-01 05:17:44 +00:00
|
|
|
var fields = null;
|
|
|
|
var msgArgs = arguments;
|
|
|
|
var str = null;
|
|
|
|
var rec = null;
|
2012-10-24 06:06:44 +00:00
|
|
|
if (arguments.length === 0) { // `log.<level>()`
|
|
|
|
return (this._level <= minLevel);
|
|
|
|
} else if (this._level > minLevel) {
|
2012-11-01 05:17:44 +00:00
|
|
|
/* pass through */
|
2012-10-31 07:28:24 +00:00
|
|
|
} else {
|
2012-11-01 05:17:44 +00:00
|
|
|
rec = mkRecord(msgArgs);
|
|
|
|
str = this._emit(rec);
|
2012-02-17 00:49:19 +00:00
|
|
|
}
|
2012-11-01 05:46:05 +00:00
|
|
|
probes && probes[minLevel].fire(function () {
|
2012-11-01 05:17:44 +00:00
|
|
|
return [ str ||
|
|
|
|
(rec && log._emit(rec, true)) ||
|
|
|
|
log._emit(mkRecord(msgArgs), true) ];
|
|
|
|
});
|
2012-10-24 06:06:44 +00:00
|
|
|
}
|
2012-01-30 20:40:57 +00:00
|
|
|
}
|
|
|
|
|
2012-10-24 16:04:57 +00:00
|
|
|
|
2012-01-30 20:40:57 +00:00
|
|
|
/**
|
2012-10-24 06:06:44 +00:00
|
|
|
* The functions below log a record at a specific level.
|
2012-01-30 20:40:57 +00:00
|
|
|
*
|
|
|
|
* Usages:
|
2012-10-24 06:06:44 +00:00
|
|
|
* log.<level>() -> boolean is-trace-enabled
|
|
|
|
* log.<level>(<Error> err, [<string> msg, ...])
|
|
|
|
* log.<level>(<string> msg, ...)
|
|
|
|
* log.<level>(<object> fields, <string> msg, ...)
|
2012-01-30 20:40:57 +00:00
|
|
|
*
|
2012-10-24 16:04:57 +00:00
|
|
|
* where <level> is the lowercase version of the log level. E.g.:
|
2012-01-30 20:40:57 +00:00
|
|
|
*
|
2012-10-24 16:04:57 +00:00
|
|
|
* log.info()
|
2012-01-30 20:40:57 +00:00
|
|
|
*
|
|
|
|
* @params fields {Object} Optional set of additional fields to log.
|
|
|
|
* @params msg {String} Log message. This can be followed by additional
|
|
|
|
* arguments that are handled like
|
|
|
|
* [util.format](http://nodejs.org/docs/latest/api/all.html#util.format).
|
|
|
|
*/
|
2012-10-24 16:04:57 +00:00
|
|
|
Logger.prototype.trace = mkLogEmitter(TRACE);
|
|
|
|
Logger.prototype.debug = mkLogEmitter(DEBUG);
|
|
|
|
Logger.prototype.info = mkLogEmitter(INFO);
|
|
|
|
Logger.prototype.warn = mkLogEmitter(WARN);
|
|
|
|
Logger.prototype.error = mkLogEmitter(ERROR);
|
|
|
|
Logger.prototype.fatal = mkLogEmitter(FATAL);
|
2012-01-30 20:40:57 +00:00
|
|
|
|
2012-01-30 20:01:15 +00:00
|
|
|
|
2012-02-01 06:36:06 +00:00
|
|
|
|
|
|
|
//---- Standard serializers
|
|
|
|
// A serializer is a function that serializes a JavaScript object to a
|
|
|
|
// JSON representation for logging. There is a standard set of presumed
|
|
|
|
// interesting objects in node.js-land.
|
|
|
|
|
|
|
|
Logger.stdSerializers = {};
|
|
|
|
|
|
|
|
// Serialize an HTTP request.
|
|
|
|
Logger.stdSerializers.req = function req(req) {
|
2012-08-08 05:34:46 +00:00
|
|
|
if (!req || !req.connection)
|
|
|
|
return req;
|
2012-02-01 06:36:06 +00:00
|
|
|
return {
|
|
|
|
method: req.method,
|
|
|
|
url: req.url,
|
2012-02-04 01:05:13 +00:00
|
|
|
headers: req.headers,
|
|
|
|
remoteAddress: req.connection.remoteAddress,
|
|
|
|
remotePort: req.connection.remotePort
|
|
|
|
};
|
|
|
|
// Trailers: Skipping for speed. If you need trailers in your app, then
|
2012-06-21 21:58:42 +00:00
|
|
|
// make a custom serializer.
|
2012-02-04 01:05:13 +00:00
|
|
|
//if (Object.keys(trailers).length > 0) {
|
|
|
|
// obj.trailers = req.trailers;
|
|
|
|
//}
|
2012-02-01 06:36:06 +00:00
|
|
|
};
|
|
|
|
|
2012-02-02 05:33:18 +00:00
|
|
|
// Serialize an HTTP response.
|
|
|
|
Logger.stdSerializers.res = function res(res) {
|
2012-08-08 05:34:46 +00:00
|
|
|
if (!res || !res.statusCode)
|
|
|
|
return res;
|
2012-02-02 05:33:18 +00:00
|
|
|
return {
|
|
|
|
statusCode: res.statusCode,
|
2012-02-04 01:05:13 +00:00
|
|
|
header: res._header
|
2012-02-02 05:33:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-11 00:09:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This function dumps long stack traces for exceptions having a cause()
|
|
|
|
* method. The error classes from
|
|
|
|
* [verror](https://github.com/davepacheco/node-verror) and
|
|
|
|
* [restify v2.0](https://github.com/mcavage/node-restify) are examples.
|
|
|
|
*
|
|
|
|
* Based on `dumpException` in
|
|
|
|
* https://github.com/davepacheco/node-extsprintf/blob/master/lib/extsprintf.js
|
|
|
|
*/
|
|
|
|
function getFullErrorStack(ex)
|
|
|
|
{
|
|
|
|
var ret = ex.stack || ex.toString();
|
2012-10-11 21:02:48 +00:00
|
|
|
if (ex.cause && typeof(ex.cause) === 'function') {
|
|
|
|
var cex = ex.cause();
|
|
|
|
if (cex) {
|
|
|
|
ret += '\nCaused by: ' + getFullErrorStack(cex);
|
|
|
|
}
|
|
|
|
}
|
2012-10-11 00:09:39 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2012-02-04 08:08:37 +00:00
|
|
|
// Serialize an Error object
|
|
|
|
// (Core error properties are enumerable in node 0.4, not in 0.6).
|
2012-02-06 17:10:11 +00:00
|
|
|
var errSerializer = Logger.stdSerializers.err = function err(err) {
|
2012-08-08 05:34:46 +00:00
|
|
|
if (!err || !err.stack)
|
|
|
|
return err;
|
2012-02-04 08:08:37 +00:00
|
|
|
var obj = {
|
|
|
|
message: err.message,
|
|
|
|
name: err.name,
|
2012-10-11 00:09:39 +00:00
|
|
|
stack: getFullErrorStack(err)
|
2012-02-04 08:08:37 +00:00
|
|
|
}
|
|
|
|
Object.keys(err).forEach(function (k) {
|
|
|
|
if (err[k] !== undefined) {
|
|
|
|
obj[k] = err[k];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
|
2012-10-11 00:09:39 +00:00
|
|
|
|
2012-09-10 18:24:39 +00:00
|
|
|
// A JSON stringifier that handles cycles safely.
|
|
|
|
// Usage: JSON.stringify(obj, safeCycles())
|
|
|
|
function safeCycles() {
|
|
|
|
var seen = [];
|
|
|
|
return function(key, val) {
|
|
|
|
if (!val || typeof val !== 'object') {
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
if (seen.indexOf(val) !== -1) {
|
|
|
|
return '[Circular]';
|
|
|
|
}
|
|
|
|
seen.push(val);
|
|
|
|
return val;
|
|
|
|
};
|
|
|
|
}
|
2012-02-01 06:36:06 +00:00
|
|
|
|
2012-06-19 21:36:02 +00:00
|
|
|
/**
|
2012-06-21 21:53:05 +00:00
|
|
|
* RingBuffer is a Writable Stream that just stores the last N records in
|
2012-06-19 21:36:02 +00:00
|
|
|
* memory.
|
|
|
|
*
|
|
|
|
* @param options {Object}, with the following fields:
|
|
|
|
*
|
2012-06-21 21:53:05 +00:00
|
|
|
* - limit: number of records to keep in memory
|
2012-06-19 21:36:02 +00:00
|
|
|
*/
|
|
|
|
function RingBuffer(options) {
|
|
|
|
this.limit = options && options.limit ? options.limit : 100;
|
|
|
|
this.writable = true;
|
2012-06-21 21:53:05 +00:00
|
|
|
this.records = [];
|
2012-06-19 21:36:02 +00:00
|
|
|
EventEmitter.call(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(RingBuffer, EventEmitter);
|
|
|
|
|
2012-06-20 23:26:28 +00:00
|
|
|
RingBuffer.prototype.write = function (record) {
|
2012-06-19 21:36:02 +00:00
|
|
|
if (!this.writable)
|
|
|
|
throw (new Error('RingBuffer has been ended already'));
|
|
|
|
|
2012-06-21 21:53:05 +00:00
|
|
|
this.records.push(record);
|
2012-06-19 21:36:02 +00:00
|
|
|
|
2012-06-21 21:53:05 +00:00
|
|
|
if (this.records.length > this.limit)
|
|
|
|
this.records.shift();
|
2012-06-19 21:36:02 +00:00
|
|
|
|
|
|
|
return (true);
|
|
|
|
};
|
|
|
|
|
|
|
|
RingBuffer.prototype.end = function () {
|
|
|
|
if (arguments.length > 0)
|
|
|
|
this.write.apply(this, Array.prototype.slice.call(arguments));
|
|
|
|
this.writable = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
RingBuffer.prototype.destroy = function () {
|
|
|
|
this.writable = false;
|
|
|
|
this.emit('close');
|
|
|
|
};
|
|
|
|
|
|
|
|
RingBuffer.prototype.destroySoon = function () {
|
|
|
|
this.destroy();
|
|
|
|
};
|
|
|
|
|
2012-02-01 06:36:06 +00:00
|
|
|
|
|
|
|
//---- Exports
|
|
|
|
|
2012-01-30 06:26:47 +00:00
|
|
|
module.exports = Logger;
|
|
|
|
|
2012-02-06 23:04:47 +00:00
|
|
|
module.exports.TRACE = TRACE;
|
|
|
|
module.exports.DEBUG = DEBUG;
|
|
|
|
module.exports.INFO = INFO;
|
|
|
|
module.exports.WARN = WARN;
|
|
|
|
module.exports.ERROR = ERROR;
|
|
|
|
module.exports.FATAL = FATAL;
|
2012-08-24 23:28:31 +00:00
|
|
|
module.exports.resolveLevel = resolveLevel;
|
2012-01-30 06:26:47 +00:00
|
|
|
|
2012-02-06 23:04:47 +00:00
|
|
|
module.exports.VERSION = VERSION;
|
|
|
|
module.exports.LOG_VERSION = LOG_VERSION;
|
2012-04-27 23:20:57 +00:00
|
|
|
|
|
|
|
module.exports.createLogger = function createLogger(options) {
|
|
|
|
return new Logger(options);
|
|
|
|
};
|
2012-06-19 21:36:02 +00:00
|
|
|
|
|
|
|
module.exports.RingBuffer = RingBuffer;
|
2012-10-24 17:54:18 +00:00
|
|
|
|
|
|
|
// Useful for custom `type == "raw"` streams that may do JSON stringification
|
|
|
|
// of log records themselves. Usage:
|
|
|
|
// var str = JSON.stringify(rec, bunyan.safeCycles());
|
|
|
|
module.exports.safeCycles = safeCycles;
|