Added node modules
This commit is contained in:
143
node_modules/winston/lib/winston.js
generated
vendored
Normal file
143
node_modules/winston/lib/winston.js
generated
vendored
Normal file
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
* winston.js: Top-level include defining Winston.
|
||||
*
|
||||
* (C) 2010 Charlie Robbins
|
||||
* MIT LICENCE
|
||||
*
|
||||
*/
|
||||
|
||||
var winston = exports;
|
||||
|
||||
//
|
||||
// Expose version using `pkginfo`
|
||||
//
|
||||
require('pkginfo')(module, 'version');
|
||||
|
||||
//
|
||||
// Include transports defined by default by winston
|
||||
//
|
||||
winston.transports = require('./winston/transports');
|
||||
|
||||
//
|
||||
// Expose utility methods
|
||||
//
|
||||
var common = require('./winston/common');
|
||||
winston.hash = common.hash;
|
||||
winston.clone = common.clone;
|
||||
winston.longestElement = common.longestElement;
|
||||
winston.exception = require('./winston/exception');
|
||||
winston.config = require('./winston/config');
|
||||
winston.addColors = winston.config.addColors;
|
||||
|
||||
//
|
||||
// Expose core Logging-related prototypes.
|
||||
//
|
||||
winston.Container = require('./winston/container').Container;
|
||||
winston.Logger = require('./winston/logger').Logger;
|
||||
winston.Transport = require('./winston/transports/transport').Transport;
|
||||
|
||||
//
|
||||
// We create and expose a default `Container` to `winston.loggers` so that the
|
||||
// programmer may manage multiple `winston.Logger` instances without any additional overhead.
|
||||
//
|
||||
// ### some-file1.js
|
||||
//
|
||||
// var logger = require('winston').loggers.get('something');
|
||||
//
|
||||
// ### some-file2.js
|
||||
//
|
||||
// var logger = require('winston').loggers.get('something');
|
||||
//
|
||||
winston.loggers = new winston.Container();
|
||||
|
||||
//
|
||||
// We create and expose a 'defaultLogger' so that the programmer may do the
|
||||
// following without the need to create an instance of winston.Logger directly:
|
||||
//
|
||||
// var winston = require('winston');
|
||||
// winston.log('info', 'some message');
|
||||
// winston.error('some error');
|
||||
//
|
||||
var defaultLogger = new winston.Logger({
|
||||
transports: [new winston.transports.Console()]
|
||||
});
|
||||
|
||||
//
|
||||
// Pass through the target methods onto `winston.
|
||||
//
|
||||
var methods = [
|
||||
'log',
|
||||
'add',
|
||||
'remove',
|
||||
'profile',
|
||||
'startTimer',
|
||||
'extend',
|
||||
'cli',
|
||||
'handleExceptions',
|
||||
'unhandleExceptions'
|
||||
];
|
||||
common.setLevels(winston, null, defaultLogger.levels);
|
||||
methods.forEach(function (method) {
|
||||
winston[method] = function () {
|
||||
return defaultLogger[method].apply(defaultLogger, arguments);
|
||||
};
|
||||
});
|
||||
|
||||
//
|
||||
// ### function cli ()
|
||||
// Configures the default winston logger to have the
|
||||
// settings for command-line interfaces: no timestamp,
|
||||
// colors enabled, padded output, and additional levels.
|
||||
//
|
||||
winston.cli = function () {
|
||||
winston.padLevels = true;
|
||||
common.setLevels(winston, defaultLogger.levels, winston.config.cli.levels);
|
||||
defaultLogger.setLevels(winston.config.cli.levels);
|
||||
winston.config.addColors(winston.config.cli.colors);
|
||||
|
||||
if (defaultLogger.transports.console) {
|
||||
defaultLogger.transports.console.colorize = true;
|
||||
defaultLogger.transports.console.timestamp = false;
|
||||
}
|
||||
|
||||
return winston;
|
||||
};
|
||||
|
||||
//
|
||||
// ### function setLevels (target)
|
||||
// #### @target {Object} Target levels to use
|
||||
// Sets the `target` levels specified on the default winston logger.
|
||||
//
|
||||
winston.setLevels = function (target) {
|
||||
common.setLevels(winston, defaultLogger.levels, target);
|
||||
defaultLogger.setLevels(target);
|
||||
};
|
||||
|
||||
//
|
||||
// Define getters / setters for appropriate properties of the
|
||||
// default logger which need to be exposed by winston.
|
||||
//
|
||||
['emitErrs', 'exitOnError', 'padLevels', 'level', 'levelLength', 'stripColors'].forEach(function (prop) {
|
||||
Object.defineProperty(winston, prop, {
|
||||
get: function () {
|
||||
return defaultLogger[prop];
|
||||
},
|
||||
set: function (val) {
|
||||
defaultLogger[prop] = val;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
//
|
||||
// @default {Object}
|
||||
// The default transports and exceptionHandlers for
|
||||
// the default winston logger.
|
||||
//
|
||||
Object.defineProperty(winston, 'default', {
|
||||
get: function () {
|
||||
return {
|
||||
transports: defaultLogger.transports,
|
||||
exceptionHandlers: defaultLogger.exceptionHandlers
|
||||
};
|
||||
}
|
||||
});
|
||||
233
node_modules/winston/lib/winston/common.js
generated
vendored
Normal file
233
node_modules/winston/lib/winston/common.js
generated
vendored
Normal file
@@ -0,0 +1,233 @@
|
||||
/*
|
||||
* common.js: Internal helper and utility functions for winston
|
||||
*
|
||||
* (C) 2010 Charlie Robbins
|
||||
* MIT LICENCE
|
||||
*
|
||||
*/
|
||||
|
||||
var util = require('util'),
|
||||
crypto = require('crypto'),
|
||||
config = require('./config');
|
||||
|
||||
//
|
||||
// ### function setLevels (target, past, current)
|
||||
// #### @target {Object} Object on which to set levels.
|
||||
// #### @past {Object} Previous levels set on target.
|
||||
// #### @current {Object} Current levels to set on target.
|
||||
// Create functions on the target objects for each level
|
||||
// in current.levels. If past is defined, remove functions
|
||||
// for each of those levels.
|
||||
//
|
||||
exports.setLevels = function (target, past, current, isDefault) {
|
||||
if (past) {
|
||||
Object.keys(past).forEach(function (level) {
|
||||
delete target[level];
|
||||
});
|
||||
}
|
||||
|
||||
target.levels = current || config.npm.levels;
|
||||
if (target.padLevels) {
|
||||
target.levelLength = exports.longestElement(Object.keys(target.levels));
|
||||
}
|
||||
|
||||
//
|
||||
// Define prototype methods for each log level
|
||||
// e.g. target.log('info', msg) <=> target.info(msg)
|
||||
//
|
||||
Object.keys(target.levels).forEach(function (level) {
|
||||
target[level] = function (msg) {
|
||||
var args = Array.prototype.slice.call(arguments),
|
||||
callback = typeof args[args.length - 1] === 'function' || !args[args.length - 1] ? args.pop() : null,
|
||||
meta = args.length === 2 ? args.pop() : null;
|
||||
|
||||
return target.log(level, msg, meta, callback);
|
||||
};
|
||||
});
|
||||
|
||||
return target;
|
||||
};
|
||||
|
||||
//
|
||||
// ### function longestElement
|
||||
// #### @xs {Array} Array to calculate against
|
||||
// Returns the longest element in the `xs` array.
|
||||
//
|
||||
exports.longestElement = function (xs) {
|
||||
return Math.max.apply(
|
||||
null,
|
||||
xs.map(function (x) { return x.length })
|
||||
);
|
||||
};
|
||||
|
||||
//
|
||||
// ### function clone (obj)
|
||||
// #### @obj {Object} Object to clone.
|
||||
// Helper method for deep cloning pure JSON objects
|
||||
// i.e. JSON objects that are either literals or objects (no Arrays, etc)
|
||||
//
|
||||
exports.clone = function (obj) {
|
||||
// we only need to clone refrence types (Object)
|
||||
if (!(obj instanceof Object)) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
var copy = {};
|
||||
for (var i in obj) {
|
||||
if (Array.isArray(obj[i])) {
|
||||
copy[i] = obj[i].slice(0);
|
||||
}
|
||||
else {
|
||||
copy[i] = obj[i] instanceof Object ? exports.clone(obj[i]) : obj[i];
|
||||
}
|
||||
}
|
||||
|
||||
return copy;
|
||||
};
|
||||
|
||||
//
|
||||
// ### function log (options)
|
||||
// #### @options {Object} All information about the log serialization.
|
||||
// Generic logging function for returning timestamped strings
|
||||
// with the following options:
|
||||
//
|
||||
// {
|
||||
// level: 'level to add to serialized message',
|
||||
// message: 'message to serialize',
|
||||
// meta: 'additional logging metadata to serialize',
|
||||
// colorize: false, // Colorizes output (only if `.json` is false)
|
||||
// timestamp: true // Adds a timestamp to the serialized message
|
||||
// }
|
||||
//
|
||||
exports.log = function (options) {
|
||||
var timestampFn = typeof options.timestamp === 'function' ? options.timestamp : exports.timestamp,
|
||||
timestamp = options.timestamp ? timestampFn() : null,
|
||||
meta = options.meta ? exports.clone(options.meta) : null,
|
||||
output;
|
||||
|
||||
if (options.json) {
|
||||
output = meta || {};
|
||||
output.level = options.level;
|
||||
output.message = options.message;
|
||||
|
||||
if (timestamp) {
|
||||
output.timestamp = timestamp;
|
||||
}
|
||||
|
||||
return typeof options.stringify === 'function'
|
||||
? options.stringify(output)
|
||||
: JSON.stringify(output);
|
||||
}
|
||||
|
||||
output = timestamp ? timestamp + ' - ' : '';
|
||||
output += options.colorize ? config.colorize(options.level) : options.level;
|
||||
output += (': ' + options.message);
|
||||
|
||||
if (meta) {
|
||||
if (typeof meta !== 'object') {
|
||||
output += ' ' + meta;
|
||||
}
|
||||
else if (Object.keys(meta).length > 0) {
|
||||
output += ' ' + exports.serialize(meta);
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
};
|
||||
|
||||
exports.capitalize = function (str) {
|
||||
return str && str[0].toUpperCase() + str.slice(1);
|
||||
};
|
||||
|
||||
//
|
||||
// ### function hash (str)
|
||||
// #### @str {string} String to hash.
|
||||
// Utility function for creating unique ids
|
||||
// e.g. Profiling incoming HTTP requests on the same tick
|
||||
//
|
||||
exports.hash = function (str) {
|
||||
return crypto.createHash('sha1').update(str).digest('hex');
|
||||
};
|
||||
|
||||
//
|
||||
// ## Borrowed from node.js core
|
||||
// I wanted a universal lowercase header message, as opposed to the `DEBUG`
|
||||
// (i.e. all uppercase header) used only in `util.debug()`
|
||||
//
|
||||
var months = ['Jan', 'Feb', 'Mar', 'Apr',
|
||||
'May', 'Jun', 'Jul', 'Aug',
|
||||
'Sep', 'Oct', 'Nov', 'Dec'];
|
||||
|
||||
//
|
||||
// ### function pad (n)
|
||||
// Returns a padded string if `n < 10`.
|
||||
//
|
||||
exports.pad = function (n) {
|
||||
return n < 10 ? '0' + n.toString(10) : n.toString(10);
|
||||
};
|
||||
|
||||
//
|
||||
// ### function timestamp ()
|
||||
// Returns a timestamp string for the current time.
|
||||
//
|
||||
exports.timestamp = function () {
|
||||
var d = new Date();
|
||||
var time = [
|
||||
exports.pad(d.getHours()),
|
||||
exports.pad(d.getMinutes()),
|
||||
exports.pad(d.getSeconds())
|
||||
].join(':');
|
||||
|
||||
return [d.getDate(), months[d.getMonth()], time].join(' ');
|
||||
};
|
||||
|
||||
//
|
||||
// ### function serialize (obj, key)
|
||||
// #### @obj {Object|literal} Object to serialize
|
||||
// #### @key {string} **Optional** Optional key represented by obj in a larger object
|
||||
// Performs simple comma-separated, `key=value` serialization for Loggly when
|
||||
// logging to non-JSON inputs.
|
||||
//
|
||||
exports.serialize = function (obj, key) {
|
||||
if (obj === null) {
|
||||
obj = 'null';
|
||||
}
|
||||
else if (obj === undefined) {
|
||||
obj = 'undefined';
|
||||
}
|
||||
else if (obj === false) {
|
||||
obj = 'false';
|
||||
}
|
||||
|
||||
if (typeof obj !== 'object') {
|
||||
return key ? key + '=' + obj : obj;
|
||||
}
|
||||
|
||||
var msg = '',
|
||||
keys = Object.keys(obj),
|
||||
length = keys.length;
|
||||
|
||||
for (var i = 0; i < length; i++) {
|
||||
if (Array.isArray(obj[keys[i]])) {
|
||||
msg += keys[i] + '=['
|
||||
|
||||
for (var j = 0, l = obj[keys[i]].length; j < l; j++) {
|
||||
msg += exports.serialize(obj[keys[i]][j]);
|
||||
if (j < l - 1) {
|
||||
msg += ', ';
|
||||
}
|
||||
}
|
||||
|
||||
msg += ']';
|
||||
}
|
||||
else {
|
||||
msg += exports.serialize(obj[keys[i]], keys[i]);
|
||||
}
|
||||
|
||||
if (i < length - 1) {
|
||||
msg += ', ';
|
||||
}
|
||||
}
|
||||
|
||||
return msg;
|
||||
};
|
||||
45
node_modules/winston/lib/winston/config.js
generated
vendored
Normal file
45
node_modules/winston/lib/winston/config.js
generated
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* config.js: Default settings for all levels that winston knows about
|
||||
*
|
||||
* (C) 2010 Charlie Robbins
|
||||
* MIT LICENCE
|
||||
*
|
||||
*/
|
||||
|
||||
var colors = require('colors');
|
||||
|
||||
var config = exports,
|
||||
allColors = exports.allColors = {};
|
||||
|
||||
config.addColors = function (colors) {
|
||||
mixin(allColors, colors);
|
||||
};
|
||||
|
||||
config.colorize = function (level) {
|
||||
return level[allColors[level]];
|
||||
};
|
||||
|
||||
//
|
||||
// Export config sets
|
||||
//
|
||||
config.cli = require('./config/cli-config');
|
||||
config.npm = require('./config/npm-config');
|
||||
config.syslog = require('./config/syslog-config');
|
||||
|
||||
//
|
||||
// Add colors for pre-defined config sets
|
||||
//
|
||||
config.addColors(config.npm.colors);
|
||||
config.addColors(config.syslog.colors);
|
||||
|
||||
function mixin (target) {
|
||||
var args = Array.prototype.slice.call(arguments, 1);
|
||||
|
||||
args.forEach(function (a) {
|
||||
var keys = Object.keys(a);
|
||||
for (var i = 0; i < keys.length; i++) {
|
||||
target[keys[i]] = a[keys[i]];
|
||||
}
|
||||
});
|
||||
return target;
|
||||
};
|
||||
35
node_modules/winston/lib/winston/config/cli-config.js
generated
vendored
Normal file
35
node_modules/winston/lib/winston/config/cli-config.js
generated
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* cli-config.js: Config that conform to commonly used CLI logging levels.
|
||||
*
|
||||
* (C) 2010 Charlie Robbins
|
||||
* MIT LICENCE
|
||||
*
|
||||
*/
|
||||
|
||||
var cliConfig = exports;
|
||||
|
||||
cliConfig.levels = {
|
||||
silly: 0,
|
||||
input: 1,
|
||||
verbose: 2,
|
||||
prompt: 3,
|
||||
info: 4,
|
||||
data: 5,
|
||||
help: 6,
|
||||
warn: 7,
|
||||
debug: 8,
|
||||
error: 9
|
||||
};
|
||||
|
||||
cliConfig.colors = {
|
||||
silly: 'magenta',
|
||||
input: 'grey',
|
||||
verbose: 'cyan',
|
||||
prompt: 'grey',
|
||||
info: 'green',
|
||||
data: 'grey',
|
||||
help: 'cyan',
|
||||
warn: 'yellow',
|
||||
debug: 'blue',
|
||||
error: 'red'
|
||||
};
|
||||
27
node_modules/winston/lib/winston/config/npm-config.js
generated
vendored
Normal file
27
node_modules/winston/lib/winston/config/npm-config.js
generated
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* npm-config.js: Config that conform to npm logging levels.
|
||||
*
|
||||
* (C) 2010 Charlie Robbins
|
||||
* MIT LICENCE
|
||||
*
|
||||
*/
|
||||
|
||||
var npmConfig = exports;
|
||||
|
||||
npmConfig.levels = {
|
||||
silly: 0,
|
||||
verbose: 1,
|
||||
info: 2,
|
||||
warn: 3,
|
||||
debug: 4,
|
||||
error: 5
|
||||
};
|
||||
|
||||
npmConfig.colors = {
|
||||
silly: 'magenta',
|
||||
verbose: 'cyan',
|
||||
info: 'green',
|
||||
warn: 'yellow',
|
||||
debug: 'blue',
|
||||
error: 'red'
|
||||
};
|
||||
31
node_modules/winston/lib/winston/config/syslog-config.js
generated
vendored
Normal file
31
node_modules/winston/lib/winston/config/syslog-config.js
generated
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* syslog-config.js: Config that conform to syslog logging levels.
|
||||
*
|
||||
* (C) 2010 Charlie Robbins
|
||||
* MIT LICENCE
|
||||
*
|
||||
*/
|
||||
|
||||
var syslogConfig = exports;
|
||||
|
||||
syslogConfig.levels = {
|
||||
debug: 0,
|
||||
info: 1,
|
||||
notice: 2,
|
||||
warning: 3,
|
||||
error: 4,
|
||||
crit: 5,
|
||||
alert: 6,
|
||||
emerg: 7
|
||||
};
|
||||
|
||||
syslogConfig.colors = {
|
||||
debug: 'blue',
|
||||
info: 'green',
|
||||
notice: 'yellow',
|
||||
warning: 'red',
|
||||
error: 'red',
|
||||
crit: 'red',
|
||||
alert: 'yellow',
|
||||
emerg: 'red'
|
||||
};
|
||||
100
node_modules/winston/lib/winston/container.js
generated
vendored
Normal file
100
node_modules/winston/lib/winston/container.js
generated
vendored
Normal file
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* container.js: Inversion of control container for winston logger instances
|
||||
*
|
||||
* (C) 2010 Charlie Robbins
|
||||
* MIT LICENCE
|
||||
*
|
||||
*/
|
||||
|
||||
var common = require('./common'),
|
||||
winston = require('../winston');
|
||||
|
||||
//
|
||||
// ### function Container (options)
|
||||
// #### @options {Object} Default pass-thru options for Loggers
|
||||
// Constructor function for the Container object responsible for managing
|
||||
// a set of `winston.Logger` instances based on string ids.
|
||||
//
|
||||
var Container = exports.Container = function (options) {
|
||||
this.loggers = {};
|
||||
this.options = options || {};
|
||||
this.default = {
|
||||
transports: [
|
||||
new winston.transports.Console({
|
||||
level: 'silly',
|
||||
colorize: false
|
||||
})
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// ### function get / add (id, options)
|
||||
// #### @id {string} Id of the Logger to get
|
||||
// #### @options {Object} **Optional** Options for the Logger instance
|
||||
// Retreives a `winston.Logger` instance for the specified `id`. If
|
||||
// an instance does not exist, one is created.
|
||||
//
|
||||
Container.prototype.get = Container.prototype.add = function (id, options) {
|
||||
if (!this.loggers[id]) {
|
||||
options = common.clone(options || this.options || this.default);
|
||||
options.transports = options.transports || [];
|
||||
|
||||
if (options.transports.length === 0 && (!options || !options['console'])) {
|
||||
options.transports.push(this.default.transports[0]);
|
||||
}
|
||||
|
||||
Object.keys(options).forEach(function (key) {
|
||||
if (key === 'transports') {
|
||||
return;
|
||||
}
|
||||
|
||||
name = common.capitalize(key);
|
||||
|
||||
if (!winston.transports[name]) {
|
||||
throw new Error('Cannot add unknown transport: ' + name);
|
||||
}
|
||||
|
||||
var namedOptions = options[key];
|
||||
namedOptions.id = id;
|
||||
options.transports.push(new (winston.transports[name])(namedOptions));
|
||||
});
|
||||
|
||||
this.loggers[id] = new winston.Logger(options);
|
||||
}
|
||||
|
||||
return this.loggers[id];
|
||||
};
|
||||
|
||||
//
|
||||
// ### function close (id)
|
||||
// #### @id {string} **Optional** Id of the Logger instance to find
|
||||
// Returns a boolean value indicating if this instance
|
||||
// has a logger with the specified `id`.
|
||||
//
|
||||
Container.prototype.has = function (id) {
|
||||
return !!this.loggers[id];
|
||||
};
|
||||
|
||||
//
|
||||
// ### function close (id)
|
||||
// #### @id {string} **Optional** Id of the Logger instance to close
|
||||
// Closes a `Logger` instance with the specified `id` if it exists.
|
||||
// If no `id` is supplied then all Loggers are closed.
|
||||
//
|
||||
Container.prototype.close = function (id) {
|
||||
var self = this;
|
||||
|
||||
function _close (id) {
|
||||
if (!self.loggers[id]) {
|
||||
return;
|
||||
}
|
||||
|
||||
self.loggers[id].close();
|
||||
delete self.loggers[id];
|
||||
}
|
||||
|
||||
return id ? _close(id) : Object.keys(this.loggers).forEach(function (id) {
|
||||
_close(id);
|
||||
});
|
||||
};
|
||||
55
node_modules/winston/lib/winston/exception.js
generated
vendored
Normal file
55
node_modules/winston/lib/winston/exception.js
generated
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* exception.js: Utility methods for gathing information about uncaughtExceptions.
|
||||
*
|
||||
* (C) 2010 Charlie Robbins
|
||||
* MIT LICENCE
|
||||
*
|
||||
*/
|
||||
|
||||
var os = require('os'),
|
||||
stackTrace = require('stack-trace');
|
||||
|
||||
var exception = exports;
|
||||
|
||||
exception.getAllInfo = function (err) {
|
||||
return {
|
||||
process: exception.getProcessInfo(),
|
||||
os: exception.getOsInfo(),
|
||||
trace: exception.getTrace(err),
|
||||
stack: err.stack.split('\n')
|
||||
};
|
||||
};
|
||||
|
||||
exception.getProcessInfo = function () {
|
||||
return {
|
||||
pid: process.pid,
|
||||
uid: process.getuid(),
|
||||
gid: process.getgid(),
|
||||
cwd: process.cwd(),
|
||||
execPath: process.execPath,
|
||||
version: process.version,
|
||||
argv: process.argv,
|
||||
memoryUsage: process.memoryUsage()
|
||||
};
|
||||
};
|
||||
|
||||
exception.getOsInfo = function () {
|
||||
return {
|
||||
loadavg: os.loadavg(),
|
||||
uptime: os.uptime()
|
||||
};
|
||||
};
|
||||
|
||||
exception.getTrace = function (err) {
|
||||
var trace = err ? stackTrace.parse(err) : stackTrace.get();
|
||||
return trace.map(function (site) {
|
||||
return {
|
||||
column: site.getColumnNumber(),
|
||||
file: site.getFileName(),
|
||||
function: site.getFunctionName(),
|
||||
line: site.getLineNumber(),
|
||||
method: site.getMethodName(),
|
||||
native: site.isNative(),
|
||||
}
|
||||
});
|
||||
};
|
||||
473
node_modules/winston/lib/winston/logger.js
generated
vendored
Normal file
473
node_modules/winston/lib/winston/logger.js
generated
vendored
Normal file
@@ -0,0 +1,473 @@
|
||||
/*
|
||||
* logger.js: Core logger object used by winston.
|
||||
*
|
||||
* (C) 2010 Charlie Robbins
|
||||
* MIT LICENCE
|
||||
*
|
||||
*/
|
||||
|
||||
var events = require('events'),
|
||||
util = require('util'),
|
||||
async = require('async'),
|
||||
config = require('./config'),
|
||||
common = require('./common'),
|
||||
exception = require('./exception');
|
||||
|
||||
//
|
||||
// Time constants
|
||||
//
|
||||
var ticksPerMillisecond = 10000;
|
||||
|
||||
//
|
||||
// ### function Logger (options)
|
||||
// #### @options {Object} Options for this instance.
|
||||
// Constructor function for the Logger object responsible
|
||||
// for persisting log messages and metadata to one or more transports.
|
||||
//
|
||||
var Logger = exports.Logger = function (options) {
|
||||
events.EventEmitter.call(this);
|
||||
options = options || {};
|
||||
|
||||
var self = this,
|
||||
handleExceptions = false;
|
||||
|
||||
//
|
||||
// Set Levels and default logging level
|
||||
//
|
||||
this.padLevels = options.padLevels || false;
|
||||
this.setLevels(options.levels);
|
||||
if (options.colors) {
|
||||
config.addColors(options.colors);
|
||||
}
|
||||
|
||||
//
|
||||
// Hoist other options onto this instance.
|
||||
//
|
||||
this.level = options.level || 'info';
|
||||
this.emitErrs = options.emitErrs || false;
|
||||
this.stripColors = options.stripColors || false;
|
||||
this.exitOnError = typeof options.exitOnError !== 'undefined'
|
||||
? options.exitOnError
|
||||
: true;
|
||||
|
||||
//
|
||||
// Setup other intelligent default settings.
|
||||
//
|
||||
this.transports = {};
|
||||
this.exceptionHandlers = {};
|
||||
this.profilers = {};
|
||||
this._names = [];
|
||||
this._hnames = [];
|
||||
|
||||
if (options.transports) {
|
||||
options.transports.forEach(function (transport) {
|
||||
self.add(transport, null, true);
|
||||
|
||||
if (transport.handleExceptions) {
|
||||
handleExceptions = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (options.exceptionHandlers) {
|
||||
options.exceptionHandlers.forEach(function (handler) {
|
||||
self._hnames.push(handler.name);
|
||||
self.exceptionHandlers[handler.name] = handler;
|
||||
});
|
||||
}
|
||||
|
||||
if (options.handleExceptions || handleExceptions) {
|
||||
this.handleExceptions();
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// Inherit from `events.EventEmitter`.
|
||||
//
|
||||
util.inherits(Logger, events.EventEmitter);
|
||||
|
||||
//
|
||||
// ### function extend (target)
|
||||
// #### @target {Object} Target to extend.
|
||||
// Extends the target object with a 'log' method
|
||||
// along with a method for each level in this instance.
|
||||
//
|
||||
Logger.prototype.extend = function (target) {
|
||||
var self = this;
|
||||
['log', 'profile', 'startTimer'].concat(Object.keys(this.levels)).forEach(function (method) {
|
||||
target[method] = function () {
|
||||
return self[method].apply(self, arguments);
|
||||
};
|
||||
});
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
//
|
||||
// ### function log (level, msg, [meta], callback)
|
||||
// #### @level {string} Level at which to log the message.
|
||||
// #### @msg {string} Message to log
|
||||
// #### @meta {Object} **Optional** Additional metadata to attach
|
||||
// #### @callback {function} Continuation to respond to when complete.
|
||||
// Core logging method exposed to Winston. Metadata is optional.
|
||||
//
|
||||
Logger.prototype.log = function (level, msg) {
|
||||
var self = this,
|
||||
callback,
|
||||
meta;
|
||||
|
||||
if (arguments.length === 3) {
|
||||
if (typeof arguments[2] === 'function') {
|
||||
meta = {};
|
||||
callback = arguments[2];
|
||||
}
|
||||
else if (typeof arguments[2] === 'object') {
|
||||
meta = arguments[2];
|
||||
}
|
||||
}
|
||||
else if (arguments.length === 4) {
|
||||
meta = arguments[2];
|
||||
callback = arguments[3];
|
||||
}
|
||||
|
||||
// If we should pad for levels, do so
|
||||
if (this.padLevels) {
|
||||
msg = new Array(this.levelLength - level.length).join(' ') + msg;
|
||||
}
|
||||
|
||||
function onError (err) {
|
||||
if (callback) {
|
||||
callback(err);
|
||||
}
|
||||
else if (self.emitErrs) {
|
||||
self.emit('error', err);
|
||||
};
|
||||
}
|
||||
|
||||
if (this.transports.length === 0) {
|
||||
return onError(new Error('Cannot log with no transports.'));
|
||||
}
|
||||
else if (typeof self.levels[level] === 'undefined') {
|
||||
return onError(new Error('Unknown log level: ' + level));
|
||||
}
|
||||
|
||||
//
|
||||
// For consideration of terminal 'color" programs like colors.js,
|
||||
// which can add ANSI escape color codes to strings, we destyle the
|
||||
// ANSI color escape codes when `this.stripColors` is set.
|
||||
//
|
||||
// see: http://en.wikipedia.org/wiki/ANSI_escape_code
|
||||
//
|
||||
if (this.stripColors) {
|
||||
var code = /\u001b\[\d+m/g;
|
||||
msg = ('' + msg).replace(code, '');
|
||||
}
|
||||
|
||||
for (var i = 0, l = this._names.length; i < l; i++) {
|
||||
var transport = this.transports[this._names[i]];
|
||||
if ((transport.level && self.levels[transport.level] <= self.levels[level])
|
||||
|| (!transport.level && self.levels[self.level] <= self.levels[level])) {
|
||||
transport.log(level, msg, meta, function (err) {
|
||||
self.emit('logging', transport, level, msg, meta);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Immediately respond to the callback
|
||||
//
|
||||
if (callback) {
|
||||
callback(null, level, msg, meta);
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
//
|
||||
// ### function close ()
|
||||
// Cleans up resources (streams, event listeners) for all
|
||||
// transports associated with this instance (if necessary).
|
||||
//
|
||||
Logger.prototype.close = function () {
|
||||
var self = this;
|
||||
|
||||
this._names.forEach(function (name) {
|
||||
var transport = self.transports[name];
|
||||
if (transport && transport.close) {
|
||||
transport.close();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
//
|
||||
// ### function handleExceptions ()
|
||||
// Handles `uncaughtException` events for the current process
|
||||
//
|
||||
Logger.prototype.handleExceptions = function () {
|
||||
var args = Array.prototype.slice.call(arguments),
|
||||
handlers = [],
|
||||
self = this;
|
||||
|
||||
args.forEach(function (a) {
|
||||
if (Array.isArray(a)) {
|
||||
handlers = handlers.concat(a);
|
||||
}
|
||||
else {
|
||||
handlers.push(a);
|
||||
}
|
||||
});
|
||||
|
||||
handlers.forEach(function (handler) {
|
||||
self.exceptionHandlers[handler.name] = handler;
|
||||
});
|
||||
|
||||
this._hnames = Object.keys(self.exceptionHandlers);
|
||||
|
||||
if (!this.catchExceptions) {
|
||||
this.catchExceptions = this._uncaughtException.bind(this);
|
||||
process.on('uncaughtException', this.catchExceptions);
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// ### function unhandleExceptions ()
|
||||
// Removes any handlers to `uncaughtException` events
|
||||
// for the current process
|
||||
//
|
||||
Logger.prototype.unhandleExceptions = function () {
|
||||
var self = this;
|
||||
|
||||
if (this.catchExceptions) {
|
||||
Object.keys(this.exceptionHandlers).forEach(function (name) {
|
||||
if (handler.close) {
|
||||
handler.close();
|
||||
}
|
||||
});
|
||||
|
||||
this.exceptionHandlers = {};
|
||||
Object.keys(this.transports).forEach(function (name) {
|
||||
var transport = self.transports[name];
|
||||
if (transport.handleExceptions) {
|
||||
transport.handleExceptions = false;
|
||||
}
|
||||
})
|
||||
|
||||
process.removeListener('uncaughtException', this.catchExceptions);
|
||||
this.catchExceptions = false;
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// ### function add (transport, [options])
|
||||
// #### @transport {Transport} Prototype of the Transport object to add.
|
||||
// #### @options {Object} **Optional** Options for the Transport to add.
|
||||
// #### @instance {Boolean} **Optional** Value indicating if `transport` is already instantiated.
|
||||
// Adds a transport of the specified type to this instance.
|
||||
//
|
||||
Logger.prototype.add = function (transport, options, created) {
|
||||
var instance = created ? transport : (new (transport)(options));
|
||||
|
||||
if (!instance.name && !instance.log) {
|
||||
throw new Error('Unknown transport with no log() method');
|
||||
}
|
||||
else if (this.transports[instance.name]) {
|
||||
throw new Error('Transport already attached: ' + instance.name);
|
||||
}
|
||||
|
||||
this.transports[instance.name] = instance;
|
||||
this._names = Object.keys(this.transports);
|
||||
|
||||
//
|
||||
// Listen for the `error` event on the new Transport
|
||||
//
|
||||
instance._onError = this._onError.bind(this, instance)
|
||||
instance.on('error', instance._onError);
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
//
|
||||
// ### function remove (transport)
|
||||
// #### @transport {Transport} Transport to remove.
|
||||
// Removes a transport of the specified type from this instance.
|
||||
//
|
||||
Logger.prototype.remove = function (transport) {
|
||||
var name = transport.name || transport.prototype.name;
|
||||
|
||||
if (!this.transports[name]) {
|
||||
throw new Error('Transport ' + name + ' not attached to this instance');
|
||||
}
|
||||
|
||||
var instance = this.transports[name];
|
||||
delete this.transports[name];
|
||||
this._names = Object.keys(this.transports);
|
||||
|
||||
if (instance.close) {
|
||||
instance.close();
|
||||
}
|
||||
|
||||
instance.removeListener('error', instance._onError);
|
||||
return this;
|
||||
};
|
||||
|
||||
var ProfileHandler = function(logger){
|
||||
this.logger = logger;
|
||||
|
||||
this.start = Date.now();
|
||||
|
||||
this.done = function(msg){
|
||||
args = Array.prototype.slice.call(arguments);
|
||||
callback = typeof args[args.length - 1] === 'function' ? args.pop() : null;
|
||||
meta = typeof args[args.length - 1] === 'object' ? args.pop() : {};
|
||||
|
||||
meta.duration = (Date.now()) - this.start + 'ms';
|
||||
|
||||
return this.logger.info(msg, meta, callback);
|
||||
}
|
||||
}
|
||||
|
||||
Logger.prototype.startTimer = function(){
|
||||
return new ProfileHandler(this);
|
||||
}
|
||||
|
||||
//
|
||||
// ### function profile (id, [msg, meta, callback])
|
||||
// #### @id {string} Unique id of the profiler
|
||||
// #### @msg {string} **Optional** Message to log
|
||||
// #### @meta {Object} **Optional** Additional metadata to attach
|
||||
// #### @callback {function} **Optional** Continuation to respond to when complete.
|
||||
// Tracks the time inbetween subsequent calls to this method
|
||||
// with the same `id` parameter. The second call to this method
|
||||
// will log the difference in milliseconds along with the message.
|
||||
//
|
||||
Logger.prototype.profile = function (id) {
|
||||
var now = Date.now(), then, args,
|
||||
msg, meta, callback;
|
||||
|
||||
if (this.profilers[id]) {
|
||||
then = this.profilers[id];
|
||||
delete this.profilers[id];
|
||||
|
||||
// Support variable arguments: msg, meta, callback
|
||||
args = Array.prototype.slice.call(arguments);
|
||||
callback = typeof args[args.length - 1] === 'function' ? args.pop() : null;
|
||||
meta = typeof args[args.length - 1] === 'object' ? args.pop() : {};
|
||||
msg = args.length === 2 ? args[1] : id;
|
||||
|
||||
// Set the duration property of the metadata
|
||||
meta.duration = now - then + 'ms';
|
||||
return this.info(msg, meta, callback);
|
||||
}
|
||||
else {
|
||||
this.profilers[id] = now;
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
//
|
||||
// ### function setLevels (target)
|
||||
// #### @target {Object} Target levels to use on this instance
|
||||
// Sets the `target` levels specified on this instance.
|
||||
//
|
||||
Logger.prototype.setLevels = function (target) {
|
||||
return common.setLevels(this, this.levels, target);
|
||||
};
|
||||
|
||||
//
|
||||
// ### function cli ()
|
||||
// Configures this instance to have the default
|
||||
// settings for command-line interfaces: no timestamp,
|
||||
// colors enabled, padded output, and additional levels.
|
||||
//
|
||||
Logger.prototype.cli = function () {
|
||||
this.padLevels = true;
|
||||
this.setLevels(config.cli.levels);
|
||||
config.addColors(config.cli.colors);
|
||||
|
||||
if (this.transports.console) {
|
||||
this.transports.console.colorize = true;
|
||||
this.transports.console.timestamp = false;
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
//
|
||||
// ### @private function _uncaughtException (err)
|
||||
// #### @err {Error} Error to handle
|
||||
// Logs all relevant information around the `err` and
|
||||
// exits the current process.
|
||||
//
|
||||
Logger.prototype._uncaughtException = function (err) {
|
||||
var self = this,
|
||||
responded = false,
|
||||
info = exception.getAllInfo(err),
|
||||
handlers = this._getExceptionHandlers(),
|
||||
timeout,
|
||||
doExit;
|
||||
|
||||
//
|
||||
// Calculate if we should exit on this error
|
||||
//
|
||||
doExit = typeof this.exitOnError === 'function'
|
||||
? this.exitOnError(err)
|
||||
: this.exitOnError;
|
||||
|
||||
function logAndWait(transport, next) {
|
||||
transport.logException('uncaughtException', info, next);
|
||||
}
|
||||
|
||||
function gracefulExit() {
|
||||
if (doExit && !responded) {
|
||||
//
|
||||
// Remark: Currently ignoring any exceptions from transports
|
||||
// when catching uncaught exceptions.
|
||||
//
|
||||
clearTimeout(timeout);
|
||||
responded = true;
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (!handlers || handlers.length === 0) {
|
||||
return gracefulExit();
|
||||
}
|
||||
|
||||
//
|
||||
// Log to all transports and allow the operation to take
|
||||
// only up to `3000ms`.
|
||||
//
|
||||
async.forEach(handlers, logAndWait, gracefulExit);
|
||||
if (doExit) {
|
||||
timeout = setTimeout(gracefulExit, 3000);
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// ### @private function _getExceptionHandlers ()
|
||||
// Returns the list of transports and exceptionHandlers
|
||||
// for this instance.
|
||||
//
|
||||
Logger.prototype._getExceptionHandlers = function () {
|
||||
var self = this;
|
||||
|
||||
return this._hnames.map(function (name) {
|
||||
return self.exceptionHandlers[name];
|
||||
}).concat(this._names.map(function (name) {
|
||||
return self.transports[name].handleExceptions && self.transports[name];
|
||||
})).filter(Boolean);
|
||||
};
|
||||
|
||||
//
|
||||
// ### @private function _onError (transport, err)
|
||||
// #### @transport {Object} Transport on which the error occured
|
||||
// #### @err {Error} Error that occurred on the transport
|
||||
// Bubbles the error, `err`, that occured on the specified `transport`
|
||||
// up from this instance if `emitErrs` has been set.
|
||||
//
|
||||
Logger.prototype._onError = function (transport, err) {
|
||||
if (this.emitErrs) {
|
||||
this.emit('error', err, transport);
|
||||
}
|
||||
};
|
||||
29
node_modules/winston/lib/winston/transports.js
generated
vendored
Normal file
29
node_modules/winston/lib/winston/transports.js
generated
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* transports.js: Set of all transports Winston knows about
|
||||
*
|
||||
* (C) 2010 Charlie Robbins
|
||||
* MIT LICENCE
|
||||
*
|
||||
*/
|
||||
|
||||
var fs = require('fs'),
|
||||
path = require('path'),
|
||||
common = require('./common');
|
||||
|
||||
var transports = exports;
|
||||
|
||||
//
|
||||
// Setup all transports as lazy-loaded getters.
|
||||
//
|
||||
fs.readdirSync(path.join(__dirname, 'transports')).forEach(function (file) {
|
||||
var transport = file.replace('.js', ''),
|
||||
name = common.capitalize(transport);
|
||||
|
||||
if (transport === 'transport') {
|
||||
return;
|
||||
}
|
||||
|
||||
transports.__defineGetter__(name, function () {
|
||||
return require('./transports/' + transport)[name];
|
||||
});
|
||||
});
|
||||
83
node_modules/winston/lib/winston/transports/console.js
generated
vendored
Normal file
83
node_modules/winston/lib/winston/transports/console.js
generated
vendored
Normal file
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* console.js: Transport for outputting to the console
|
||||
*
|
||||
* (C) 2010 Charlie Robbins
|
||||
* MIT LICENCE
|
||||
*
|
||||
*/
|
||||
|
||||
var events = require('events'),
|
||||
util = require('util'),
|
||||
colors = require('colors'),
|
||||
common = require('../common'),
|
||||
Transport = require('./transport').Transport;
|
||||
|
||||
//
|
||||
// ### function Console (options)
|
||||
// #### @options {Object} Options for this instance.
|
||||
// Constructor function for the Console transport object responsible
|
||||
// for persisting log messages and metadata to a terminal or TTY.
|
||||
//
|
||||
var Console = exports.Console = function (options) {
|
||||
Transport.call(this, options);
|
||||
options = options || {};
|
||||
|
||||
this.name = 'console';
|
||||
this.json = options.json || false;
|
||||
this.colorize = options.colorize || false;
|
||||
this.timestamp = typeof options.timestamp !== 'undefined' ? options.timestamp : false;
|
||||
|
||||
if (this.json) {
|
||||
this.stringify = options.stringify || function (obj) {
|
||||
return JSON.stringify(obj, null, 2);
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// Inherit from `winston.Transport`.
|
||||
//
|
||||
util.inherits(Console, Transport);
|
||||
|
||||
//
|
||||
// Expose the name of this Transport on the prototype
|
||||
//
|
||||
Console.prototype.name = 'console';
|
||||
|
||||
//
|
||||
// ### function log (level, msg, [meta], callback)
|
||||
// #### @level {string} Level at which to log the message.
|
||||
// #### @msg {string} Message to log
|
||||
// #### @meta {Object} **Optional** Additional metadata to attach
|
||||
// #### @callback {function} Continuation to respond to when complete.
|
||||
// Core logging method exposed to Winston. Metadata is optional.
|
||||
//
|
||||
Console.prototype.log = function (level, msg, meta, callback) {
|
||||
if (this.silent) {
|
||||
return callback(null, true);
|
||||
}
|
||||
|
||||
var self = this, output = common.log({
|
||||
colorize: this.colorize,
|
||||
json: this.json,
|
||||
level: level,
|
||||
message: msg,
|
||||
meta: meta,
|
||||
stringify: this.stringify,
|
||||
timestamp: this.timestamp
|
||||
});
|
||||
|
||||
if (level === 'error' || level === 'debug') {
|
||||
util.error(output);
|
||||
}
|
||||
else {
|
||||
util.puts(output);
|
||||
}
|
||||
|
||||
//
|
||||
// Emit the `logged` event immediately because the event loop
|
||||
// will not exit until `process.stdout` has drained anyway.
|
||||
//
|
||||
self.emit('logged');
|
||||
callback(null, true);
|
||||
};
|
||||
122
node_modules/winston/lib/winston/transports/couchdb.js
generated
vendored
Normal file
122
node_modules/winston/lib/winston/transports/couchdb.js
generated
vendored
Normal file
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* Couchdb.js: Transport for logging to Couchdb
|
||||
*
|
||||
* (C) 2011 Max Ogden
|
||||
* MIT LICENSE
|
||||
*
|
||||
*/
|
||||
|
||||
var events = require('events'),
|
||||
http = require('http'),
|
||||
util = require('util'),
|
||||
common = require('../common'),
|
||||
Transport = require('./transport').Transport;
|
||||
|
||||
//
|
||||
// ### function Couchdb (options)
|
||||
// #### @options {Object} Options for this instance.
|
||||
// Constructor function for the Console transport object responsible
|
||||
// for making arbitrary HTTP requests whenever log messages and metadata
|
||||
// are received.
|
||||
//
|
||||
var Couchdb = exports.Couchdb = function (options) {
|
||||
Transport.call(this, options);
|
||||
|
||||
this.name = 'Couchdb';
|
||||
this.db = options.db;
|
||||
this.user = options.user;
|
||||
this.pass = options.pass;
|
||||
this.host = options.host || 'localhost';
|
||||
this.port = options.port || 5984;
|
||||
|
||||
if (options.auth) {
|
||||
//
|
||||
// TODO: add http basic auth options for outgoing HTTP requests
|
||||
//
|
||||
}
|
||||
|
||||
if (options.ssl) {
|
||||
//
|
||||
// TODO: add ssl support for outgoing HTTP requests
|
||||
//
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// Inherit from `winston.Transport`.
|
||||
//
|
||||
util.inherits(Couchdb, Transport);
|
||||
|
||||
//
|
||||
// Expose the name of this Transport on the prototype
|
||||
//
|
||||
Couchdb.prototype.name = 'Couchdb';
|
||||
|
||||
//
|
||||
// ### function log (level, msg, [meta], callback)
|
||||
// #### @level {string} Level at which to log the message.
|
||||
// #### @msg {string} Message to log
|
||||
// #### @meta {Object} **Optional** Additional metadata to attach
|
||||
// #### @callback {function} Continuation to respond to when complete.
|
||||
// Core logging method exposed to Winston. Metadata is optional.
|
||||
//
|
||||
Couchdb.prototype.log = function (level, msg, meta, callback) {
|
||||
if (this.silent) {
|
||||
return callback(null, true);
|
||||
}
|
||||
|
||||
var self = this,
|
||||
message = common.clone(meta),
|
||||
options,
|
||||
req;
|
||||
|
||||
message.level = level;
|
||||
message.message = msg;
|
||||
|
||||
// Prepare options for outgoing HTTP request
|
||||
options = {
|
||||
host: this.host,
|
||||
port: this.port,
|
||||
path: "/" + this.db,
|
||||
method: "POST",
|
||||
headers: {"content-type": "application/json"}
|
||||
};
|
||||
|
||||
if (options.user && options.pass) {
|
||||
options.headers["Authorization"] = "Basic " + new Buffer(options.user + ":" + options.pass).toString('base64');
|
||||
}
|
||||
|
||||
// Perform HTTP logging request
|
||||
req = http.request(options, function (res) {
|
||||
//
|
||||
// No callback on request, fire and forget about the response
|
||||
//
|
||||
self.emit('logged', res);
|
||||
});
|
||||
|
||||
req.on('error', function (err) {
|
||||
//
|
||||
// Propagate the `error` back up to the `Logger` that this
|
||||
// instance belongs to.
|
||||
//
|
||||
self.emit('error', err);
|
||||
});
|
||||
|
||||
//
|
||||
// Write logging event to the outgoing request body
|
||||
//
|
||||
req.write(JSON.stringify({
|
||||
method: 'log',
|
||||
params: {
|
||||
timestamp: new Date(), // RFC3339/ISO8601 format instead of common.timestamp()
|
||||
msg: msg,
|
||||
level: level,
|
||||
meta: meta
|
||||
}
|
||||
}));
|
||||
|
||||
req.end();
|
||||
|
||||
// Always return true, regardless of any errors
|
||||
callback(null, true);
|
||||
};
|
||||
332
node_modules/winston/lib/winston/transports/file.js
generated
vendored
Normal file
332
node_modules/winston/lib/winston/transports/file.js
generated
vendored
Normal file
@@ -0,0 +1,332 @@
|
||||
/*
|
||||
* file.js: Transport for outputting to a local log file
|
||||
*
|
||||
* (C) 2010 Charlie Robbins
|
||||
* MIT LICENCE
|
||||
*
|
||||
*/
|
||||
|
||||
var events = require('events'),
|
||||
fs = require('fs'),
|
||||
path = require('path'),
|
||||
util = require('util'),
|
||||
colors = require('colors'),
|
||||
common = require('../common'),
|
||||
Transport = require('./transport').Transport;
|
||||
|
||||
//
|
||||
// ### function File (options)
|
||||
// #### @options {Object} Options for this instance.
|
||||
// Constructor function for the File transport object responsible
|
||||
// for persisting log messages and metadata to one or more files.
|
||||
//
|
||||
var File = exports.File = function (options) {
|
||||
Transport.call(this, options);
|
||||
|
||||
//
|
||||
// Helper function which throws an `Error` in the event
|
||||
// that any of the rest of the arguments is present in `options`.
|
||||
//
|
||||
function throwIf (target /*, illegal... */) {
|
||||
Array.prototype.slice.call(arguments, 1).forEach(function (name) {
|
||||
if (options[name]) {
|
||||
throw new Error('Cannot set ' + name + ' and ' + target + 'together');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (options.filename || options.dirname) {
|
||||
throwIf('filename or dirname', 'stream');
|
||||
this._basename = this.filename = path.basename(options.filename) || 'winston.log';
|
||||
this.dirname = options.dirname || path.dirname(options.filename);
|
||||
this.options = options.options || { flags: 'a' };
|
||||
}
|
||||
else if (options.stream) {
|
||||
throwIf('stream', 'filename', 'maxsize');
|
||||
this.stream = options.stream;
|
||||
}
|
||||
else {
|
||||
throw new Error('Cannot log to file without filename or stream.');
|
||||
}
|
||||
|
||||
this.json = options.json !== false;
|
||||
this.colorize = options.colorize || false;
|
||||
this.maxsize = options.maxsize || null;
|
||||
this.maxFiles = options.maxFiles || null;
|
||||
this.timestamp = typeof options.timestamp !== 'undefined' ? options.timestamp : false;
|
||||
|
||||
//
|
||||
// Internal state variables representing the number
|
||||
// of files this instance has created and the current
|
||||
// size (in bytes) of the current logfile.
|
||||
//
|
||||
this._size = 0;
|
||||
this._created = 0;
|
||||
this._buffer = [];
|
||||
this._draining = false;
|
||||
};
|
||||
|
||||
//
|
||||
// Inherit from `winston.Transport`.
|
||||
//
|
||||
util.inherits(File, Transport);
|
||||
|
||||
//
|
||||
// Expose the name of this Transport on the prototype
|
||||
//
|
||||
File.prototype.name = 'file';
|
||||
|
||||
//
|
||||
// ### function log (level, msg, [meta], callback)
|
||||
// #### @level {string} Level at which to log the message.
|
||||
// #### @msg {string} Message to log
|
||||
// #### @meta {Object} **Optional** Additional metadata to attach
|
||||
// #### @callback {function} Continuation to respond to when complete.
|
||||
// Core logging method exposed to Winston. Metadata is optional.
|
||||
//
|
||||
File.prototype.log = function (level, msg, meta, callback) {
|
||||
if (this.silent) {
|
||||
return callback(null, true);
|
||||
}
|
||||
|
||||
var self = this, output = common.log({
|
||||
level: level,
|
||||
message: msg,
|
||||
meta: meta,
|
||||
json: this.json,
|
||||
colorize: this.colorize,
|
||||
timestamp: this.timestamp
|
||||
}) + '\n';
|
||||
|
||||
this._size += output.length;
|
||||
|
||||
if (!this.filename) {
|
||||
//
|
||||
// If there is no `filename` on this instance then it was configured
|
||||
// with a raw `WriteableStream` instance and we should not perform any
|
||||
// size restrictions.
|
||||
//
|
||||
this.stream.write(output);
|
||||
self._lazyDrain();
|
||||
}
|
||||
else {
|
||||
this.open(function (err) {
|
||||
if (err) {
|
||||
//
|
||||
// If there was an error enqueue the message
|
||||
//
|
||||
return self._buffer.push(output);
|
||||
}
|
||||
|
||||
self.stream.write(output);
|
||||
self._lazyDrain();
|
||||
});
|
||||
}
|
||||
|
||||
callback(null, true);
|
||||
};
|
||||
|
||||
//
|
||||
// ### function open (callback)
|
||||
// #### @callback {function} Continuation to respond to when complete
|
||||
// Checks to see if a new file needs to be created based on the `maxsize`
|
||||
// (if any) and the current size of the file used.
|
||||
//
|
||||
File.prototype.open = function (callback) {
|
||||
if (this.opening) {
|
||||
//
|
||||
// If we are already attempting to open the next
|
||||
// available file then respond with a value indicating
|
||||
// that the message should be buffered.
|
||||
//
|
||||
return callback(true);
|
||||
}
|
||||
else if (!this.stream || (this.maxsize && this._size >= this.maxsize)) {
|
||||
//
|
||||
// If we dont have a stream or have exceeded our size, then create
|
||||
// the next stream and respond with a value indicating that
|
||||
// the message should be buffered.
|
||||
//
|
||||
callback(true);
|
||||
return this._createStream();
|
||||
}
|
||||
|
||||
//
|
||||
// Otherwise we have a valid (and ready) stream.
|
||||
//
|
||||
callback();
|
||||
};
|
||||
|
||||
//
|
||||
// ### function close ()
|
||||
// Closes the stream associated with this instance.
|
||||
//
|
||||
File.prototype.close = function() {
|
||||
var self = this;
|
||||
|
||||
if (this.stream) {
|
||||
this.stream.end();
|
||||
this.stream.destroySoon();
|
||||
|
||||
this.stream.once('drain', function () {
|
||||
self.emit('flush');
|
||||
self.emit('closed');
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// ### function flush ()
|
||||
// Flushes any buffered messages to the current `stream`
|
||||
// used by this instance.
|
||||
//
|
||||
File.prototype.flush = function () {
|
||||
var self = this;
|
||||
|
||||
//
|
||||
// Iterate over the `_buffer` of enqueued messaged
|
||||
// and then write them to the newly created stream.
|
||||
//
|
||||
this._buffer.forEach(function (str) {
|
||||
process.nextTick(function () {
|
||||
self.stream.write(str);
|
||||
self._size += str.length;
|
||||
});
|
||||
});
|
||||
|
||||
//
|
||||
// Quickly truncate the `_buffer` once the write operations
|
||||
// have been started
|
||||
//
|
||||
self._buffer.length = 0;
|
||||
|
||||
//
|
||||
// When the stream has drained we have flushed
|
||||
// our buffer.
|
||||
//
|
||||
self.stream.once('drain', function () {
|
||||
self.emit('flush');
|
||||
self.emit('logged');
|
||||
});
|
||||
};
|
||||
|
||||
//
|
||||
// ### @private function _createStream ()
|
||||
// Attempts to open the next appropriate file for this instance
|
||||
// based on the common state (such as `maxsize` and `_basename`).
|
||||
//
|
||||
File.prototype._createStream = function () {
|
||||
var self = this;
|
||||
this.opening = true;
|
||||
|
||||
(function checkFile (target) {
|
||||
var fullname = path.join(self.dirname, target);
|
||||
|
||||
//
|
||||
// Creates the `WriteStream` and then flushes any
|
||||
// buffered messages.
|
||||
//
|
||||
function createAndFlush (size) {
|
||||
if (self.stream) {
|
||||
self.stream.end();
|
||||
self.stream.destroySoon();
|
||||
}
|
||||
|
||||
self._size = size;
|
||||
self.filename = target;
|
||||
self.stream = fs.createWriteStream(fullname, self.options);
|
||||
|
||||
//
|
||||
// When the current stream has finished flushing
|
||||
// then we can be sure we have finished opening
|
||||
// and thus can emit the `open` event.
|
||||
//
|
||||
self.once('flush', function () {
|
||||
self.opening = false;
|
||||
self.emit('open', fullname);
|
||||
});
|
||||
|
||||
//
|
||||
// Remark: It is possible that in the time it has taken to find the
|
||||
// next logfile to be written more data than `maxsize` has been buffered,
|
||||
// but for sensible limits (10s - 100s of MB) this seems unlikely in less
|
||||
// than one second.
|
||||
//
|
||||
self.flush();
|
||||
}
|
||||
|
||||
fs.stat(fullname, function (err, stats) {
|
||||
if (err) {
|
||||
if (err.code !== 'ENOENT') {
|
||||
return self.emit('error', err);
|
||||
}
|
||||
|
||||
return createAndFlush(0);
|
||||
}
|
||||
|
||||
if (!stats || (self.maxsize && stats.size >= self.maxsize)) {
|
||||
//
|
||||
// If `stats.size` is greater than the `maxsize` for
|
||||
// this instance then try again
|
||||
//
|
||||
return checkFile(self._getFile(true));
|
||||
}
|
||||
|
||||
createAndFlush(stats.size);
|
||||
});
|
||||
})(this._getFile());
|
||||
};
|
||||
|
||||
//
|
||||
// ### @private function _getFile ()
|
||||
// Gets the next filename to use for this instance
|
||||
// in the case that log filesizes are being capped.
|
||||
//
|
||||
File.prototype._getFile = function (inc) {
|
||||
var self = this,
|
||||
ext = path.extname(this._basename),
|
||||
basename = path.basename(this._basename, ext),
|
||||
remaining;
|
||||
|
||||
if (inc) {
|
||||
//
|
||||
// Increment the number of files created or
|
||||
// checked by this instance.
|
||||
//
|
||||
// Check for maxFiles option and delete file
|
||||
if (this.maxFiles && (this._created >= (this.maxFiles - 1))) {
|
||||
remaining = this._created - (this.maxFiles - 1);
|
||||
if (remaining === 0) {
|
||||
fs.unlinkSync(path.join(this.dirname, basename + ext));
|
||||
}
|
||||
else {
|
||||
fs.unlinkSync(path.join(this.dirname, basename + remaining + ext));
|
||||
}
|
||||
}
|
||||
|
||||
this._created += 1;
|
||||
}
|
||||
|
||||
return this._created
|
||||
? basename + this._created + ext
|
||||
: basename + ext;
|
||||
};
|
||||
|
||||
//
|
||||
// ### @private function _lazyDrain ()
|
||||
// Lazily attempts to emit the `logged` event when `this.stream` has
|
||||
// drained. This is really just a simple mutex that only works because
|
||||
// Node.js is single-threaded.
|
||||
//
|
||||
File.prototype._lazyDrain = function () {
|
||||
var self = this;
|
||||
|
||||
if (!this._draining && this.stream) {
|
||||
this._draining = true;
|
||||
|
||||
this.stream.once('drain', function () {
|
||||
this._draining = false;
|
||||
self.emit('logged');
|
||||
});
|
||||
}
|
||||
};
|
||||
161
node_modules/winston/lib/winston/transports/loggly.js
generated
vendored
Normal file
161
node_modules/winston/lib/winston/transports/loggly.js
generated
vendored
Normal file
@@ -0,0 +1,161 @@
|
||||
/*
|
||||
* loggly.js: Transport for logginh to remote Loggly API
|
||||
*
|
||||
* (C) 2010 Charlie Robbins
|
||||
* MIT LICENCE
|
||||
*
|
||||
*/
|
||||
|
||||
var events = require('events'),
|
||||
loggly = require('loggly'),
|
||||
util = require('util'),
|
||||
async = require('async'),
|
||||
common = require('../common'),
|
||||
Transport = require('./transport').Transport;
|
||||
|
||||
//
|
||||
// ### function Loggly (options)
|
||||
// #### @options {Object} Options for this instance.
|
||||
// Constructor function for the Loggly transport object responsible
|
||||
// for persisting log messages and metadata to Loggly; 'LaaS'.
|
||||
//
|
||||
var Loggly = exports.Loggly = function (options) {
|
||||
Transport.call(this, options);
|
||||
|
||||
function valid() {
|
||||
return options.inputToken
|
||||
|| options.inputName && options.auth
|
||||
|| options.inputName && options.inputs && options.inputs[options.inputName]
|
||||
|| options.id && options.inputs && options.inputs[options.id];
|
||||
}
|
||||
|
||||
if (!options.subdomain) {
|
||||
throw new Error('Loggly Subdomain is required');
|
||||
}
|
||||
|
||||
if (!valid()) {
|
||||
throw new Error('Target input token or name is required.');
|
||||
}
|
||||
|
||||
this.name = 'loggly';
|
||||
this.logBuffer = [];
|
||||
|
||||
this.client = loggly.createClient({
|
||||
subdomain: options.subdomain,
|
||||
auth: options.auth || null,
|
||||
json: options.json || false
|
||||
});
|
||||
|
||||
if (options.inputToken) {
|
||||
this.inputToken = options.inputToken;
|
||||
this.ready = true;
|
||||
}
|
||||
else if (options.inputs && (options.inputs[options.inputName]
|
||||
|| options.inputs[options.id])) {
|
||||
this.inputToken = options.inputs[options.inputName] || options.inputs[options.id];
|
||||
this.ready = true;
|
||||
}
|
||||
else if (options.inputName) {
|
||||
this.ready = false;
|
||||
this.inputName = options.inputName;
|
||||
|
||||
var self = this;
|
||||
this.client.getInput(this.inputName, function (err, input) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
self.inputToken = input.input_token;
|
||||
self.ready = true;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// Inherit from `winston.Transport`.
|
||||
//
|
||||
util.inherits(Loggly, Transport);
|
||||
|
||||
//
|
||||
// Expose the name of this Transport on the prototype
|
||||
//
|
||||
Loggly.prototype.name = 'loggly';
|
||||
|
||||
//
|
||||
// ### function log (level, msg, [meta], callback)
|
||||
// #### @level {string} Level at which to log the message.
|
||||
// #### @msg {string} Message to log
|
||||
// #### @meta {Object} **Optional** Additional metadata to attach
|
||||
// #### @callback {function} Continuation to respond to when complete.
|
||||
// Core logging method exposed to Winston. Metadata is optional.
|
||||
//
|
||||
Loggly.prototype.log = function (level, msg, meta, callback) {
|
||||
if (this.silent) {
|
||||
return callback(null, true);
|
||||
}
|
||||
|
||||
var self = this,
|
||||
message = common.clone(meta || {});
|
||||
|
||||
message.level = level;
|
||||
message.message = msg;
|
||||
|
||||
if (!this.ready) {
|
||||
//
|
||||
// If we haven't gotten the input token yet
|
||||
// add this message to the log buffer.
|
||||
//
|
||||
this.logBuffer.push(message);
|
||||
}
|
||||
else if (this.ready && this.logBuffer.length > 0) {
|
||||
//
|
||||
// Otherwise if we have buffered messages
|
||||
// add this message to the buffer and flush them.
|
||||
//
|
||||
this.logBuffer.push(message);
|
||||
this.flush();
|
||||
}
|
||||
else {
|
||||
//
|
||||
// Otherwise just log the message as normal
|
||||
//
|
||||
this.client.log(this.inputToken, message, function () {
|
||||
self.emit('logged');
|
||||
});
|
||||
}
|
||||
|
||||
callback(null, true);
|
||||
};
|
||||
|
||||
//
|
||||
// ### function flush ()
|
||||
// Flushes any buffered messages to the current `stream`
|
||||
// used by this instance.
|
||||
//
|
||||
Loggly.prototype.flush = function () {
|
||||
var self = this;
|
||||
|
||||
function logMsg (msg, next) {
|
||||
self.client.log(self.inputToken, msg, function (err) {
|
||||
if (err) {
|
||||
self.emit('error', err);
|
||||
}
|
||||
|
||||
next();
|
||||
});
|
||||
}
|
||||
|
||||
//
|
||||
// Initiate calls to loggly for each message in the buffer
|
||||
//
|
||||
async.forEach(this.logBuffer, logMsg, function () {
|
||||
self.emit('logged');
|
||||
});
|
||||
|
||||
process.nextTick(function () {
|
||||
//
|
||||
// Then quickly truncate the list
|
||||
//
|
||||
self.logBuffer.length = 0;
|
||||
});
|
||||
};
|
||||
57
node_modules/winston/lib/winston/transports/transport.js
generated
vendored
Normal file
57
node_modules/winston/lib/winston/transports/transport.js
generated
vendored
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* transport.js: Base Transport object for all Winston transports.
|
||||
*
|
||||
* (C) 2010 Charlie Robbins
|
||||
* MIT LICENCE
|
||||
*
|
||||
*/
|
||||
|
||||
var events = require('events'),
|
||||
util = require('util');
|
||||
|
||||
//
|
||||
// ### function Transport (options)
|
||||
// #### @options {Object} Options for this instance.
|
||||
// Constructor function for the Tranport object responsible
|
||||
// base functionality for all winston transports.
|
||||
//
|
||||
var Transport = exports.Transport = function (options) {
|
||||
events.EventEmitter.call(this);
|
||||
|
||||
options = options || {};
|
||||
this.level = options.level || 'info';
|
||||
this.silent = options.silent || false;
|
||||
this.handleExceptions = options.handleExceptions || false;
|
||||
};
|
||||
|
||||
//
|
||||
// Inherit from `events.EventEmitter`.
|
||||
//
|
||||
util.inherits(Transport, events.EventEmitter);
|
||||
|
||||
//
|
||||
// ### function logException (msg, meta, callback)
|
||||
// #### @msg {string} Message to log
|
||||
// #### @meta {Object} **Optional** Additional metadata to attach
|
||||
// #### @callback {function} Continuation to respond to when complete.
|
||||
// Logs the specified `msg`, `meta` and responds to the callback once the log
|
||||
// operation is complete to ensure that the event loop will not exit before
|
||||
// all logging has completed.
|
||||
//
|
||||
Transport.prototype.logException = function (msg, meta, callback) {
|
||||
var self = this;
|
||||
|
||||
function onLogged () {
|
||||
self.removeListener('error', onError);
|
||||
callback();
|
||||
}
|
||||
|
||||
function onError () {
|
||||
self.removeListener('logged', onLogged);
|
||||
callback();
|
||||
}
|
||||
|
||||
this.once('logged', onLogged);
|
||||
this.once('error', onError);
|
||||
this.log('error', msg, meta, function () { });
|
||||
};
|
||||
136
node_modules/winston/lib/winston/transports/webhook.js
generated
vendored
Normal file
136
node_modules/winston/lib/winston/transports/webhook.js
generated
vendored
Normal file
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* webhook.js: Transport for logging to remote http endpoints ( POST / RECEIVE webhooks )
|
||||
*
|
||||
* (C) 2011 Marak Squires
|
||||
* MIT LICENCE
|
||||
*
|
||||
*/
|
||||
|
||||
var events = require('events'),
|
||||
http = require('http'),
|
||||
https = require('https'),
|
||||
util = require('util'),
|
||||
common = require('../common'),
|
||||
Transport = require('./transport').Transport;
|
||||
|
||||
//
|
||||
// ### function WebHook (options)
|
||||
// #### @options {Object} Options for this instance.
|
||||
// Constructor function for the Console transport object responsible
|
||||
// for making arbitrary HTTP requests whenever log messages and metadata
|
||||
// are received.
|
||||
//
|
||||
var Webhook = exports.Webhook = function (options) {
|
||||
Transport.call(this, options);
|
||||
|
||||
this.name = 'webhook';
|
||||
this.host = options.host || 'localhost';
|
||||
this.port = options.port || 8080;
|
||||
this.method = options.method || 'POST';
|
||||
this.path = options.path || '/winston-log';
|
||||
|
||||
if (options.auth) {
|
||||
this.auth = {};
|
||||
this.auth.username = options.auth.username || '';
|
||||
this.auth.password = options.auth.password || '';
|
||||
}
|
||||
|
||||
if (options.ssl) {
|
||||
this.ssl = {};
|
||||
this.ssl.key = options.ssl.key || null;
|
||||
this.ssl.cert = options.ssl.cert || null;
|
||||
this.ssl.ca = options.ssl.ca;
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// Inherit from `winston.Transport`.
|
||||
//
|
||||
util.inherits(Webhook, Transport);
|
||||
|
||||
//
|
||||
// Expose the name of this Transport on the prototype
|
||||
//
|
||||
Webhook.prototype.name = 'webhook';
|
||||
|
||||
//
|
||||
// ### function log (level, msg, [meta], callback)
|
||||
// #### @level {string} Level at which to log the message.
|
||||
// #### @msg {string} Message to log
|
||||
// #### @meta {Object} **Optional** Additional metadata to attach
|
||||
// #### @callback {function} Continuation to respond to when complete.
|
||||
// Core logging method exposed to Winston. Metadata is optional.
|
||||
//
|
||||
Webhook.prototype.log = function (level, msg, meta, callback) {
|
||||
if (this.silent) {
|
||||
return callback(null, true);
|
||||
}
|
||||
|
||||
var self = this,
|
||||
message = common.clone(meta),
|
||||
options,
|
||||
req;
|
||||
|
||||
message.level = level;
|
||||
message.message = msg;
|
||||
|
||||
// Prepare options for outgoing HTTP request
|
||||
options = {
|
||||
host: this.host,
|
||||
port: this.port,
|
||||
path: this.path,
|
||||
method: this.method,
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
};
|
||||
|
||||
if (this.ssl) {
|
||||
options.ca = this.ssl.ca;
|
||||
options.key = this.ssl.key;
|
||||
options.cert = this.ssl.cert;
|
||||
}
|
||||
|
||||
if (this.auth) {
|
||||
// Encode `Authorization` header used by Basic Auth
|
||||
options.headers['Authorization'] = 'Basic ' + new Buffer(
|
||||
this.auth.username + ':' + this.auth.password, 'utf8'
|
||||
).toString('base64');
|
||||
}
|
||||
|
||||
// Perform HTTP logging request
|
||||
req = (self.ssl ? https : http).request(options, function (res) {
|
||||
//
|
||||
// No callback on request, fire and forget about the response
|
||||
//
|
||||
self.emit('logged');
|
||||
});
|
||||
|
||||
req.on('error', function (err) {
|
||||
//
|
||||
// Propagate the `error` back up to the `Logger` that this
|
||||
// instance belongs to.
|
||||
//
|
||||
self.emit('error', err);
|
||||
});
|
||||
|
||||
//
|
||||
// Write logging event to the outgoing request body
|
||||
//
|
||||
// jsonMessage is currently conforming to JSON-RPC v1.0,
|
||||
// but without the unique id since there is no anticipated response
|
||||
// see: http://en.wikipedia.org/wiki/JSON-RPC
|
||||
//
|
||||
req.write(JSON.stringify({
|
||||
method: 'log',
|
||||
params: {
|
||||
timestamp: new Date(),
|
||||
msg: msg,
|
||||
level: level,
|
||||
meta: meta
|
||||
}
|
||||
}));
|
||||
|
||||
req.end();
|
||||
|
||||
// Always return true, regardless of any errors
|
||||
callback(null, true);
|
||||
};
|
||||
Reference in New Issue
Block a user