1 define(function(require) {
5 var _ = require('libs/underscore'),
6 handlers = require('fnpjs/logging/handlers'),
7 formatters = require('fnpjs/logging/formatters'),
9 levels = ['debug', 'info', 'warning', 'error', 'critical'];
12 var Logger = function(name) {
14 Object.defineProperty(this, 'config', {
20 }, config.loggers[name] || {});
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',
34 originalMessage: message,
36 loggerName: this.name,
40 if(typeof handler === 'string') {
41 handler = handlers[handlerConfig.handler];
43 if(typeof formatter === 'string') {
44 if(formatter.indexOf('%') !== -1) {
45 formatter = formatters.fromFormatString(formatter);
47 formatter = formatters[handlerConfig.formatter];
51 throw new Error('Unknown handler: ' + handlerName);
54 formatter = formatters.simple;
57 if(levels.indexOf(handlerLevel) !== -1 && levels.indexOf(level) >= levels.indexOf(handlerLevel)) {
58 record.message = formatter(record);
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);
68 exception: function(e) {
69 this.log('error', e.toString(), {exception: e});
73 levels.forEach(function(level) {
74 Logger.prototype[level] = function(message, data) {
75 return this.log(level, message, data);
81 getLogger: function(name) {
82 return new Logger(name);
84 setConfig: function(_config) {
90 clearConfig: function() {