cylon/lib/logger.js

132 lines
3.5 KiB
JavaScript
Raw Normal View History

2013-10-25 05:25:42 +08:00
/*
* logger
* cylonjs.com
*
* Copyright (c) 2013 The Hybrid Group
* Licensed under the Apache 2.0 license.
*/
2014-02-28 04:39:31 +08:00
"use strict";
var getArgs = function(args) {
return args.length >= 1 ? [].slice.call(args, 0) : [];
}
// The Logger is a global object to facilitate logging stuff to the console (or
// other output) easily and consistently. It's available anywhere in Cylon, as
// well as in external modules that are loaded into Cylon
global.Logger = {
// Public: Creates a Logger instance and assigns it to @logger
//
// logger - logger object to use. Defaults to a BasicLogger, or a NullLogger if
// false is supplied
//
// Returns the new logger instance
setup: function(logger) {
if (logger == null) { logger = new BasicLogger; }
if (logger === false) {
this.logger = new NullLogger();
} else {
this.logger = logger;
2013-10-25 05:25:42 +08:00
}
2014-02-28 04:39:31 +08:00
return this.logger
},
toString: function() { return this.logger.toString(); },
debug: function() {
var args = getArgs(arguments);
return this.logger.debug.apply(this.logger, args);
},
info: function() {
var args = getArgs(arguments);
return this.logger.info.apply(this.logger, args);
},
warn: function() {
var args = getArgs(arguments);
return this.logger.warn.apply(this.logger, args);
},
error: function() {
var args = getArgs(arguments);
return this.logger.error.apply(this.logger, args);
},
2013-10-25 05:25:42 +08:00
2014-02-28 04:39:31 +08:00
fatal: function() {
var args = getArgs(arguments);
return this.logger.fatal.apply(this.logger, args);
}
};
2013-10-25 05:25:42 +08:00
2014-02-28 04:39:31 +08:00
// The BasicLogger pushes stuff to console.log. Nothing more, nothing less.
var BasicLogger = (function() {
function BasicLogger() {}
2013-10-25 05:25:42 +08:00
2014-02-28 04:39:31 +08:00
BasicLogger.prototype.toString = function() { return "BasicLogger"; };
2013-10-25 05:25:42 +08:00
2014-02-28 04:39:31 +08:00
BasicLogger.prototype.debug = function() {
var args = getArgs(arguments),
string = ["D, [" + (new Date().toISOString()) + "] DEBUG -- :"],
data = string.concat(args.slice());
2013-10-25 05:25:42 +08:00
2014-02-28 04:39:31 +08:00
return console.log.apply(console, data);
};
2013-10-25 05:25:42 +08:00
2014-02-28 04:39:31 +08:00
BasicLogger.prototype.info = function() {
var args = getArgs(arguments),
string = ["I, [" + (new Date().toISOString()) + "] INFO -- :"],
data = string.concat(args.slice());
2013-10-25 05:25:42 +08:00
2014-02-28 04:39:31 +08:00
return console.log.apply(console, data);
};
2013-10-25 05:25:42 +08:00
2014-02-28 04:39:31 +08:00
BasicLogger.prototype.warn = function() {
var args = getArgs(arguments),
string = ["W, [" + (new Date().toISOString()) + "] WARN -- :"],
data = string.concat(args.slice());
2013-10-25 05:25:42 +08:00
2014-02-28 04:39:31 +08:00
return console.log.apply(console, data);
};
2013-10-25 05:25:42 +08:00
2014-02-28 04:39:31 +08:00
BasicLogger.prototype.error = function() {
var args = getArgs(arguments),
string = ["E, [" + (new Date().toISOString()) + "] ERROR -- :"],
data = string.concat(args.slice());
2013-11-05 03:41:51 +08:00
2014-02-28 04:39:31 +08:00
return console.log.apply(console, data);
};
BasicLogger.prototype.fatal = function() {
var args = getArgs(arguments),
string = ["F, [" + (new Date().toISOString()) + "] FATAL -- :"],
data = string.concat(args.slice());
return console.log.apply(console, data);
};
2013-10-25 05:25:42 +08:00
2014-02-28 04:39:31 +08:00
return BasicLogger;
2013-10-25 05:25:42 +08:00
2014-02-28 04:39:31 +08:00
})();
2013-10-25 05:25:42 +08:00
2014-02-28 04:39:31 +08:00
// The NullLogger is designed for cases where you want absolutely nothing to
// print to anywhere. Every proxied method from the Logger returns a noop.
var NullLogger = (function() {
function NullLogger() {}
2013-10-25 05:25:42 +08:00
2014-02-28 04:39:31 +08:00
NullLogger.prototype.toString = function() { return "NullLogger"; };
2013-10-25 05:25:42 +08:00
2014-02-28 04:39:31 +08:00
NullLogger.prototype.debug = function() {};
NullLogger.prototype.info = function() {};
NullLogger.prototype.warn = function() {};
NullLogger.prototype.error = function() {};
NullLogger.prototype.fatal = function() {};
2013-10-25 05:25:42 +08:00
2014-02-28 04:39:31 +08:00
return NullLogger;
2013-10-25 05:25:42 +08:00
2014-02-28 04:39:31 +08:00
})();