comparison src/DML/VendorAssetsBundle/Resources/assets/js-logger/0.9.14/js-logger.js @ 0:493bcb69166c

added public content
author Daniel Wolff
date Tue, 09 Feb 2016 20:54:02 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:493bcb69166c
1 /*!
2 * js-logger - http://github.com/jonnyreeves/js-logger
3 * Jonny Reeves, http://jonnyreeves.co.uk/
4 * js-logger may be freely distributed under the MIT license.
5 */
6
7 /*jshint sub:true*/
8 /*global console:true,define:true, module:true*/
9 (function (global) {
10 "use strict";
11
12 // Top level module for the global, static logger instance.
13 var Logger = { };
14
15 // For those that are at home that are keeping score.
16 Logger.VERSION = "0.9.14";
17
18 // Function which handles all incoming log messages.
19 var logHandler;
20
21 // Map of ContextualLogger instances by name; used by Logger.get() to return the same named instance.
22 var contextualLoggersByNameMap = {};
23
24 // Polyfill for ES5's Function.bind.
25 var bind = function(scope, func) {
26 return function() {
27 return func.apply(scope, arguments);
28 };
29 };
30
31 // Super exciting object merger-matron 9000 adding another 100 bytes to your download.
32 var merge = function () {
33 var args = arguments, target = args[0], key, i;
34 for (i = 1; i < args.length; i++) {
35 for (key in args[i]) {
36 if (!(key in target) && args[i].hasOwnProperty(key)) {
37 target[key] = args[i][key];
38 }
39 }
40 }
41 return target;
42 };
43
44 // Helper to define a logging level object; helps with optimisation.
45 var defineLogLevel = function(value, name) {
46 return { value: value, name: name };
47 };
48
49 // Predefined logging levels.
50 Logger.DEBUG = defineLogLevel(1, 'DEBUG');
51 Logger.INFO = defineLogLevel(2, 'INFO');
52 Logger.WARN = defineLogLevel(4, 'WARN');
53 Logger.ERROR = defineLogLevel(8, 'ERROR');
54 Logger.OFF = defineLogLevel(99, 'OFF');
55
56 // Inner class which performs the bulk of the work; ContextualLogger instances can be configured independently
57 // of each other.
58 var ContextualLogger = function(defaultContext) {
59 this.context = defaultContext;
60 this.setLevel(defaultContext.filterLevel);
61 this.log = this.info; // Convenience alias.
62 };
63
64 ContextualLogger.prototype = {
65 // Changes the current logging level for the logging instance.
66 setLevel: function (newLevel) {
67 // Ensure the supplied Level object looks valid.
68 if (newLevel && "value" in newLevel) {
69 this.context.filterLevel = newLevel;
70 }
71 },
72
73 // Is the logger configured to output messages at the supplied level?
74 enabledFor: function (lvl) {
75 var filterLevel = this.context.filterLevel;
76 return lvl.value >= filterLevel.value;
77 },
78
79 debug: function () {
80 this.invoke(Logger.DEBUG, arguments);
81 },
82
83 info: function () {
84 this.invoke(Logger.INFO, arguments);
85 },
86
87 warn: function () {
88 this.invoke(Logger.WARN, arguments);
89 },
90
91 error: function () {
92 this.invoke(Logger.ERROR, arguments);
93 },
94
95 // Invokes the logger callback if it's not being filtered.
96 invoke: function (level, msgArgs) {
97 if (logHandler && this.enabledFor(level)) {
98 logHandler(msgArgs, merge({ level: level }, this.context));
99 }
100 }
101 };
102
103 // Protected instance which all calls to the to level `Logger` module will be routed through.
104 var globalLogger = new ContextualLogger({ filterLevel: Logger.OFF });
105
106 // Configure the global Logger instance.
107 (function() {
108 // Shortcut for optimisers.
109 var L = Logger;
110
111 L.enabledFor = bind(globalLogger, globalLogger.enabledFor);
112 L.debug = bind(globalLogger, globalLogger.debug);
113 L.info = bind(globalLogger, globalLogger.info);
114 L.warn = bind(globalLogger, globalLogger.warn);
115 L.error = bind(globalLogger, globalLogger.error);
116
117 // Don't forget the convenience alias!
118 L.log = L.info;
119 }());
120
121 // Set the global logging handler. The supplied function should expect two arguments, the first being an arguments
122 // object with the supplied log messages and the second being a context object which contains a hash of stateful
123 // parameters which the logging function can consume.
124 Logger.setHandler = function (func) {
125 logHandler = func;
126 };
127
128 // Sets the global logging filter level which applies to *all* previously registered, and future Logger instances.
129 // (note that named loggers (retrieved via `Logger.get`) can be configured independently if required).
130 Logger.setLevel = function(level) {
131 // Set the globalLogger's level.
132 globalLogger.setLevel(level);
133
134 // Apply this level to all registered contextual loggers.
135 for (var key in contextualLoggersByNameMap) {
136 if (contextualLoggersByNameMap.hasOwnProperty(key)) {
137 contextualLoggersByNameMap[key].setLevel(level);
138 }
139 }
140 };
141
142 // Retrieve a ContextualLogger instance. Note that named loggers automatically inherit the global logger's level,
143 // default context and log handler.
144 Logger.get = function (name) {
145 // All logger instances are cached so they can be configured ahead of use.
146 return contextualLoggersByNameMap[name] ||
147 (contextualLoggersByNameMap[name] = new ContextualLogger(merge({ name: name }, globalLogger.context)));
148 };
149
150 // Configure and example a Default implementation which writes to the `window.console` (if present).
151 Logger.useDefaults = function(defaultLevel) {
152 // Check for the presence of a logger.
153 if (typeof console === "undefined") {
154 return;
155 }
156
157 Logger.setLevel(defaultLevel || Logger.DEBUG);
158 Logger.setHandler(function(messages, context) {
159 var hdlr = console.log;
160
161 // Prepend the logger's name to the log message for easy identification.
162 if (context.name) {
163 messages[0] = "[" + context.name + "] " + messages[0];
164 }
165
166 // Delegate through to custom warn/error loggers if present on the console.
167 if (context.level === Logger.WARN && console.warn) {
168 hdlr = console.warn;
169 } else if (context.level === Logger.ERROR && console.error) {
170 hdlr = console.error;
171 } else if (context.level === Logger.INFO && console.info) {
172 hdlr = console.info;
173 }
174
175 // Support for IE8+ (and other, slightly more sane environments)
176 Function.prototype.apply.call(hdlr, console, messages);
177 });
178 };
179
180 // Export to popular environments boilerplate.
181 if (typeof define === 'function' && define.amd) {
182 define(Logger);
183 }
184 else if (typeof module !== 'undefined' && module.exports) {
185 module.exports = Logger;
186 }
187 else {
188 Logger._prevLogger = global.Logger;
189
190 Logger.noConflict = function () {
191 global.Logger = Logger._prevLogger;
192 return Logger;
193 };
194
195 global.Logger = Logger;
196 }
197 }(this));