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