X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/a4e92f85f83046cef29d4ce0f71cea6250ce8df6..d6011e09fc76ad45e40d2930d0af160797fc9daa:/src/fnpjs/logging/logging.js?ds=inline diff --git a/src/fnpjs/logging/logging.js b/src/fnpjs/logging/logging.js index e68302d..23dbb24 100644 --- a/src/fnpjs/logging/logging.js +++ b/src/fnpjs/logging/logging.js @@ -4,6 +4,7 @@ define(function(require) { var _ = require('libs/underscore'), handlers = require('fnpjs/logging/handlers'), + formatters = require('fnpjs/logging/formatters'), config = {}, levels = ['debug', 'info', 'warning', 'error', 'critical']; @@ -27,19 +28,37 @@ _.extend(Logger.prototype, { this.config.handlers.forEach(function(handlerName) { var handlerConfig = config.handlers[handlerName], handler = handlerConfig.handler, - handlerLevel = handlerConfig.level || 'info'; + formatter = handlerConfig.formatter, + handlerLevel = handlerConfig.level || 'info', + record = { + originalMessage: message, + level: level, + loggerName: this.name, + data: data + }; if(typeof handler === 'string') { handler = handlers[handlerConfig.handler]; } + if(typeof formatter === 'string') { + if(formatter.indexOf('%') !== -1) { + formatter = formatters.fromFormatString(formatter); + } else { + formatter = formatters[handlerConfig.formatter]; + } + } if(!handler) { throw new Error('Unknown handler: ' + handlerName); } + if(!formatter) { + formatter = formatters.noop; + } if(levels.indexOf(handlerLevel) !== -1 && levels.indexOf(level) >= levels.indexOf(handlerLevel)) { - handler(message, level, data); + record.message = formatter(record); + handler(record); } - }); + }.bind(this)); } if(this.config.propagate && this.name) { var logger = new Logger(this.name.split('.').slice(0, -1).join('.'));