[Pkg-javascript-commits] [node-ain2] 61/102: allow to specify a custom message composer

Jonas Smedegaard js at moszumanska.debian.org
Tue Apr 29 11:59:50 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository node-ain2.

commit efceb2dd7b44b7a61e0832e5cac7462b6370d987
Author: Patrick Huesler <patrick.huesler at gmail.com>
Date:   Fri Feb 3 17:05:27 2012 +0100

    allow to specify a custom message composer
---
 index.js            | 82 ++++++++++++++++++++++++++++-------------------------
 test/integration.js | 50 ++++++++++++++++++++++++++++++++
 test/testServer.js  | 15 ----------
 3 files changed, 93 insertions(+), 54 deletions(-)

diff --git a/index.js b/index.js
index e507928..fa6d95b 100644
--- a/index.js
+++ b/index.js
@@ -6,45 +6,15 @@ var DefaultHostname = require("os").hostname();
 var DefaultAddress = "127.0.0.1";
 var SingletonInstance = null;
 
-function leadZero(n) {
-    if (n < 10) {
-        return '0' + n;
-    } else {
-        return n;
-    }
-}
-
-/**
- * Get current date in syslog format. Thanks https://github.com/kordless/lodge
- * @returns {String}
- */
-function getDate() {
-    var dt = new Date();
-    var hours = leadZero(dt.getHours());
-    var minutes = leadZero(dt.getMinutes());
-    var seconds = leadZero(dt.getSeconds());
-    var month = dt.getMonth();
-    var day = dt.getDate();
-    if(day < 10){
-      day = ' ' + day;
-    }
-    var months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
-            'Sep', 'Oct', 'Nov', 'Dec' ];
-    return months[month] + " " + day + " " + hours + ":" + minutes + ":" + seconds;
-}
 
 var Transport = {
     UDP: function(message, severity) {
         var client = dgram.createSocket('udp4');
         var self = this;
-
-        message = new Buffer('<' + (this.facility * 8 + severity) + '>' +
-            getDate() + ' ' + this.hostname + ' ' + 
-            this.tag + '[' + process.pid + ']:' + message);
-
-        client.send(message,
+        var syslogMessage = this.composeSyslogMessage(message, severity);
+        client.send(syslogMessage,
                     0,
-                    message.length,
+                    syslogMessage.length,
                     this.port,
                     this.address,
                     function(err, bytes) {
@@ -73,13 +43,10 @@ var Transport = {
 
         return function(message, severity) {
             var client = dgram.createSocket('unix_dgram') ;
-            message = new Buffer('<' + (this.facility * 8 + severity) + '>' +
-                getDate() + ' ' + this.hostname + ' ' + 
-                this.tag + '[' + process.pid + ']:' + message);
-
-            client.send(message,
+            var syslogMessage = this.composeSyslogMessage(message, severity);
+            client.send(syslogMessage,
                         0,
-                        message.length,
+                        syslogMessage.length,
                         logTarget,
                         this._logError
             );
@@ -302,6 +269,16 @@ SysLogger.prototype.debug = function() {
     this._send(format.apply(this, arguments), Severity.debug);
 };
 
+
+/**
+ * Compose syslog message
+ */
+SysLogger.prototype.composeSyslogMessage = function(message, severity) {
+    return new Buffer('<' + (this.facility * 8 + severity) + '>' +
+            this.getDate() + ' ' + this.hostname + ' ' + 
+            this.tag + '[' + process.pid + ']:' + message);
+}
+
 /**
  * Log object with `util.inspect` with notice severity
  */
@@ -333,4 +310,31 @@ SysLogger.prototype.assert = function(expression) {
     }
 };
 
+/**
+ * Get current date in syslog format. Thanks https://github.com/kordless/lodge
+ * @returns {String}
+ */
+SysLogger.prototype.getDate = function() {
+    var dt = new Date();
+    var hours = this.leadZero(dt.getHours());
+    var minutes = this.leadZero(dt.getMinutes());
+    var seconds = this.leadZero(dt.getSeconds());
+    var month = dt.getMonth();
+    var day = dt.getDate();
+    if(day < 10){
+      day = ' ' + day;
+    }
+    var months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
+            'Sep', 'Oct', 'Nov', 'Dec' ];
+    return months[month] + " " + day + " " + hours + ":" + minutes + ":" + seconds;
+}
+
+SysLogger.prototype.leadZero = function(n) {
+    if (n < 10) {
+        return '0' + n;
+    } else {
+        return n;
+    }
+}
+
 module.exports = SysLogger;
diff --git a/test/integration.js b/test/integration.js
new file mode 100644
index 0000000..f70c40b
--- /dev/null
+++ b/test/integration.js
@@ -0,0 +1,50 @@
+function executeDifferentLogCalls(logger){
+    logger.log('log');
+    logger.info('info');
+    logger.warn('warn');
+    logger.error('error');
+    logger.debug('debug');
+}
+
+function runTests(){
+    var Syslog = require('../index.js');
+    var logger = new Syslog({port : 5514});
+
+    executeDifferentLogCalls(logger);
+
+    logger.composeSyslogMessage = function(message, severity){
+        return new Buffer('<' + (this.facility * 8 + severity) + '>' +
+                this.getDate() + ' ' + '[' + process.pid + ']:' + message);
+    };
+
+    executeDifferentLogCalls(logger);
+
+    setTimeout(function(){
+        process.exit();
+    }, 500);
+
+}
+
+
+
+
+var dgram = require("dgram");
+
+var server = dgram.createSocket("udp4");
+var messages = [];
+
+
+server.on("message", function (msg, rinfo) {
+  console.log(msg.toString());
+  messages.push(msg.toString());
+});
+
+server.on("listening", function () {
+  var address = server.address();
+  console.log("server listening " +
+      address.address + ":" + address.port);
+  runTests();
+});
+
+server.bind(5514);
+
diff --git a/test/testServer.js b/test/testServer.js
deleted file mode 100644
index eea859e..0000000
--- a/test/testServer.js
+++ /dev/null
@@ -1,15 +0,0 @@
-var dgram = require("dgram");
-
-var server = dgram.createSocket("udp4");
-
-server.on("message", function (msg, rinfo) {
-  console.log(msg.toString());
-});
-
-server.on("listening", function () {
-  var address = server.address();
-  console.log("server listening " +
-      address.address + ":" + address.port);
-});
-
-server.bind(5514);

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-ain2.git



More information about the Pkg-javascript-commits mailing list