2014-08-01 22:57:48 +00:00
|
|
|
/**
|
2015-09-07 21:37:29 +00:00
|
|
|
* Copyright (c) 2015 Trent Mick.
|
|
|
|
* Copyright (c) 2015 Joyent Inc.
|
2012-02-08 18:30:13 +00:00
|
|
|
*
|
|
|
|
* The bunyan logging library for node.js.
|
2013-11-29 22:49:23 +00:00
|
|
|
*
|
2014-08-01 22:57:48 +00:00
|
|
|
* -*- mode: js -*-
|
2013-11-29 22:49:23 +00:00
|
|
|
* vim: expandtab:ts=4:sw=4
|
2012-01-30 06:26:47 +00:00
|
|
|
*/
|
|
|
|
|
2016-03-17 05:52:27 +00:00
|
|
|
var VERSION = '1.8.1';
|
2012-02-02 17:11:45 +00:00
|
|
|
|
2015-09-07 21:37:29 +00:00
|
|
|
/*
|
|
|
|
* Bunyan log format version. This becomes the 'v' field on all log records.
|
|
|
|
* This will be incremented if there is any backward incompatible change to
|
|
|
|
* the log record format. Details will be in '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
|
2013-04-02 00:21:01 +00:00
|
|
|
var args = ['XX' + 'X: '+s].concat(
|
|
|
|
Array.prototype.slice.call(arguments, 1));
|
2013-03-29 00:42:32 +00:00
|
|
|
console.error.apply(this, args);
|
2012-01-30 06:26:47 +00:00
|
|
|
};
|
2013-03-15 18:25:56 +00:00
|
|
|
var xxx = function xxx() {}; // comment out to turn on debug logging
|
2012-01-30 06:26:47 +00:00
|
|
|
|
2012-01-30 20:01:15 +00:00
|
|
|
|
2016-02-21 21:31:57 +00:00
|
|
|
/*
|
|
|
|
* Runtime environment notes:
|
|
|
|
*
|
|
|
|
* Bunyan is intended to run in a number of runtime environments. Here are
|
|
|
|
* some notes on differences for those envs and how the code copes.
|
|
|
|
*
|
|
|
|
* - node.js: The primary target environment.
|
|
|
|
* - NW.js: http://nwjs.io/ An *app* environment that feels like both a
|
|
|
|
* node env -- it has node-like globals (`process`, `global`) and
|
|
|
|
* browser-like globals (`window`, `navigator`). My *understanding* is that
|
|
|
|
* bunyan can operate as if this is vanilla node.js.
|
|
|
|
* - browser: Failing the above, we sniff using the `window` global
|
|
|
|
* <https://developer.mozilla.org/en-US/docs/Web/API/Window/window>.
|
|
|
|
* - browserify: http://browserify.org/ A browser-targetting bundler of
|
|
|
|
* node.js deps. The runtime is a browser env, so can't use fs access,
|
|
|
|
* etc. Browserify's build looks for `require(<single-string>)` imports
|
|
|
|
* to bundle. For some imports it won't be able to handle, we "hide"
|
|
|
|
* from browserify with `require('frobshizzle' + '')`.
|
|
|
|
* - Other? Please open issues if things are broken.
|
|
|
|
*/
|
|
|
|
var runtimeEnv;
|
|
|
|
if (typeof (process) !== 'undefined' && process.versions) {
|
|
|
|
if (process.versions.nw) {
|
|
|
|
runtimeEnv = 'nw';
|
|
|
|
} else if (process.versions.node) {
|
|
|
|
runtimeEnv = 'node';
|
2015-07-27 21:02:56 +00:00
|
|
|
}
|
2016-02-21 21:31:57 +00:00
|
|
|
}
|
|
|
|
if (!runtimeEnv && typeof (window) !== 'undefined' &&
|
|
|
|
window.window === window) {
|
|
|
|
runtimeEnv = 'browser';
|
|
|
|
}
|
|
|
|
if (!runtimeEnv) {
|
|
|
|
throw new Error('unknown runtime environment');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var os, fs, dtrace;
|
|
|
|
if (runtimeEnv === 'browser') {
|
|
|
|
os = {
|
2015-09-07 07:29:17 +00:00
|
|
|
hostname: function () {
|
|
|
|
return window.location.host;
|
|
|
|
}
|
|
|
|
};
|
2016-02-21 21:31:57 +00:00
|
|
|
fs = {};
|
|
|
|
dtrace = null;
|
|
|
|
} else {
|
|
|
|
os = require('os');
|
|
|
|
fs = require('fs');
|
|
|
|
try {
|
|
|
|
dtrace = require('dtrace-provider' + '');
|
|
|
|
} catch (e) {
|
|
|
|
dtrace = null;
|
|
|
|
}
|
2012-11-01 05:46:05 +00:00
|
|
|
}
|
2015-07-27 21:02:56 +00:00
|
|
|
var util = require('util');
|
|
|
|
var assert = require('assert');
|
2012-06-05 03:23:12 +00:00
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2016-02-20 07:58:37 +00:00
|
|
|
var stream = require('stream');
|
2012-01-30 06:26:47 +00:00
|
|
|
|
2015-01-19 07:16:35 +00:00
|
|
|
try {
|
|
|
|
var safeJsonStringify = require('safe-json-stringify');
|
|
|
|
} catch (e) {
|
|
|
|
safeJsonStringify = null;
|
|
|
|
}
|
2015-01-19 07:27:28 +00:00
|
|
|
if (process.env.BUNYAN_TEST_NO_SAFE_JSON_STRINGIFY) {
|
|
|
|
safeJsonStringify = null;
|
|
|
|
}
|
2015-01-19 07:16:35 +00:00
|
|
|
|
2013-01-04 22:30:27 +00:00
|
|
|
// The 'mv' module is required for rotating-file stream support.
|
|
|
|
try {
|
2014-09-22 04:20:43 +00:00
|
|
|
var mv = require('mv' + '');
|
2013-01-04 22:30:27 +00:00
|
|
|
} catch (e) {
|
2013-03-29 00:42:32 +00:00
|
|
|
mv = null;
|
2013-01-04 22:30:27 +00:00
|
|
|
}
|
|
|
|
|
2015-01-18 05:15:38 +00:00
|
|
|
try {
|
2015-02-20 04:41:34 +00:00
|
|
|
var sourceMapSupport = require('source-map-support' + '');
|
2015-01-18 05:15:38 +00:00
|
|
|
} catch (_) {
|
|
|
|
sourceMapSupport = null;
|
|
|
|
}
|
|
|
|
|
2012-01-30 06:26:47 +00:00
|
|
|
|
|
|
|
//---- Internal support stuff
|
|
|
|
|
2014-12-08 18:43:33 +00:00
|
|
|
/**
|
|
|
|
* A shallow copy of an object. Bunyan logging attempts to never cause
|
|
|
|
* exceptions, so this function attempts to handle non-objects gracefully.
|
|
|
|
*/
|
2012-01-30 06:26:47 +00:00
|
|
|
function objCopy(obj) {
|
2014-12-08 18:43:33 +00:00
|
|
|
if (obj == null) { // null or undefined
|
2014-12-06 03:23:43 +00:00
|
|
|
return obj;
|
2013-03-29 00:42:32 +00:00
|
|
|
} else if (Array.isArray(obj)) {
|
|
|
|
return obj.slice();
|
2015-01-16 06:19:59 +00:00
|
|
|
} else if (typeof (obj) === 'object') {
|
2013-03-29 00:42:32 +00:00
|
|
|
var copy = {};
|
|
|
|
Object.keys(obj).forEach(function (k) {
|
|
|
|
copy[k] = obj[k];
|
|
|
|
});
|
|
|
|
return copy;
|
2014-12-06 03:23:43 +00:00
|
|
|
} else {
|
|
|
|
return obj;
|
2013-03-29 00:42:32 +00:00
|
|
|
}
|
2012-01-30 06:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var format = util.format;
|
|
|
|
if (!format) {
|
2013-03-29 00:42:32 +00:00
|
|
|
// If 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-06 23:04:47 +00:00
|
|
|
|
2013-03-29 00:42:32 +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++], safeCycles());
|
|
|
|
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-01-30 06:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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() {
|
2015-09-07 21:37:29 +00:00
|
|
|
if (this === undefined) {
|
|
|
|
// Cannot access caller info in 'strict' mode.
|
|
|
|
return;
|
|
|
|
}
|
2013-03-29 00:42:32 +00:00
|
|
|
var obj = {};
|
|
|
|
var saveLimit = Error.stackTraceLimit;
|
|
|
|
var savePrepare = Error.prepareStackTrace;
|
|
|
|
Error.stackTraceLimit = 3;
|
|
|
|
Error.captureStackTrace(this, getCaller3Info);
|
2015-01-18 05:15:38 +00:00
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
Error.prepareStackTrace = function (_, stack) {
|
|
|
|
var caller = stack[2];
|
2015-01-18 05:15:38 +00:00
|
|
|
if (sourceMapSupport) {
|
|
|
|
caller = sourceMapSupport.wrapCallSite(caller);
|
|
|
|
}
|
2013-03-29 00:42:32 +00:00
|
|
|
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-06 04:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-19 07:27:28 +00:00
|
|
|
function _indent(s, indent) {
|
|
|
|
if (!indent) indent = ' ';
|
|
|
|
var lines = s.split(/\r?\n/g);
|
|
|
|
return indent + lines.join('\n' + indent);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-07 05:34:04 +00:00
|
|
|
/**
|
|
|
|
* Warn about an bunyan processing error.
|
|
|
|
*
|
|
|
|
* @param msg {String} Message with which to warn.
|
2014-12-08 18:43:33 +00:00
|
|
|
* @param dedupKey {String} Optional. A short string key for this warning to
|
|
|
|
* have its warning only printed once.
|
2012-02-07 05:34:04 +00:00
|
|
|
*/
|
2014-12-08 18:43:33 +00:00
|
|
|
function _warn(msg, dedupKey) {
|
2013-03-29 00:42:32 +00:00
|
|
|
assert.ok(msg);
|
2014-12-08 18:43:33 +00:00
|
|
|
if (dedupKey) {
|
|
|
|
if (_warned[dedupKey]) {
|
2013-03-29 00:42:32 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-12-08 18:43:33 +00:00
|
|
|
_warned[dedupKey] = true;
|
2012-02-07 05:34:04 +00:00
|
|
|
}
|
2013-03-29 00:42:32 +00:00
|
|
|
process.stderr.write(msg + '\n');
|
2012-02-07 05:34:04 +00:00
|
|
|
}
|
2014-12-08 18:43:33 +00:00
|
|
|
function _haveWarned(dedupKey) {
|
|
|
|
return _warned[dedupKey];
|
|
|
|
}
|
2012-02-07 05:34:04 +00:00
|
|
|
var _warned = {};
|
|
|
|
|
2012-02-06 04:33:57 +00:00
|
|
|
|
2014-09-22 04:20:43 +00:00
|
|
|
function ConsoleRawStream() {}
|
|
|
|
ConsoleRawStream.prototype.write = function (rec) {
|
|
|
|
if (rec.level < INFO) {
|
|
|
|
console.log(rec);
|
|
|
|
} else if (rec.level < WARN) {
|
|
|
|
console.info(rec);
|
|
|
|
} else if (rec.level < ERROR) {
|
|
|
|
console.warn(rec);
|
|
|
|
} else {
|
|
|
|
console.error(rec);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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 = {
|
2013-03-29 00:42:32 +00:00
|
|
|
'trace': TRACE,
|
|
|
|
'debug': DEBUG,
|
|
|
|
'info': INFO,
|
|
|
|
'warn': WARN,
|
|
|
|
'error': ERROR,
|
|
|
|
'fatal': FATAL
|
2012-01-30 06:26:47 +00:00
|
|
|
};
|
2015-01-16 06:18:29 +00:00
|
|
|
var nameFromLevel = {};
|
|
|
|
Object.keys(levelFromName).forEach(function (name) {
|
|
|
|
nameFromLevel[levelFromName[name]] = name;
|
|
|
|
});
|
2012-01-30 06:26:47 +00:00
|
|
|
|
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.
|
|
|
|
*
|
2016-02-03 07:12:02 +00:00
|
|
|
* @param nameOrNum {String|Number} A level name (case-insensitive) or positive
|
|
|
|
* integer level.
|
2012-08-24 23:28:31 +00:00
|
|
|
* @api public
|
|
|
|
*/
|
2012-02-04 08:08:37 +00:00
|
|
|
function resolveLevel(nameOrNum) {
|
2016-02-03 07:12:02 +00:00
|
|
|
var level;
|
|
|
|
var type = typeof (nameOrNum);
|
|
|
|
if (type === 'string') {
|
|
|
|
level = levelFromName[nameOrNum.toLowerCase()];
|
|
|
|
if (!level) {
|
|
|
|
throw new Error(format('unknown level name: "%s"', nameOrNum));
|
|
|
|
}
|
|
|
|
} else if (type !== 'number') {
|
|
|
|
throw new TypeError(format('cannot resolve level: invalid arg (%s):',
|
|
|
|
type, nameOrNum));
|
|
|
|
} else if (nameOrNum < 0 || Math.floor(nameOrNum) !== nameOrNum) {
|
|
|
|
throw new TypeError(format('level is not a positive integer: %s',
|
|
|
|
nameOrNum));
|
|
|
|
} else {
|
|
|
|
level = nameOrNum;
|
|
|
|
}
|
2013-03-29 00:42:32 +00:00
|
|
|
return level;
|
2012-01-30 22:28:02 +00:00
|
|
|
}
|
|
|
|
|
2012-01-30 06:26:47 +00:00
|
|
|
|
2016-02-20 07:58:37 +00:00
|
|
|
function isWritable(obj) {
|
|
|
|
if (obj instanceof stream.Writable) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return typeof (obj.write) === 'function';
|
|
|
|
}
|
|
|
|
|
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
|
2013-03-29 00:25:01 +00:00
|
|
|
* 'file', 'stream' and "raw".
|
|
|
|
* - `level`: Defaults to 'info'.
|
2012-06-21 21:49:04 +00:00
|
|
|
* - `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
|
2013-03-29 00:25:01 +00:00
|
|
|
* '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) {
|
2013-03-29 00:42:32 +00:00
|
|
|
xxx('Logger start:', options)
|
2014-08-09 01:15:55 +00:00
|
|
|
if (!(this instanceof Logger)) {
|
2013-03-29 00:42:32 +00:00
|
|
|
return new Logger(options, _childOptions);
|
2012-02-04 08:08:37 +00:00
|
|
|
}
|
2013-03-29 00:42:32 +00:00
|
|
|
|
|
|
|
// Input arg validation.
|
|
|
|
var parent;
|
|
|
|
if (_childOptions !== undefined) {
|
|
|
|
parent = options;
|
|
|
|
options = _childOptions;
|
2014-08-09 01:17:37 +00:00
|
|
|
if (!(parent instanceof Logger)) {
|
2013-04-02 00:21:01 +00:00
|
|
|
throw new TypeError(
|
|
|
|
'invalid Logger creation: do not pass a second arg');
|
2013-03-29 00:42:32 +00:00
|
|
|
}
|
2012-02-08 18:30:13 +00:00
|
|
|
}
|
2013-03-29 00:42:32 +00:00
|
|
|
if (!options) {
|
|
|
|
throw new TypeError('options (object) is required');
|
2012-02-08 18:30:13 +00:00
|
|
|
}
|
2013-03-29 00:42:32 +00:00
|
|
|
if (!parent) {
|
|
|
|
if (!options.name) {
|
|
|
|
throw new TypeError('options.name (string) is required');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (options.name) {
|
2013-04-02 00:21:01 +00:00
|
|
|
throw new TypeError(
|
|
|
|
'invalid options.name: child cannot set logger name');
|
2013-03-29 00:42:32 +00:00
|
|
|
}
|
2012-02-28 00:46:25 +00:00
|
|
|
}
|
2013-03-29 00:42:32 +00:00
|
|
|
if (options.stream && options.streams) {
|
|
|
|
throw new TypeError('cannot mix "streams" and "stream" options');
|
2012-02-05 05:42:47 +00:00
|
|
|
}
|
2013-03-29 00:42:32 +00:00
|
|
|
if (options.streams && !Array.isArray(options.streams)) {
|
|
|
|
throw new TypeError('invalid options.streams: must be an array')
|
2012-02-05 05:42:47 +00:00
|
|
|
}
|
2013-03-29 00:42:32 +00:00
|
|
|
if (options.serializers && (typeof (options.serializers) !== 'object' ||
|
|
|
|
Array.isArray(options.serializers))) {
|
|
|
|
throw new TypeError('invalid options.serializers: must be an object')
|
2012-10-23 05:30:00 +00:00
|
|
|
}
|
2012-02-06 23:04:47 +00:00
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
EventEmitter.call(this);
|
|
|
|
|
|
|
|
// Fast path for simple child creation.
|
|
|
|
if (parent && _childSimple) {
|
|
|
|
// `_isSimpleChild` is a signal to stream close handling that this child
|
|
|
|
// owns none of its streams.
|
|
|
|
this._isSimpleChild = true;
|
|
|
|
|
|
|
|
this._level = parent._level;
|
|
|
|
this.streams = parent.streams;
|
|
|
|
this.serializers = parent.serializers;
|
|
|
|
this.src = parent.src;
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
var names = Object.keys(options);
|
|
|
|
for (var i = 0; i < names.length; i++) {
|
|
|
|
var name = names[i];
|
|
|
|
fields[name] = options[name];
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2012-10-31 07:28:24 +00:00
|
|
|
|
2015-07-26 05:29:53 +00:00
|
|
|
// Start values.
|
2013-03-29 00:42:32 +00:00
|
|
|
var self = this;
|
|
|
|
if (parent) {
|
|
|
|
this._level = parent._level;
|
|
|
|
this.streams = [];
|
|
|
|
for (var i = 0; i < parent.streams.length; i++) {
|
|
|
|
var s = objCopy(parent.streams[i]);
|
|
|
|
s.closeOnExit = false; // Don't own parent stream.
|
|
|
|
this.streams.push(s);
|
|
|
|
}
|
|
|
|
this.serializers = objCopy(parent.serializers);
|
|
|
|
this.src = parent.src;
|
|
|
|
this.fields = objCopy(parent.fields);
|
|
|
|
if (options.level) {
|
|
|
|
this.level(options.level);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this._level = Number.POSITIVE_INFINITY;
|
|
|
|
this.streams = [];
|
|
|
|
this.serializers = null;
|
|
|
|
this.src = false;
|
|
|
|
this.fields = {};
|
|
|
|
}
|
2012-10-31 07:28:24 +00:00
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
if (!dtp && dtrace) {
|
|
|
|
dtp = dtrace.createDTraceProvider('bunyan');
|
2012-10-31 07:28:24 +00:00
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
for (var level in levelFromName) {
|
|
|
|
var probe;
|
2012-10-31 07:28:24 +00:00
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
probes[levelFromName[level]] = probe =
|
|
|
|
dtp.addProbe('log-' + level, 'char *');
|
2012-02-01 06:36:06 +00:00
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
// Explicitly add a reference to dtp to prevent it from being GC'd
|
|
|
|
probe.dtp = dtp;
|
|
|
|
}
|
|
|
|
|
|
|
|
dtp.enable();
|
2012-01-30 22:28:02 +00:00
|
|
|
}
|
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
// Handle *config* options (i.e. options that are not just plain data
|
|
|
|
// for log records).
|
|
|
|
if (options.stream) {
|
2014-04-17 12:37:44 +00:00
|
|
|
self.addStream({
|
2013-03-29 00:42:32 +00:00
|
|
|
type: 'stream',
|
|
|
|
stream: options.stream,
|
|
|
|
closeOnExit: false,
|
2015-01-18 04:39:06 +00:00
|
|
|
level: options.level
|
2013-03-29 00:42:32 +00:00
|
|
|
});
|
|
|
|
} else if (options.streams) {
|
2015-01-18 04:39:06 +00:00
|
|
|
options.streams.forEach(function (s) {
|
|
|
|
self.addStream(s, options.level);
|
|
|
|
});
|
2013-03-29 00:42:32 +00:00
|
|
|
} else if (parent && options.level) {
|
|
|
|
this.level(options.level);
|
|
|
|
} else if (!parent) {
|
2016-02-21 21:31:57 +00:00
|
|
|
if (runtimeEnv === 'browser') {
|
2014-09-22 04:20:43 +00:00
|
|
|
/*
|
|
|
|
* In the browser we'll be emitting to console.log by default.
|
|
|
|
* Any console.log worth its salt these days can nicely render
|
|
|
|
* and introspect objects (e.g. the Firefox and Chrome console)
|
|
|
|
* so let's emit the raw log record. Are there browsers for which
|
|
|
|
* that breaks things?
|
|
|
|
*/
|
|
|
|
self.addStream({
|
|
|
|
type: 'raw',
|
|
|
|
stream: new ConsoleRawStream(),
|
|
|
|
closeOnExit: false,
|
2015-01-18 04:39:06 +00:00
|
|
|
level: options.level
|
2014-09-22 04:20:43 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
self.addStream({
|
|
|
|
type: 'stream',
|
|
|
|
stream: process.stdout,
|
|
|
|
closeOnExit: false,
|
2015-01-18 04:39:06 +00:00
|
|
|
level: options.level
|
2014-09-22 04:20:43 +00:00
|
|
|
});
|
|
|
|
}
|
2012-02-04 08:08:37 +00:00
|
|
|
}
|
2013-03-29 00:42:32 +00:00
|
|
|
if (options.serializers) {
|
2014-04-17 12:37:44 +00:00
|
|
|
self.addSerializers(options.serializers);
|
2013-03-29 00:42:32 +00:00
|
|
|
}
|
|
|
|
if (options.src) {
|
|
|
|
this.src = true;
|
|
|
|
}
|
|
|
|
xxx('Logger: ', self)
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
delete fields.src;
|
|
|
|
if (this.serializers) {
|
|
|
|
this._applySerializers(fields);
|
|
|
|
}
|
2016-02-03 07:45:31 +00:00
|
|
|
if (!fields.hostname && !self.fields.hostname) {
|
2013-03-29 00:42:32 +00:00
|
|
|
fields.hostname = os.hostname();
|
|
|
|
}
|
|
|
|
if (!fields.pid) {
|
|
|
|
fields.pid = process.pid;
|
|
|
|
}
|
|
|
|
Object.keys(fields).forEach(function (k) {
|
|
|
|
self.fields[k] = fields[k];
|
2012-02-04 08:08:37 +00:00
|
|
|
});
|
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
|
|
|
|
2014-04-17 12:37:44 +00:00
|
|
|
/**
|
|
|
|
* Add a stream
|
|
|
|
*
|
|
|
|
* @param stream {Object}. Object 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".
|
|
|
|
* - `path` or `stream`: The specify the file path or writeable
|
|
|
|
* stream to which log records are written. E.g.
|
|
|
|
* `stream: process.stdout`.
|
2015-01-18 04:39:06 +00:00
|
|
|
* - `level`: Optional. Falls back to `defaultLevel`.
|
2014-04-17 12:37:44 +00:00
|
|
|
* - `closeOnExit` (boolean): Optional. Default is true for a
|
|
|
|
* 'file' stream when `path` is given, false otherwise.
|
|
|
|
* See README.md for full details.
|
2015-01-18 04:39:06 +00:00
|
|
|
* @param defaultLevel {Number|String} Optional. A level to use if
|
|
|
|
* `stream.level` is not set. If neither is given, this defaults to INFO.
|
2014-04-17 12:37:44 +00:00
|
|
|
*/
|
2015-01-18 04:39:06 +00:00
|
|
|
Logger.prototype.addStream = function addStream(s, defaultLevel) {
|
2014-04-17 12:37:44 +00:00
|
|
|
var self = this;
|
2015-01-18 04:39:06 +00:00
|
|
|
if (defaultLevel === null || defaultLevel === undefined) {
|
|
|
|
defaultLevel = INFO;
|
|
|
|
}
|
2014-04-17 12:37:44 +00:00
|
|
|
|
|
|
|
s = objCopy(s);
|
|
|
|
|
|
|
|
// Implicit 'type' from other args.
|
|
|
|
if (!s.type) {
|
|
|
|
if (s.stream) {
|
|
|
|
s.type = 'stream';
|
|
|
|
} else if (s.path) {
|
|
|
|
s.type = 'file'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.raw = (s.type === 'raw'); // PERF: Allow for faster check in `_emit`.
|
|
|
|
|
2016-02-03 07:12:02 +00:00
|
|
|
if (s.level !== undefined) {
|
2014-04-17 12:37:44 +00:00
|
|
|
s.level = resolveLevel(s.level);
|
|
|
|
} else {
|
2015-01-18 04:39:06 +00:00
|
|
|
s.level = resolveLevel(defaultLevel);
|
2014-04-17 12:37:44 +00:00
|
|
|
}
|
|
|
|
if (s.level < self._level) {
|
|
|
|
self._level = s.level;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (s.type) {
|
2014-08-25 07:50:53 +00:00
|
|
|
case 'stream':
|
2016-02-20 07:58:37 +00:00
|
|
|
assert.ok(isWritable(s.stream),
|
|
|
|
'"stream" stream is not writable: ' + util.inspect(s.stream));
|
|
|
|
|
2014-04-17 12:37:44 +00:00
|
|
|
if (!s.closeOnExit) {
|
|
|
|
s.closeOnExit = false;
|
|
|
|
}
|
|
|
|
break;
|
2014-08-25 07:50:53 +00:00
|
|
|
case 'file':
|
2016-02-21 02:04:47 +00:00
|
|
|
if (s.reemitErrorEvents === undefined) {
|
|
|
|
s.reemitErrorEvents = true;
|
|
|
|
}
|
2014-04-17 12:37:44 +00:00
|
|
|
if (!s.stream) {
|
|
|
|
s.stream = fs.createWriteStream(s.path,
|
|
|
|
{flags: 'a', encoding: 'utf8'});
|
|
|
|
if (!s.closeOnExit) {
|
|
|
|
s.closeOnExit = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!s.closeOnExit) {
|
|
|
|
s.closeOnExit = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2014-08-25 07:50:53 +00:00
|
|
|
case 'rotating-file':
|
2014-04-17 12:37:44 +00:00
|
|
|
assert.ok(!s.stream,
|
|
|
|
'"rotating-file" stream should not give a "stream"');
|
|
|
|
assert.ok(s.path);
|
|
|
|
assert.ok(mv, '"rotating-file" stream type is not supported: '
|
|
|
|
+ 'missing "mv" module');
|
|
|
|
s.stream = new RotatingFileStream(s);
|
|
|
|
if (!s.closeOnExit) {
|
|
|
|
s.closeOnExit = true;
|
|
|
|
}
|
|
|
|
break;
|
2014-08-25 07:50:53 +00:00
|
|
|
case 'raw':
|
2014-04-17 12:37:44 +00:00
|
|
|
if (!s.closeOnExit) {
|
|
|
|
s.closeOnExit = false;
|
|
|
|
}
|
|
|
|
break;
|
2014-08-25 07:50:53 +00:00
|
|
|
default:
|
2014-04-17 12:37:44 +00:00
|
|
|
throw new TypeError('unknown stream type "' + s.type + '"');
|
|
|
|
}
|
|
|
|
|
2016-02-21 02:04:47 +00:00
|
|
|
if (s.reemitErrorEvents && typeof (s.stream.on) === 'function') {
|
|
|
|
// TODO: When we have `<logger>.close()`, it should remove event
|
|
|
|
// listeners to not leak Logger instances.
|
2016-02-11 07:38:23 +00:00
|
|
|
s.stream.on('error', function onStreamError(err) {
|
2015-12-01 01:25:42 +00:00
|
|
|
self.emit('error', err, s);
|
|
|
|
});
|
|
|
|
}
|
2016-02-11 07:38:23 +00:00
|
|
|
|
2014-04-17 12:37:44 +00:00
|
|
|
self.streams.push(s);
|
2015-02-06 05:45:35 +00:00
|
|
|
delete self.haveNonRawStreams; // reset
|
2014-04-17 12:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add serializers
|
|
|
|
*
|
2014-08-25 07:50:53 +00:00
|
|
|
* @param serializers {Object} Optional. Object mapping log record field names
|
|
|
|
* to serializing functions. See README.md for details.
|
2014-04-17 12:37:44 +00:00
|
|
|
*/
|
2014-08-25 07:50:53 +00:00
|
|
|
Logger.prototype.addSerializers = function addSerializers(serializers) {
|
2014-04-17 12:37:44 +00:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (!self.serializers) {
|
|
|
|
self.serializers = {};
|
|
|
|
}
|
|
|
|
Object.keys(serializers).forEach(function (field) {
|
|
|
|
var serializer = serializers[field];
|
|
|
|
if (typeof (serializer) !== 'function') {
|
|
|
|
throw new TypeError(format(
|
|
|
|
'invalid serializer for "%s" field: must be a function',
|
|
|
|
field));
|
|
|
|
} else {
|
|
|
|
self.serializers[field] = serializer;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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) {
|
2015-02-20 22:52:37 +00:00
|
|
|
return new (this.constructor)(this, options || {}, simple);
|
2012-02-01 06:36:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-11 20:16:29 +00:00
|
|
|
/**
|
|
|
|
* A convenience method to reopen 'file' streams on a logger. This can be
|
|
|
|
* useful with external log rotation utilities that move and re-open log files
|
|
|
|
* (e.g. logrotate on Linux, logadm on SmartOS/Illumos). Those utilities
|
|
|
|
* typically have rotation options to copy-and-truncate the log file, but
|
|
|
|
* you may not want to use that. An alternative is to do this in your
|
|
|
|
* application:
|
|
|
|
*
|
|
|
|
* var log = bunyan.createLogger(...);
|
|
|
|
* ...
|
|
|
|
* process.on('SIGUSR2', function () {
|
|
|
|
* log.reopenFileStreams();
|
|
|
|
* });
|
|
|
|
* ...
|
|
|
|
*
|
|
|
|
* See <https://github.com/trentm/node-bunyan/issues/104>.
|
|
|
|
*/
|
|
|
|
Logger.prototype.reopenFileStreams = function () {
|
|
|
|
var self = this;
|
|
|
|
self.streams.forEach(function (s) {
|
|
|
|
if (s.type === 'file') {
|
|
|
|
if (s.stream) {
|
|
|
|
// Not sure if typically would want this, or more immediate
|
|
|
|
// `s.stream.destroy()`.
|
|
|
|
s.stream.end();
|
|
|
|
s.stream.destroySoon();
|
|
|
|
delete s.stream;
|
|
|
|
}
|
|
|
|
s.stream = fs.createWriteStream(s.path,
|
|
|
|
{flags: 'a', encoding: 'utf8'});
|
|
|
|
s.stream.on('error', function (err) {
|
|
|
|
self.emit('error', err, s);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
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 () {
|
2013-03-29 00:42:32 +00:00
|
|
|
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;
|
2012-04-28 00:31:46 +00:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
/* 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) {
|
2013-03-29 00:42:32 +00:00
|
|
|
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;
|
2012-02-06 23:04:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2013-03-29 00:42:32 +00:00
|
|
|
if (name === undefined) {
|
|
|
|
assert.equal(value, undefined);
|
|
|
|
return this.streams.map(
|
|
|
|
function (s) { return s.level });
|
2012-02-06 23:04:47 +00:00
|
|
|
}
|
2013-03-29 00:42:32 +00:00
|
|
|
var stream;
|
|
|
|
if (typeof (name) === 'number') {
|
|
|
|
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));
|
|
|
|
}
|
2012-02-06 23:04:47 +00:00
|
|
|
}
|
2013-03-29 00:42:32 +00:00
|
|
|
if (value === undefined) {
|
|
|
|
return stream.level;
|
|
|
|
} else {
|
|
|
|
var newLevel = resolveLevel(value);
|
|
|
|
stream.level = newLevel;
|
|
|
|
if (newLevel < this._level) {
|
|
|
|
this._level = newLevel;
|
|
|
|
}
|
2012-02-06 23:04:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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.
|
2013-03-02 00:58:56 +00:00
|
|
|
* @param excludeFields (Object) Optional mapping of keys to `true` for
|
|
|
|
* keys to NOT apply a serializer.
|
2012-02-01 06:36:06 +00:00
|
|
|
*/
|
2013-03-02 00:58:56 +00:00
|
|
|
Logger.prototype._applySerializers = function (fields, excludeFields) {
|
2013-03-29 00:42:32 +00:00
|
|
|
var self = this;
|
2012-02-01 06:36:06 +00:00
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
xxx('_applySerializers: excludeFields', excludeFields);
|
2012-02-06 23:04:47 +00:00
|
|
|
|
2013-03-29 00:42:32 +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 (fields[name] === undefined ||
|
|
|
|
(excludeFields && excludeFields[name]))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
xxx('_applySerializers; apply to "%s" key', name)
|
|
|
|
try {
|
|
|
|
fields[name] = self.serializers[name](fields[name]);
|
|
|
|
} catch (err) {
|
2014-12-08 18:43:33 +00:00
|
|
|
_warn(format('bunyan: ERROR: Exception thrown from the "%s" '
|
|
|
|
+ 'Bunyan serializer. This should never happen. This is a bug'
|
|
|
|
+ 'in that serializer function.\n%s',
|
2013-03-29 00:42:32 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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) {
|
2013-03-29 00:42:32 +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-06-20 23:04:23 +00:00
|
|
|
}
|
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
// Stringify the object. Attempt to warn/recover on error.
|
|
|
|
var str;
|
|
|
|
if (noemit || this.haveNonRawStreams) {
|
2015-01-19 07:16:35 +00:00
|
|
|
try {
|
|
|
|
str = JSON.stringify(rec, safeCycles()) + '\n';
|
2015-01-19 07:27:28 +00:00
|
|
|
} catch (e) {
|
2015-01-19 07:16:35 +00:00
|
|
|
if (safeJsonStringify) {
|
|
|
|
str = safeJsonStringify(rec) + '\n';
|
2015-01-19 07:27:28 +00:00
|
|
|
} else {
|
|
|
|
var dedupKey = e.stack.split(/\n/g, 2).join('\n');
|
|
|
|
_warn('bunyan: ERROR: Exception in '
|
|
|
|
+ '`JSON.stringify(rec)`. You can install the '
|
|
|
|
+ '"safe-json-stringify" module to have Bunyan fallback '
|
|
|
|
+ 'to safer stringification. Record:\n'
|
|
|
|
+ _indent(format('%s\n%s', util.inspect(rec), e.stack)),
|
|
|
|
dedupKey);
|
|
|
|
str = format('(Exception in JSON.stringify(rec): %j. '
|
|
|
|
+ 'See stderr for details.)\n', e.message);
|
2015-01-19 07:16:35 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-29 00:42:32 +00:00
|
|
|
}
|
2012-02-07 05:34:04 +00:00
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
if (noemit)
|
|
|
|
return str;
|
2012-10-31 07:28:24 +00:00
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
var level = rec.level;
|
|
|
|
for (i = 0; i < this.streams.length; i++) {
|
|
|
|
var s = this.streams[i];
|
|
|
|
if (s.level <= level) {
|
|
|
|
xxx('writing log rec "%s" to "%s" stream (%d <= %d): %j',
|
|
|
|
rec.msg, s.type, s.level, level, rec);
|
|
|
|
s.stream.write(s.raw ? rec : str);
|
|
|
|
}
|
|
|
|
};
|
2012-10-31 07:28:24 +00:00
|
|
|
|
2013-03-29 00:42:32 +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) {
|
2013-03-29 00:42:32 +00:00
|
|
|
return function () {
|
|
|
|
var log = this;
|
|
|
|
|
|
|
|
function mkRecord(args) {
|
|
|
|
var excludeFields;
|
|
|
|
if (args[0] instanceof Error) {
|
|
|
|
// `log.<level>(err, ...)`
|
2014-09-14 00:12:09 +00:00
|
|
|
fields = {
|
2014-09-28 04:04:14 +00:00
|
|
|
// Use this Logger's err serializer, if defined.
|
2014-09-28 04:22:17 +00:00
|
|
|
err: (log.serializers && log.serializers.err
|
2014-09-28 04:04:14 +00:00
|
|
|
? log.serializers.err(args[0])
|
|
|
|
: Logger.stdSerializers.err(args[0]))
|
2014-09-14 00:12:09 +00:00
|
|
|
};
|
2013-03-29 00:42:32 +00:00
|
|
|
excludeFields = {err: true};
|
|
|
|
if (args.length === 1) {
|
|
|
|
msgArgs = [fields.err.message];
|
|
|
|
} else {
|
|
|
|
msgArgs = Array.prototype.slice.call(args, 1);
|
2014-09-28 04:04:14 +00:00
|
|
|
}
|
2014-05-29 06:24:57 +00:00
|
|
|
} else if (typeof (args[0]) !== 'object' && args[0] !== null ||
|
|
|
|
Array.isArray(args[0])) {
|
2013-04-02 00:21:01 +00:00
|
|
|
// `log.<level>(msg, ...)`
|
2013-03-29 00:42:32 +00:00
|
|
|
fields = null;
|
|
|
|
msgArgs = Array.prototype.slice.call(args);
|
|
|
|
} else if (Buffer.isBuffer(args[0])) { // `log.<level>(buf, ...)`
|
2013-04-02 00:21:01 +00:00
|
|
|
// Almost certainly an error, show `inspect(buf)`. See bunyan
|
|
|
|
// issue #35.
|
2013-03-29 00:42:32 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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, excludeFields);
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
var fields = null;
|
|
|
|
var msgArgs = arguments;
|
|
|
|
var str = null;
|
|
|
|
var rec = null;
|
2014-12-08 18:43:33 +00:00
|
|
|
if (! this._emit) {
|
|
|
|
/*
|
|
|
|
* Show this invalid Bunyan usage warning *once*.
|
|
|
|
*
|
|
|
|
* See <https://github.com/trentm/node-bunyan/issues/100> for
|
|
|
|
* an example of how this can happen.
|
|
|
|
*/
|
|
|
|
var dedupKey = 'unbound';
|
|
|
|
if (!_haveWarned[dedupKey]) {
|
|
|
|
var caller = getCaller3Info();
|
|
|
|
_warn(format('bunyan usage error: %s:%s: attempt to log '
|
|
|
|
+ 'with an unbound log method: `this` is: %s',
|
|
|
|
caller.file, caller.line, util.inspect(this)),
|
|
|
|
dedupKey);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if (arguments.length === 0) { // `log.<level>()`
|
2013-03-29 00:42:32 +00:00
|
|
|
return (this._level <= minLevel);
|
|
|
|
} else if (this._level > minLevel) {
|
|
|
|
/* pass through */
|
2012-10-31 07:28:24 +00:00
|
|
|
} else {
|
2013-03-29 00:42:32 +00:00
|
|
|
rec = mkRecord(msgArgs);
|
|
|
|
str = this._emit(rec);
|
2012-10-31 07:28:24 +00:00
|
|
|
}
|
2013-03-29 00:42:32 +00:00
|
|
|
probes && probes[minLevel].fire(function () {
|
|
|
|
return [ str ||
|
|
|
|
(rec && log._emit(rec, true)) ||
|
|
|
|
log._emit(mkRecord(msgArgs), true) ];
|
2012-11-01 05:17:44 +00:00
|
|
|
});
|
2012-02-17 00:49:19 +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) {
|
2013-03-29 00:42:32 +00:00
|
|
|
if (!req || !req.connection)
|
|
|
|
return req;
|
|
|
|
return {
|
|
|
|
method: req.method,
|
|
|
|
url: req.url,
|
|
|
|
headers: req.headers,
|
|
|
|
remoteAddress: req.connection.remoteAddress,
|
|
|
|
remotePort: req.connection.remotePort
|
|
|
|
};
|
|
|
|
// Trailers: Skipping for speed. If you need trailers in your app, then
|
|
|
|
// make a custom serializer.
|
|
|
|
//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) {
|
2013-03-29 00:42:32 +00:00
|
|
|
if (!res || !res.statusCode)
|
|
|
|
return res;
|
|
|
|
return {
|
|
|
|
statusCode: res.statusCode,
|
|
|
|
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)
|
|
|
|
{
|
2013-03-29 00:42:32 +00:00
|
|
|
var ret = ex.stack || ex.toString();
|
|
|
|
if (ex.cause && typeof (ex.cause) === 'function') {
|
|
|
|
var cex = ex.cause();
|
|
|
|
if (cex) {
|
|
|
|
ret += '\nCaused by: ' + getFullErrorStack(cex);
|
|
|
|
}
|
2012-10-11 21:02:48 +00:00
|
|
|
}
|
2013-03-29 00:42:32 +00:00
|
|
|
return (ret);
|
2012-10-11 00:09:39 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2013-03-29 00:42:32 +00:00
|
|
|
if (!err || !err.stack)
|
|
|
|
return err;
|
|
|
|
var obj = {
|
|
|
|
message: err.message,
|
|
|
|
name: err.name,
|
|
|
|
stack: getFullErrorStack(err),
|
|
|
|
code: err.code,
|
|
|
|
signal: err.signal
|
|
|
|
}
|
|
|
|
return obj;
|
2012-02-04 08:08:37 +00:00
|
|
|
};
|
|
|
|
|
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() {
|
2013-03-29 00:42:32 +00:00
|
|
|
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-09-10 18:24:39 +00:00
|
|
|
}
|
2012-02-01 06:36:06 +00:00
|
|
|
|
2013-01-04 22:30:27 +00:00
|
|
|
|
2015-09-07 08:21:43 +00:00
|
|
|
|
|
|
|
var RotatingFileStream = null;
|
2013-01-04 22:30:27 +00:00
|
|
|
if (mv) {
|
|
|
|
|
2015-09-07 08:21:43 +00:00
|
|
|
RotatingFileStream = function RotatingFileStream(options) {
|
2013-03-29 00:42:32 +00:00
|
|
|
this.path = options.path;
|
2015-12-28 21:01:11 +00:00
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
this.count = (options.count == null ? 10 : options.count);
|
2015-01-17 06:31:44 +00:00
|
|
|
assert.equal(typeof (this.count), 'number',
|
2015-01-16 17:00:10 +00:00
|
|
|
format('rotating-file stream "count" is not a number: %j (%s) in %j',
|
2015-01-17 21:33:12 +00:00
|
|
|
this.count, typeof (this.count), this));
|
2015-01-17 06:31:44 +00:00
|
|
|
assert.ok(this.count >= 0,
|
2015-01-16 17:00:10 +00:00
|
|
|
format('rotating-file stream "count" is not >= 0: %j in %j',
|
2015-01-17 06:31:44 +00:00
|
|
|
this.count, this));
|
2013-03-29 00:42:32 +00:00
|
|
|
|
|
|
|
// Parse `options.period`.
|
|
|
|
if (options.period) {
|
|
|
|
// <number><scope> where scope is:
|
|
|
|
// h hours (at the start of the hour)
|
|
|
|
// d days (at the start of the day, i.e. just after midnight)
|
|
|
|
// w weeks (at the start of Sunday)
|
|
|
|
// m months (on the first of the month)
|
|
|
|
// y years (at the start of Jan 1st)
|
|
|
|
// with special values 'hourly' (1h), 'daily' (1d), "weekly" (1w),
|
|
|
|
// 'monthly' (1m) and 'yearly' (1y)
|
|
|
|
var period = {
|
|
|
|
'hourly': '1h',
|
|
|
|
'daily': '1d',
|
|
|
|
'weekly': '1w',
|
|
|
|
'monthly': '1m',
|
|
|
|
'yearly': '1y'
|
|
|
|
}[options.period] || options.period;
|
|
|
|
var m = /^([1-9][0-9]*)([hdwmy]|ms)$/.exec(period);
|
|
|
|
if (!m) {
|
|
|
|
throw new Error(format('invalid period: "%s"', options.period));
|
|
|
|
}
|
|
|
|
this.periodNum = Number(m[1]);
|
|
|
|
this.periodScope = m[2];
|
|
|
|
} else {
|
|
|
|
this.periodNum = 1;
|
|
|
|
this.periodScope = 'd';
|
2013-01-04 22:30:27 +00:00
|
|
|
}
|
2013-03-29 00:42:32 +00:00
|
|
|
|
2015-12-28 21:01:11 +00:00
|
|
|
var lastModified = null;
|
|
|
|
try {
|
|
|
|
var fileInfo = fs.statSync(this.path);
|
|
|
|
lastModified = fileInfo.mtime.getTime();
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
// file doesn't exist
|
|
|
|
}
|
|
|
|
var rotateAfterOpen = false;
|
|
|
|
if (lastModified) {
|
|
|
|
var lastRotTime = this._calcRotTime(0);
|
|
|
|
if (lastModified < lastRotTime) {
|
|
|
|
rotateAfterOpen = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
// TODO: template support for backup files
|
|
|
|
// template: <path to which to rotate>
|
|
|
|
// default is %P.%n
|
|
|
|
// '/var/log/archive/foo.log' -> foo.log.%n
|
|
|
|
// '/var/log/archive/foo.log.%n'
|
|
|
|
// codes:
|
|
|
|
// XXX support strftime codes (per node version of those)
|
|
|
|
// or whatever module. Pick non-colliding for extra
|
|
|
|
// codes
|
|
|
|
// %P `path` base value
|
|
|
|
// %n integer number of rotated log (1,2,3,...)
|
|
|
|
// %d datetime in YYYY-MM-DD_HH-MM-SS
|
|
|
|
// XXX what should default date format be?
|
|
|
|
// prior art? Want to avoid ':' in
|
|
|
|
// filenames (illegal on Windows for one).
|
|
|
|
|
2015-12-28 21:01:11 +00:00
|
|
|
this.stream = fs.createWriteStream(this.path,
|
|
|
|
{flags: 'a', encoding: 'utf8'});
|
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
this.rotQueue = [];
|
|
|
|
this.rotating = false;
|
2015-12-28 21:01:11 +00:00
|
|
|
if (rotateAfterOpen) {
|
2016-04-21 07:31:46 +00:00
|
|
|
this._debug('rotateAfterOpen -> call rotate()');
|
2015-12-28 21:01:11 +00:00
|
|
|
this.rotate();
|
2016-03-31 22:31:39 +00:00
|
|
|
} else {
|
|
|
|
this._setupNextRot();
|
2015-12-28 21:01:11 +00:00
|
|
|
}
|
2013-01-04 22:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(RotatingFileStream, EventEmitter);
|
|
|
|
|
2016-04-21 07:31:46 +00:00
|
|
|
RotatingFileStream.prototype._debug = function () {
|
|
|
|
// Set this to `true` to add debug logging.
|
|
|
|
if (false) {
|
|
|
|
if (arguments.length === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
args[0] = '[' + (new Date().toISOString()) + ', '
|
|
|
|
+ this.path + '] ' + args[0];
|
|
|
|
console.log.apply(this, args);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-04 22:30:27 +00:00
|
|
|
RotatingFileStream.prototype._setupNextRot = function () {
|
2015-12-28 21:01:11 +00:00
|
|
|
this.rotAt = this._calcRotTime(1);
|
2016-02-01 11:41:59 +00:00
|
|
|
this._setRotationTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
RotatingFileStream.prototype._setRotationTimer = function () {
|
|
|
|
var self = this;
|
2014-11-01 10:20:20 +00:00
|
|
|
var delay = this.rotAt - Date.now();
|
2014-11-18 05:45:41 +00:00
|
|
|
// Cap timeout to Node's max setTimeout, see
|
|
|
|
// <https://github.com/joyent/node/issues/8656>.
|
|
|
|
var TIMEOUT_MAX = 2147483647; // 2^31-1
|
|
|
|
if (delay > TIMEOUT_MAX) {
|
|
|
|
delay = TIMEOUT_MAX;
|
2014-11-01 10:20:20 +00:00
|
|
|
}
|
2013-03-29 00:42:32 +00:00
|
|
|
this.timeout = setTimeout(
|
2016-04-21 07:31:46 +00:00
|
|
|
function () {
|
|
|
|
self._debug('_setRotationTimer timeout -> call rotate()');
|
|
|
|
self.rotate();
|
|
|
|
},
|
2014-11-01 10:20:20 +00:00
|
|
|
delay);
|
2014-06-01 05:25:06 +00:00
|
|
|
if (typeof (this.timeout.unref) === 'function') {
|
|
|
|
this.timeout.unref();
|
2013-08-21 07:47:28 +00:00
|
|
|
}
|
2013-01-04 22:30:27 +00:00
|
|
|
}
|
|
|
|
|
2015-12-28 21:01:11 +00:00
|
|
|
RotatingFileStream.prototype._calcRotTime =
|
|
|
|
function _calcRotTime(periodOffset) {
|
2016-04-21 07:31:46 +00:00
|
|
|
this._debug('_calcRotTime: %s%s', this.periodNum, this.periodScope);
|
2013-03-29 00:42:32 +00:00
|
|
|
var d = new Date();
|
|
|
|
|
2016-04-21 07:31:46 +00:00
|
|
|
this._debug(' now local: %s', d);
|
|
|
|
this._debug(' now utc: %s', d.toISOString());
|
2013-03-29 00:42:32 +00:00
|
|
|
var rotAt;
|
|
|
|
switch (this.periodScope) {
|
|
|
|
case 'ms':
|
|
|
|
// Hidden millisecond period for debugging.
|
|
|
|
if (this.rotAt) {
|
2015-12-28 21:01:11 +00:00
|
|
|
rotAt = this.rotAt + this.periodNum * periodOffset;
|
2013-03-29 00:42:32 +00:00
|
|
|
} else {
|
2015-12-28 21:01:11 +00:00
|
|
|
rotAt = Date.now() + this.periodNum * periodOffset;
|
2013-03-29 00:42:32 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
if (this.rotAt) {
|
2015-12-28 21:01:11 +00:00
|
|
|
rotAt = this.rotAt + this.periodNum * 60 * 60 * 1000 * periodOffset;
|
2013-03-29 00:42:32 +00:00
|
|
|
} else {
|
|
|
|
// First time: top of the next hour.
|
2013-04-02 00:21:01 +00:00
|
|
|
rotAt = Date.UTC(d.getUTCFullYear(), d.getUTCMonth(),
|
2015-12-28 21:01:11 +00:00
|
|
|
d.getUTCDate(), d.getUTCHours() + periodOffset);
|
2013-03-29 00:42:32 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
if (this.rotAt) {
|
2015-12-28 21:01:11 +00:00
|
|
|
rotAt = this.rotAt + this.periodNum * 24 * 60 * 60 * 1000
|
|
|
|
* periodOffset;
|
2013-03-29 00:42:32 +00:00
|
|
|
} else {
|
|
|
|
// First time: start of tomorrow (i.e. at the coming midnight) UTC.
|
2013-04-02 00:21:01 +00:00
|
|
|
rotAt = Date.UTC(d.getUTCFullYear(), d.getUTCMonth(),
|
2015-12-28 21:01:11 +00:00
|
|
|
d.getUTCDate() + periodOffset);
|
2013-03-29 00:42:32 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
// Currently, always on Sunday morning at 00:00:00 (UTC).
|
|
|
|
if (this.rotAt) {
|
2015-12-28 21:01:11 +00:00
|
|
|
rotAt = this.rotAt + this.periodNum * 7 * 24 * 60 * 60 * 1000
|
|
|
|
* periodOffset;
|
2013-03-29 00:42:32 +00:00
|
|
|
} else {
|
|
|
|
// First time: this coming Sunday.
|
2015-12-28 21:01:11 +00:00
|
|
|
var dayOffset = (7 - d.getUTCDay());
|
|
|
|
if (periodOffset < 1) {
|
|
|
|
dayOffset = -d.getUTCDay();
|
|
|
|
}
|
|
|
|
if (periodOffset > 1 || periodOffset < -1) {
|
|
|
|
dayOffset += 7 * periodOffset;
|
|
|
|
}
|
2013-03-29 00:42:32 +00:00
|
|
|
rotAt = Date.UTC(d.getUTCFullYear(), d.getUTCMonth(),
|
2015-12-28 21:01:11 +00:00
|
|
|
d.getUTCDate() + dayOffset);
|
2013-03-29 00:42:32 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
if (this.rotAt) {
|
2013-04-02 00:21:01 +00:00
|
|
|
rotAt = Date.UTC(d.getUTCFullYear(),
|
2015-12-28 21:01:11 +00:00
|
|
|
d.getUTCMonth() + this.periodNum * periodOffset, 1);
|
2013-03-29 00:42:32 +00:00
|
|
|
} else {
|
|
|
|
// First time: the start of the next month.
|
2015-12-28 21:01:11 +00:00
|
|
|
rotAt = Date.UTC(d.getUTCFullYear(),
|
|
|
|
d.getUTCMonth() + periodOffset, 1);
|
2013-03-29 00:42:32 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'y':
|
|
|
|
if (this.rotAt) {
|
2015-12-28 21:01:11 +00:00
|
|
|
rotAt = Date.UTC(d.getUTCFullYear() + this.periodNum * periodOffset,
|
|
|
|
0, 1);
|
2013-03-29 00:42:32 +00:00
|
|
|
} else {
|
|
|
|
// First time: the start of the next year.
|
2015-12-28 21:01:11 +00:00
|
|
|
rotAt = Date.UTC(d.getUTCFullYear() + periodOffset, 0, 1);
|
2013-03-29 00:42:32 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert.fail(format('invalid period scope: "%s"', this.periodScope));
|
2013-01-04 22:30:27 +00:00
|
|
|
}
|
2013-03-29 00:42:32 +00:00
|
|
|
|
2016-04-21 07:31:46 +00:00
|
|
|
if (this._debug()) {
|
|
|
|
this._debug(' **rotAt**: %s (utc: %s)', rotAt,
|
2013-03-29 00:42:32 +00:00
|
|
|
new Date(rotAt).toUTCString());
|
|
|
|
var now = Date.now();
|
2016-04-21 07:31:46 +00:00
|
|
|
this._debug(' now: %s (%sms == %smin == %sh to go)',
|
2013-03-29 00:42:32 +00:00
|
|
|
now,
|
|
|
|
rotAt - now,
|
|
|
|
(rotAt-now)/1000/60,
|
|
|
|
(rotAt-now)/1000/60/60);
|
2013-01-04 22:30:27 +00:00
|
|
|
}
|
2013-03-29 00:42:32 +00:00
|
|
|
return rotAt;
|
2013-01-04 22:30:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
RotatingFileStream.prototype.rotate = function rotate() {
|
2013-03-29 00:42:32 +00:00
|
|
|
// XXX What about shutdown?
|
|
|
|
var self = this;
|
2013-01-04 22:30:27 +00:00
|
|
|
|
2014-11-18 05:45:41 +00:00
|
|
|
// If rotation period is > ~25 days, we have to break into multiple
|
|
|
|
// setTimeout's. See <https://github.com/joyent/node/issues/8656>.
|
2014-11-01 10:20:20 +00:00
|
|
|
if (self.rotAt && self.rotAt > Date.now()) {
|
2016-02-01 11:41:59 +00:00
|
|
|
return self._setRotationTimer();
|
2014-11-01 10:20:20 +00:00
|
|
|
}
|
|
|
|
|
2016-04-21 07:31:46 +00:00
|
|
|
this._debug('rotate');
|
2013-03-29 00:42:32 +00:00
|
|
|
if (self.rotating) {
|
|
|
|
throw new TypeError('cannot start a rotation when already rotating');
|
2013-01-04 22:30:27 +00:00
|
|
|
}
|
2013-03-29 00:42:32 +00:00
|
|
|
self.rotating = true;
|
|
|
|
|
|
|
|
self.stream.end(); // XXX can do moves sync after this? test at high rate
|
|
|
|
|
|
|
|
function del() {
|
|
|
|
var toDel = self.path + '.' + String(n - 1);
|
|
|
|
if (n === 0) {
|
|
|
|
toDel = self.path;
|
|
|
|
}
|
|
|
|
n -= 1;
|
2016-04-21 07:31:46 +00:00
|
|
|
self._debug(' rm %s', toDel);
|
2013-03-29 00:42:32 +00:00
|
|
|
fs.unlink(toDel, function (delErr) {
|
|
|
|
//XXX handle err other than not exists
|
2013-01-04 22:30:27 +00:00
|
|
|
moves();
|
|
|
|
});
|
|
|
|
}
|
2013-03-29 00:42:32 +00:00
|
|
|
|
|
|
|
function moves() {
|
|
|
|
if (self.count === 0 || n < 0) {
|
|
|
|
return finish();
|
|
|
|
}
|
|
|
|
var before = self.path;
|
|
|
|
var after = self.path + '.' + String(n);
|
|
|
|
if (n > 0) {
|
|
|
|
before += '.' + String(n - 1);
|
|
|
|
}
|
|
|
|
n -= 1;
|
|
|
|
fs.exists(before, function (exists) {
|
|
|
|
if (!exists) {
|
|
|
|
moves();
|
|
|
|
} else {
|
2016-04-21 07:31:46 +00:00
|
|
|
self._debug(' mv %s %s', before, after);
|
2013-03-29 00:42:32 +00:00
|
|
|
mv(before, after, function (mvErr) {
|
|
|
|
if (mvErr) {
|
|
|
|
self.emit('error', mvErr);
|
|
|
|
finish(); // XXX finish here?
|
|
|
|
} else {
|
|
|
|
moves();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function finish() {
|
2016-04-21 07:31:46 +00:00
|
|
|
self._debug(' open %s', self.path);
|
2013-03-29 00:42:32 +00:00
|
|
|
self.stream = fs.createWriteStream(self.path,
|
|
|
|
{flags: 'a', encoding: 'utf8'});
|
|
|
|
var q = self.rotQueue, len = q.length;
|
|
|
|
for (var i = 0; i < len; i++) {
|
|
|
|
self.stream.write(q[i]);
|
|
|
|
}
|
|
|
|
self.rotQueue = [];
|
|
|
|
self.rotating = false;
|
|
|
|
self.emit('drain');
|
|
|
|
self._setupNextRot();
|
|
|
|
}
|
|
|
|
|
|
|
|
var n = this.count;
|
|
|
|
del();
|
2013-01-04 22:30:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
RotatingFileStream.prototype.write = function write(s) {
|
2013-03-29 00:42:32 +00:00
|
|
|
if (this.rotating) {
|
|
|
|
this.rotQueue.push(s);
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return this.stream.write(s);
|
|
|
|
}
|
2013-01-04 22:30:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
RotatingFileStream.prototype.end = function end(s) {
|
2013-03-29 00:42:32 +00:00
|
|
|
this.stream.end();
|
2013-01-04 22:30:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
RotatingFileStream.prototype.destroy = function destroy(s) {
|
2013-03-29 00:42:32 +00:00
|
|
|
this.stream.destroy();
|
2013-01-04 22:30:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
RotatingFileStream.prototype.destroySoon = function destroySoon(s) {
|
2013-03-29 00:42:32 +00:00
|
|
|
this.stream.destroySoon();
|
2013-01-04 22:30:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} /* if (mv) */
|
|
|
|
|
|
|
|
|
|
|
|
|
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) {
|
2013-03-29 00:42:32 +00:00
|
|
|
this.limit = options && options.limit ? options.limit : 100;
|
|
|
|
this.writable = true;
|
|
|
|
this.records = [];
|
|
|
|
EventEmitter.call(this);
|
2012-06-19 21:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(RingBuffer, EventEmitter);
|
|
|
|
|
2012-06-20 23:26:28 +00:00
|
|
|
RingBuffer.prototype.write = function (record) {
|
2013-03-29 00:42:32 +00:00
|
|
|
if (!this.writable)
|
|
|
|
throw (new Error('RingBuffer has been ended already'));
|
2012-06-19 21:36:02 +00:00
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
this.records.push(record);
|
2012-06-19 21:36:02 +00:00
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
if (this.records.length > this.limit)
|
|
|
|
this.records.shift();
|
2012-06-19 21:36:02 +00:00
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
return (true);
|
2012-06-19 21:36:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
RingBuffer.prototype.end = function () {
|
2013-03-29 00:42:32 +00:00
|
|
|
if (arguments.length > 0)
|
|
|
|
this.write.apply(this, Array.prototype.slice.call(arguments));
|
|
|
|
this.writable = false;
|
2012-06-19 21:36:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
RingBuffer.prototype.destroy = function () {
|
2013-03-29 00:42:32 +00:00
|
|
|
this.writable = false;
|
|
|
|
this.emit('close');
|
2012-06-19 21:36:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
RingBuffer.prototype.destroySoon = function () {
|
2013-03-29 00:42:32 +00:00
|
|
|
this.destroy();
|
2012-06-19 21:36:02 +00:00
|
|
|
};
|
|
|
|
|
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;
|
2015-01-16 06:18:29 +00:00
|
|
|
module.exports.levelFromName = levelFromName;
|
|
|
|
module.exports.nameFromLevel = nameFromLevel;
|
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) {
|
2013-03-29 00:42:32 +00:00
|
|
|
return new Logger(options);
|
2012-04-27 23:20:57 +00:00
|
|
|
};
|
2012-06-19 21:36:02 +00:00
|
|
|
|
|
|
|
module.exports.RingBuffer = RingBuffer;
|
2015-01-18 05:56:46 +00:00
|
|
|
module.exports.RotatingFileStream = RotatingFileStream;
|
2012-10-24 17:54:18 +00:00
|
|
|
|
2013-03-29 00:25:01 +00:00
|
|
|
// Useful for custom `type == 'raw'` streams that may do JSON stringification
|
2012-10-24 17:54:18 +00:00
|
|
|
// of log records themselves. Usage:
|
|
|
|
// var str = JSON.stringify(rec, bunyan.safeCycles());
|
|
|
|
module.exports.safeCycles = safeCycles;
|