[Pkg-javascript-commits] [node-ain2] 62/102: support for 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 48c404e7d17bc5dac50eba571bf23d7797b50830
Author: Patrick Huesler <patrick.huesler at gmail.com>
Date:   Fri Feb 3 17:56:46 2012 +0100

    support for a custom message composer
---
 CHANGELOG.md        |  4 ++++
 index.js            | 10 ++++++++--
 package.json        |  2 +-
 readme.md           | 23 ++++++++++++++++++++++-
 test/integration.js | 37 +++++++++++++++++++------------------
 5 files changed, 54 insertions(+), 22 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3093edf..31d4c16 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,9 @@
 # CHANGELOG
 
+## v1.1.0
+
+* Custom message composer
+
 ## v1.0.0
 
 WARNING: This upgrade is not API compatible to previous version.
diff --git a/index.js b/index.js
index fa6d95b..04ce270 100644
--- a/index.js
+++ b/index.js
@@ -11,7 +11,7 @@ var Transport = {
     UDP: function(message, severity) {
         var client = dgram.createSocket('udp4');
         var self = this;
-        var syslogMessage = this.composeSyslogMessage(message, severity);
+        var syslogMessage = this.composerFunction(message, severity);
         client.send(syslogMessage,
                     0,
                     syslogMessage.length,
@@ -43,7 +43,7 @@ var Transport = {
 
         return function(message, severity) {
             var client = dgram.createSocket('unix_dgram') ;
-            var syslogMessage = this.composeSyslogMessage(message, severity);
+            var syslogMessage = this.composerFunction(message, severity);
             client.send(syslogMessage,
                         0,
                         syslogMessage.length,
@@ -173,6 +173,7 @@ SysLogger.prototype.set = function(config) {
     this.setFacility(config.facility);
     this.setHostname(config.hostname);
     this.setPort(config.port);
+    this.setMessageComposer(config.messageComposer);
     if (config.hostname) {
         this.setTransport(Transport.UDP) ;
     } else {
@@ -216,6 +217,11 @@ SysLogger.prototype.setPort = function(port) {
     return this;
 };
 
+SysLogger.prototype.setMessageComposer = function(composerFunction){
+    this.composerFunction = composerFunction || this.composeSyslogMessage;
+    return this;
+};
+
 /**
  * Send message
  * @param {String} message
diff --git a/package.json b/package.json
index 4f2407e..518f80c 100644
--- a/package.json
+++ b/package.json
@@ -1,7 +1,7 @@
 {
     "name" : "ain2",
     "description" : "Syslog logging for node.js. Continuation of ain",
-    "version" : "1.0.0",
+    "version" : "1.1.0",
     "main" : "./index",
     "author" : "Alexander Dorofeev <aka.spin at gmail.com>",
     "contributors" : [
diff --git a/readme.md b/readme.md
index b212471..d3d8134 100644
--- a/readme.md
+++ b/readme.md
@@ -88,6 +88,7 @@ The `set` function takes one argument, a configuration object which can contain
  * hostname - defaults to require('os').hostname()
  * port - defaults to 514
  * transport - defaults to 'UDP', can also be 'file'
+ * messageComposer - a custom function to compose syslog messages
 
 All of these are optional. If you provide a `hostname` transport is automatically set to UDP
 
@@ -123,7 +124,7 @@ You can set the `facility` by `String` or `Number`:
     logger.set({tag: 'node-test-app', facility: 'daemon'});
     
 Also you can set `TAG`, `Facility`, `HOSTNAME`, `PORT`, and `transport` separately by `setTag`, 
-`setFacility`, `setHostname`, `setPort` and `setTransport` functions. All of them are chainable too.
+`setFacility`, `setHostname`, `setPort`, `setTransport` and `setMessageComposer` functions. All of them are chainable too.
 
 You can get all destinations by these properties:
 
@@ -132,6 +133,26 @@ You can get all destinations by these properties:
 * `hostname` HOSTNAME
 * `port` PORT
 
+## Custom message composer
+
+    var SysLogger = require('ain2');
+    var console = new SysLogger();
+
+    console.setMessageComposer(function(message, severity){
+        return new Buffer('<' + (this.facility * 8 + severity) + '>' +
+                this.getDate() + ' ' + '[' + process.pid + ']:' + message);
+    });
+
+    
+    //The default implementation looks this:
+
+
+    SysLogger.prototype.composeSyslogMessage = function(message, severity) {
+        return new Buffer('<' + (this.facility * 8 + severity) + '>' +
+                this.getDate() + ' ' + this.hostname + ' ' + 
+                this.tag + '[' + process.pid + ']:' + message);
+    }
+
 ## Logging
 
 As noticed before *ain* implements all `console` functions. Severity level is 
diff --git a/test/integration.js b/test/integration.js
index f70c40b..b314cef 100644
--- a/test/integration.js
+++ b/test/integration.js
@@ -12,11 +12,10 @@ function runTests(){
 
     executeDifferentLogCalls(logger);
 
-    logger.composeSyslogMessage = function(message, severity){
+    logger.setMessageComposer(function(message, severity){
         return new Buffer('<' + (this.facility * 8 + severity) + '>' +
                 this.getDate() + ' ' + '[' + process.pid + ']:' + message);
-    };
-
+    });
     executeDifferentLogCalls(logger);
 
     setTimeout(function(){
@@ -26,25 +25,27 @@ function runTests(){
 }
 
 
+function setupServer(){
+    var dgram = require("dgram");
 
+    var server = dgram.createSocket("udp4");
+    var messages = [];
 
-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("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.on("listening", function () {
-  var address = server.address();
-  console.log("server listening " +
-      address.address + ":" + address.port);
-  runTests();
-});
+    server.bind(5514);
+}
 
-server.bind(5514);
+setupServer();
 

-- 
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