2011-08-04 20:32:01 +00:00
|
|
|
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///--- Globals
|
|
|
|
|
|
|
|
var FMT_STR = '%d-%s-%s %s:%s:%sZ %s - %s: ';
|
|
|
|
|
|
|
|
var _i = 0;
|
|
|
|
var LEVELS = {
|
|
|
|
Trace: _i++,
|
|
|
|
Debug: _i++,
|
|
|
|
Info: _i++,
|
|
|
|
Warn: _i++,
|
|
|
|
Error: _i++,
|
|
|
|
Fatal: _i++
|
|
|
|
};
|
|
|
|
|
|
|
|
var level = 'Info';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// --- Helpers
|
|
|
|
|
|
|
|
function pad(val) {
|
|
|
|
if (parseInt(val, 10) < 10) {
|
|
|
|
val = '0' + val;
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function format(level, name, args) {
|
|
|
|
var d = new Date();
|
|
|
|
var fmtStr = args.shift();
|
|
|
|
var fmtArgs = [
|
|
|
|
d.getUTCFullYear(),
|
2011-08-31 04:04:43 +00:00
|
|
|
pad(d.getUTCMonth() + 1),
|
2011-08-04 20:32:01 +00:00
|
|
|
pad(d.getUTCDate()),
|
|
|
|
pad(d.getUTCHours()),
|
|
|
|
pad(d.getUTCMinutes()),
|
|
|
|
pad(d.getUTCSeconds()),
|
|
|
|
level,
|
|
|
|
name
|
|
|
|
];
|
|
|
|
|
|
|
|
args = fmtArgs.concat(args);
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
var output = (FMT_STR + fmtStr).replace(/%[sdj]/g, function (match) {
|
2011-08-04 20:32:01 +00:00
|
|
|
switch (match) {
|
|
|
|
case '%s': return new String(args.shift());
|
|
|
|
case '%d': return new Number(args.shift());
|
|
|
|
case '%j': return JSON.stringify(args.shift());
|
|
|
|
default:
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///--- API
|
|
|
|
|
|
|
|
function Log(name) {
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Log.prototype._write = function (level, args) {
|
2011-08-04 20:32:01 +00:00
|
|
|
var data = format(level, this.name, args);
|
|
|
|
console.error(data);
|
|
|
|
};
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Log.prototype.isTraceEnabled = function () {
|
2011-08-04 20:32:01 +00:00
|
|
|
return (LEVELS.Trace >= LEVELS[level]);
|
|
|
|
};
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Log.prototype.trace = function () {
|
2011-08-04 20:32:01 +00:00
|
|
|
if (this.isTraceEnabled())
|
|
|
|
this._write('TRACE', Array.prototype.slice.call(arguments));
|
|
|
|
};
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Log.prototype.isDebugEnabled = function () {
|
2011-08-04 20:32:01 +00:00
|
|
|
return (LEVELS.Debug >= LEVELS[level]);
|
|
|
|
};
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Log.prototype.debug = function () {
|
2011-08-04 20:32:01 +00:00
|
|
|
if (this.isDebugEnabled())
|
|
|
|
this._write('DEBUG', Array.prototype.slice.call(arguments));
|
|
|
|
};
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Log.prototype.isInfoEnabled = function () {
|
2011-08-04 20:32:01 +00:00
|
|
|
return (LEVELS.Info >= LEVELS[level]);
|
|
|
|
};
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Log.prototype.info = function () {
|
2011-08-04 20:32:01 +00:00
|
|
|
if (this.isInfoEnabled())
|
|
|
|
this._write('INFO', Array.prototype.slice.call(arguments));
|
|
|
|
};
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Log.prototype.isWarnEnabled = function () {
|
2011-08-04 20:32:01 +00:00
|
|
|
return (LEVELS.Warn >= LEVELS[level]);
|
|
|
|
};
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Log.prototype.warn = function () {
|
2011-08-04 20:32:01 +00:00
|
|
|
if (this.isWarnEnabled())
|
|
|
|
this._write('WARN', Array.prototype.slice.call(arguments));
|
|
|
|
};
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Log.prototype.isErrorEnabled = function () {
|
2011-08-04 20:32:01 +00:00
|
|
|
return (LEVELS.Error >= LEVELS[level]);
|
|
|
|
};
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Log.prototype.error = function () {
|
2011-08-04 20:32:01 +00:00
|
|
|
if (this.isErrorEnabled())
|
|
|
|
this._write('ERROR', Array.prototype.slice.call(arguments));
|
|
|
|
};
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Log.prototype.isFatalEnabled = function () {
|
2011-08-04 20:32:01 +00:00
|
|
|
return (LEVELS.Fatal >= LEVELS[level]);
|
|
|
|
};
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Log.prototype.fatal = function () {
|
2011-08-04 20:32:01 +00:00
|
|
|
if (this.isFatalEnabled())
|
|
|
|
this._write('FATAL', Array.prototype.slice.call(arguments));
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
setLevel: function (l) {
|
2011-11-22 21:36:08 +00:00
|
|
|
l = l.charAt(0).toUpperCase() + l.slice(1).toLowerCase();
|
2011-08-04 20:32:01 +00:00
|
|
|
if (LEVELS[l] !== undefined)
|
|
|
|
level = l;
|
|
|
|
|
|
|
|
return level;
|
|
|
|
},
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
getLogger: function (name) {
|
|
|
|
if (!name || typeof (name) !== 'string')
|
2011-08-04 20:32:01 +00:00
|
|
|
throw new TypeError('name (string) required');
|
|
|
|
|
|
|
|
return new Log(name);
|
2011-11-22 21:36:08 +00:00
|
|
|
},
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
setGlobalLogLevel: function (l) {
|
2011-11-22 21:36:08 +00:00
|
|
|
l = l.charAt(0).toUpperCase() + l.slice(1).toLowerCase();
|
|
|
|
if (LEVELS[l] !== undefined)
|
|
|
|
level = l;
|
|
|
|
|
|
|
|
return level;
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|