checkbox for cybernauts + checkbox fix in dialog.js
[fnpeditor.git] / src / fnpjs / logging / logging.js
1 define(function(require) {
2     
3 'use strict';
4
5 var _ = require('libs/underscore'),
6     handlers = require('fnpjs/logging/handlers'),
7     formatters = require('fnpjs/logging/formatters'),
8     config = {},
9     levels = ['debug', 'info', 'warning', 'error', 'critical'];
10
11
12 var Logger = function(name) {
13     this.name = name;
14     Object.defineProperty(this, 'config', {
15         get: function() {
16             return _.extend({
17                 propagate: true,
18                 level: 'warn',
19                 handlers: []
20             }, config.loggers[name] || {});
21         }
22     });
23 };
24
25 _.extend(Logger.prototype, {
26     log: function(level, message, data) {
27         if(levels.indexOf(level) !== -1 && levels.indexOf(level) >= levels.indexOf(this.config.level)) {
28             this.config.handlers.forEach(function(handlerName) {
29                 var handlerConfig = config.handlers[handlerName],
30                     handler = handlerConfig.handler,
31                     formatter = handlerConfig.formatter,
32                     handlerLevel = handlerConfig.level || 'info',
33                     record = {
34                         originalMessage: message,
35                         level: level,
36                         loggerName: this.name,
37                         data: data
38                     };
39
40                 if(typeof handler === 'string') {
41                     handler = handlers[handlerConfig.handler];
42                 }
43                 if(typeof formatter === 'string') {
44                     if(formatter.indexOf('%') !== -1) {
45                         formatter = formatters.fromFormatString(formatter);
46                     } else {
47                         formatter = formatters[handlerConfig.formatter];
48                     }
49                 }
50                 if(!handler) {
51                     throw new Error('Unknown handler: ' + handlerName);
52                 }
53                 if(!formatter) {
54                     formatter = formatters.noop;
55                 }
56
57                 if(levels.indexOf(handlerLevel) !== -1 && levels.indexOf(level) >= levels.indexOf(handlerLevel)) {
58                     record.message = formatter(record);
59                     handler(record);
60                 }
61             }.bind(this));
62         }
63         if(this.config.propagate && this.name) {
64             var logger = new Logger(this.name.split('.').slice(0, -1).join('.'));
65             logger.log(level, message, data);
66         }
67     },
68     exception: function(e) {
69         this.log('error', e.toString(), {exception: e});
70     }
71 });
72
73 levels.forEach(function(level) {
74     Logger.prototype[level] = function(message, data) {
75         return this.log(level, message, data);
76     };
77 });
78
79
80 var api = {
81     getLogger: function(name) {
82         return new Logger(name);
83     },
84     setConfig: function(_config) {
85         config = _.extend({
86             handlers: [],
87             loggers: []
88         } ,_config);
89     },
90     clearConfig: function() {
91         this.setConfig({});
92     }
93 };
94
95 api.clearConfig();
96
97
98 return api;
99
100 });