logging: basic formatters support
[fnpeditor.git] / src / fnpjs / logging / formatters.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')
+};
+
+});