logging: basic formatters support
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 31 Dec 2013 11:31:49 +0000 (12:31 +0100)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 31 Dec 2013 11:31:49 +0000 (12:31 +0100)
src/fnpjs/logging/formatters.js [new file with mode: 0644]
src/fnpjs/logging/logging.js

diff --git a/src/fnpjs/logging/formatters.js b/src/fnpjs/logging/formatters.js
new file mode 100644 (file)
index 0000000..5ada6fb
--- /dev/null
@@ -0,0 +1,33 @@
+define(function() {
+
+'use strict';
+
+
+var formatterFromFormatString = function(formatString) {
+    return function(record) {
+        var message = formatString
+                .replace('%level', record.level || '-')
+                .replace('%message', record.originalMessage)
+                .replace('%logger', record.loggerName),
+            currentDate;
+        if(formatString.indexOf('%datetime') !== -1) {
+            currentDate = new Date();
+            message = message.replace('%datetime', + currentDate.getDate() + '-' +
+                            (currentDate.getMonth() + 1)  + '-' +
+                            currentDate.getFullYear() + ' ' +
+                            currentDate.getHours() + ':' +
+                            currentDate.getMinutes() + ':' +
+                            currentDate.getSeconds()
+                            );
+        }
+        return message;
+    };
+};
+
+return {
+    fromFormatString: formatterFromFormatString,
+    simple: formatterFromFormatString('[%level] %datetime (%logger) - %message'),
+    noop: formatterFromFormatString('%message')
+};
+
+});
index 4237f65..9954fad 100644 (file)
@@ -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,22 +28,35 @@ _.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.simple;
+                }
 
                 if(levels.indexOf(handlerLevel) !== -1 && levels.indexOf(level) >= levels.indexOf(handlerLevel)) {
-                    handler({
-                        message: message,
-                        level: level,
-                        loggerName: this.name,
-                        data: data
-                    });
+                    record.message = formatter(record);
+                    handler(record);
                 }
             }.bind(this));
         }