Daniel@0
|
1 "use strict";
|
Daniel@0
|
2
|
Daniel@0
|
3 /*
|
Daniel@0
|
4 * Logging is controlled by Logger library. Each module has got its own named logger,
|
Daniel@0
|
5 * the app has its own named logger too (App.logger)
|
Daniel@0
|
6 */
|
Daniel@0
|
7 App.addInitializer(function(options){
|
Daniel@0
|
8 // Default log level
|
Daniel@0
|
9 Logger.setLevel(
|
Daniel@0
|
10 // Logger.ERROR
|
Daniel@0
|
11 Logger.WARN
|
Daniel@0
|
12 // Logger.INFO
|
Daniel@0
|
13 // Logger.DEBUG
|
Daniel@0
|
14 );
|
Daniel@0
|
15
|
Daniel@0
|
16 // App logger
|
Daniel@0
|
17 App.logger = Logger.get("App");
|
Daniel@0
|
18 App.logger.setLevel(Logger.DEBUG);
|
Daniel@0
|
19
|
Daniel@0
|
20 // Replace the default handle of the Logger to prepend time coordinate and the name of the logger
|
Daniel@0
|
21 // Based on a native setHandler in js-logger (time, time diff and logger name are prepended)
|
Daniel@0
|
22 var previousLogTime = null;
|
Daniel@0
|
23 Logger.setHandler(function (originalArguments, context) {
|
Daniel@0
|
24 var hdlr = console.log;
|
Daniel@0
|
25 var messages = Array.prototype.slice.call(originalArguments);
|
Daniel@0
|
26
|
Daniel@0
|
27 // Prepend the logger's name to the log message for easy identification.
|
Daniel@0
|
28 if (context.name) {
|
Daniel@0
|
29 messages.unshift(_.str.pad(_.str.sprintf("[%s]", context.name), 25, " ", "right"));
|
Daniel@0
|
30 }
|
Daniel@0
|
31 // Prepend time diff
|
Daniel@0
|
32 var logDate = new Date();
|
Daniel@0
|
33 var logTime = logDate.getTime();
|
Daniel@0
|
34 if (previousLogTime && logTime - previousLogTime < 3000) {
|
Daniel@0
|
35 messages.unshift(_.str.sprintf(" %4d", logTime - previousLogTime ));
|
Daniel@0
|
36 } else {
|
Daniel@0
|
37 messages.unshift(" ");
|
Daniel@0
|
38 }
|
Daniel@0
|
39 messages.unshift(_.str.sprintf("%s.%03d", logDate.toTimeString().slice(0, 8),logTime % 1000));
|
Daniel@0
|
40 previousLogTime = logTime;
|
Daniel@0
|
41
|
Daniel@0
|
42 // Delegate through to custom warn/error loggers if present on the console.
|
Daniel@0
|
43 if (context.level === Logger.WARN && console.warn) {
|
Daniel@0
|
44 hdlr = console.warn;
|
Daniel@0
|
45 } else if (context.level === Logger.ERROR && console.error) {
|
Daniel@0
|
46 hdlr = console.error;
|
Daniel@0
|
47 } else if (context.level === Logger.INFO && console.info) {
|
Daniel@0
|
48 hdlr = console.info;
|
Daniel@0
|
49 }
|
Daniel@0
|
50
|
Daniel@0
|
51 // Support for IE8+ (and other, slightly more sane environments)
|
Daniel@0
|
52 Function.prototype.apply.call(hdlr, console, messages);
|
Daniel@0
|
53 });
|
Daniel@0
|
54
|
Daniel@0
|
55 // Log all errors in code (unless jasmine is on)
|
Daniel@0
|
56 if (!options.enableJasmine) {
|
Daniel@0
|
57 window.onerror = function(message, file, line) {
|
Daniel@0
|
58 App.logger.error("[unhandled]", message, file, line);
|
Daniel@0
|
59 };
|
Daniel@0
|
60 }
|
Daniel@0
|
61
|
Daniel@0
|
62 });
|