logger.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // Copyright 2012 United States Government, as represented by the Secretary of Defense, Under
  2. // Secretary of Defense (Personnel & Readiness).
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  5. // in compliance with the License. You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software distributed under the License
  10. // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  11. // or implied. See the License for the specific language governing permissions and limitations under
  12. // the License.
  13. /// @module logger
  14. /// @requires vwf/configuration
  15. define( [ "vwf/configuration" ], function( configuration ) {
  16. var TRACE = 1,
  17. DEBUG = 2,
  18. INFO = 3,
  19. WARN = 4,
  20. ERROR = 5,
  21. FATAL = 6;
  22. var exports = {
  23. levels: {
  24. trace: TRACE,
  25. debug: DEBUG,
  26. info: INFO,
  27. warn: WARN,
  28. error: ERROR,
  29. // fatal: FATAL,
  30. },
  31. label: undefined,
  32. context: undefined,
  33. level: WARN,
  34. for: function( label, context, level ) {
  35. return Object.create( this ).configure( label, context, level );
  36. },
  37. configure: function( label, context, level ) {
  38. var proto = Object.getPrototypeOf( this ) !== Object.prototype ?
  39. Object.getPrototypeOf( this ) : undefined;
  40. this.label = combined_label( proto && proto.label, label );
  41. this.context = context || proto && proto.context;
  42. this.level = { name: "warn", number: WARN }; // default
  43. switch( level ) {
  44. case "trace": case "TRACE": this.level = { name: "trace", number: TRACE }; break;
  45. case "debug": case "DEBUG": this.level = { name: "debug", number: DEBUG }; break;
  46. case "info": case "INFO": this.level = { name: "info", number: INFO }; break;
  47. case "warn": case "WARN": this.level = { name: "warn", number: WARN }; break;
  48. case "error": case "ERROR": this.level = { name: "error", number: ERROR }; break;
  49. // case "fatal": case "FATAL": this.level = { name: "fatal", number: FATAL }; break;
  50. default: proto && delete this.level; break; // inherit from the prototype
  51. }
  52. return this;
  53. },
  54. /// Log a message and open a group if the log threshold is "trace" or below.
  55. traceg: function( /* ... */ ) {
  56. TRACE >= this.level.number &&
  57. log.call( this, arguments, console && console.group, console );
  58. },
  59. /// Log a message and open a group if the log threshold is "debug" or below.
  60. debugg: function( /* ... */ ) {
  61. DEBUG >= this.level.number &&
  62. log.call( this, arguments, console && console.group, console );
  63. },
  64. /// Log a message and open a group if the log threshold is "info" or below.
  65. infog: function( /* ... */ ) {
  66. INFO >= this.level.number &&
  67. log.call( this, arguments, console && console.group, console );
  68. },
  69. /// Close a group if the log threshold is "trace" or below.
  70. traceu: function() {
  71. TRACE >= this.level.number &&
  72. log.call( this, arguments, console && console.groupEnd, console );
  73. },
  74. /// Close a group if the log threshold is "debug" or below.
  75. debugu: function() {
  76. DEBUG >= this.level.number &&
  77. log.call( this, arguments, console && console.groupEnd, console );
  78. },
  79. /// Close a group if the log threshold is "info" or below.
  80. infou: function() {
  81. INFO >= this.level.number &&
  82. log.call( this, arguments, console && console.groupEnd, console );
  83. },
  84. /// Log a message if the log threshold is "trace" or below.
  85. trace: function( /* ... */ ) {
  86. TRACE >= this.level.number &&
  87. log.call( this, arguments, console && console.debug, console ); // not console.trace(), which would log the stack
  88. },
  89. /// Log a message if the log threshold is "debug" or below.
  90. debug: function( /* ... */ ) {
  91. DEBUG >= this.level.number &&
  92. log.call( this, arguments, console && console.debug, console );
  93. },
  94. /// Log a message if the log threshold is "info" or below.
  95. info: function( /* ... */ ) {
  96. INFO >= this.level.number &&
  97. log.call( this, arguments, console && console.info, console );
  98. },
  99. /// Log a message if the log threshold is "warn" or below.
  100. warn: function( /* ... */ ) {
  101. WARN >= this.level.number &&
  102. log.call( this, arguments, console && console.warn, console );
  103. },
  104. /// Log a message if the log threshold is "error" or below.
  105. error: function( /* ... */ ) {
  106. ERROR >= this.level.number &&
  107. log.call( this, arguments, console && console.error, console );
  108. },
  109. /// Log a message.
  110. log: function( /* ... */ ) {
  111. log.call( this, arguments, console && console.log, console );
  112. },
  113. // Log with an extra one-time label. Equivalent to this.logger.for( label ).log( ... ),
  114. // etc., but without the overhead of creating a new logger.
  115. /// Log a message with an extra one-time label and open a group if the log threshold is
  116. /// "trace" or below.
  117. tracegx: function( /* label, ... */ ) {
  118. TRACE >= this.level.number &&
  119. log.call( this, arguments, console && console.group, console, true );
  120. },
  121. /// Log a message with an extra one-time label and open a group if the log threshold is
  122. /// "debug" or below.
  123. debuggx: function( /* label, ... */ ) {
  124. DEBUG >= this.level.number &&
  125. log.call( this, arguments, console && console.group, console, true );
  126. },
  127. /// Log a message with an extra one-time label and open a group if the log threshold is
  128. /// "info" or below.
  129. infogx: function( /* label, ... */ ) {
  130. INFO >= this.level.number &&
  131. log.call( this, arguments, console && console.group, console, true );
  132. },
  133. /// Log a message with an extra one-time label if the log threshold is "trace" or below.
  134. tracex: function( /* ... */ ) {
  135. TRACE >= this.level.number &&
  136. log.call( this, arguments, console && console.debug, console, true ); // not console.trace(), which would log the stack
  137. },
  138. /// Log a message with an extra one-time label if the log threshold is "debug" or below.
  139. debugx: function( /* label, ... */ ) {
  140. DEBUG >= this.level.number &&
  141. log.call( this, arguments, console && console.debug, console, true );
  142. },
  143. /// Log a message with an extra one-time label if the log threshold is "info" or below.
  144. infox: function( /* label, ... */ ) {
  145. INFO >= this.level.number &&
  146. log.call( this, arguments, console && console.info, console, true );
  147. },
  148. /// Log a message with an extra one-time label if the log threshold is "warn" or below.
  149. warnx: function( /* label, ... */ ) {
  150. WARN >= this.level.number &&
  151. log.call( this, arguments, console && console.warn, console, true );
  152. },
  153. /// Log a message with an extra one-time label if the log threshold is "error" or below.
  154. errorx: function( /* label, ... */ ) {
  155. ERROR >= this.level.number &&
  156. log.call( this, arguments, console && console.warn, console, true );
  157. },
  158. /// Log a message with an extra one-time label.
  159. logx: function( /* label, ... */ ) {
  160. log.call( this, arguments, console && console.log, console, true );
  161. },
  162. };
  163. /// Log a message to the console. Normalize the arguments list and invoke the appender function.
  164. ///
  165. /// @param {Array} args
  166. /// An Array-like list of arguments passed to a log function. normalize describes the formats
  167. /// supported.
  168. /// @param {Function} appender
  169. /// A Firebug-like log function that logs its arguments, such as window.console.log.
  170. /// @param {Object} context
  171. /// The *this* object for the appender, such as window.console.
  172. /// @param {Boolean} [extra]
  173. /// If true, interpret args[0] as a one-time label that extends the logger's output prefix.
  174. function log( args, appender, context, extra ) { // invoke with *this* as the logger module
  175. // Normalize the arguments and log the message. Don't log a message if normalize() returned
  176. // undefined (because a generator function didn't return a result).
  177. if ( args = /* assignment! */ normalize.call( this, args, extra ) ) {
  178. appender && appender.apply( context, args );
  179. }
  180. }
  181. /// Normalize the arguments provided to a log function. The arguments may take one of the
  182. /// following forms:
  183. ///
  184. /// A series of values, or a function that generates the values:
  185. ///
  186. /// [ value, value, ... ]
  187. /// [ function( name, number ) { return [ value, value, ... ] }, context ]
  188. ///
  189. /// For a generator function, an optional context argument provides the function's *this*
  190. /// context. The logger's default context will be used if the context argument is no provided.
  191. /// The log threshold name and number ("info" and 3, for example) are passed as arguments to the
  192. /// function.
  193. ///
  194. /// No message will be logged if a generator function returns undefined. Since the function will
  195. /// only be called if the message type exceeds the log threshold, logger calls may be used to
  196. /// control program behavior based on the log level:
  197. ///
  198. /// this.logger.debug( function( name, number ) {
  199. /// debugControls.visible = true; // returns undefined, so no message
  200. /// } );
  201. ///
  202. /// When *extra* is truthy, the first argument is interpreted as a one-time label that extends
  203. /// the logger's output prefix:
  204. ///
  205. /// [ "label", value, value, ... ]
  206. /// [ "label", function( name, number ) { return [ value, value, ... ] }, context ]
  207. ///
  208. /// The arguments are normalized into a list of values ready to pass to the appender:
  209. ///
  210. /// [ value, value, ... ]
  211. ///
  212. /// @param {Array} args
  213. /// An Array-like list of arguments passed to one of the log functions.
  214. /// @param {Boolean} [extra]
  215. /// If true, interpret args[0] as a one-time label that extends the logger's output prefix.
  216. function normalize( args, extra ) { // invoke with *this* as the logger module
  217. // Record the extra one-time label if one is provided. We leave it in the arguments list so
  218. // that we don't convert Arguments to an Array if it isn't necessary.
  219. if ( extra && ( typeof args[0] == "string" || args[0] instanceof String ) ) {
  220. var label = args[0];
  221. var start = 1;
  222. } else {
  223. var label = undefined;
  224. var start = 0;
  225. }
  226. // If a generator function is provided (instead of a series of values), call it to get the
  227. // arguments list.
  228. if ( typeof args[ start ] == "function" || args[ start ] instanceof Function ) {
  229. // Call the function using the provided context or this logger's context. We expect the
  230. // function to return an array of values, a single value, or undefined.
  231. args = args[ start ].call( args[ start+1 ] || this.context, this.level.name, this.level.number );
  232. // Convert a single value to an Array. An Array remains an Array. Leave undefined
  233. // unchanged.
  234. if ( args !== undefined && ( typeof args != "object" || ! ( args instanceof Array ) ) ) {
  235. args = [ args ];
  236. }
  237. } else {
  238. // Remove the extra one-time label.
  239. if ( start > 0 ) {
  240. args = Array.prototype.slice.call( args, start );
  241. }
  242. }
  243. // Add the prefix label to the arguments list and return. But return undefined if a
  244. // generator didn't return a result.
  245. return args ? prefixed_arguments( this.label, label, args ) : undefined;
  246. }
  247. /// Update an arguments list to prepend "<label>: " to the output.
  248. function prefixed_arguments( label, extra, args ) {
  249. if ( label || extra ) {
  250. if ( args.length == 0 ) {
  251. return [ combined_label( label, extra ) ]; // just show the module and function name when there are no additional arguments
  252. } else if ( typeof args[0] == "string" || args[0] instanceof String ) {
  253. return [ combined_label( label, extra ) + ": " + args[0] ].concat( Array.prototype.slice.call( args, 1 ) ); // concatenate when the first field is a string so that it may remain a format string
  254. } else {
  255. return [ combined_label( label, extra ) + ":" ].concat( args ); // otherwise insert a new first field
  256. }
  257. } else {
  258. return args;
  259. }
  260. }
  261. /// Generate a new label from a parent label and an extra part.
  262. function combined_label( label, extra ) {
  263. // Combine with "." unless the extension provides its own separator.
  264. var separator = extra && extra.match( /^[0-9A-Za-z]/ ) ? "." : "";
  265. // Concatenate and return.
  266. if ( label && extra ) {
  267. return label + separator + extra;
  268. } else if ( extra ) {
  269. return extra;
  270. } else if ( label ) {
  271. return label;
  272. } else {
  273. return undefined;
  274. }
  275. }
  276. // Get the initial level setting from the configuration module, and update the level when the
  277. // configuration changes.
  278. // TODO: should be done somewhere else; the logger isn't bound to VWF and shouldn't have a dependency on the configuration module.
  279. exports.configure( undefined, undefined, configuration.active["log-level"] || "warn" );
  280. configuration.changed( function( active ) {
  281. exports.configure( undefined, undefined, active["log-level"] || "warn" );
  282. }, this );
  283. // Return the module.
  284. return exports;
  285. } );