[Pkg-javascript-commits] [node-ain2] 35/102: support logging via /dev/log (Linux) or /var/run/syslog (Darwin)

Jonas Smedegaard js at moszumanska.debian.org
Tue Apr 29 11:59:47 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 c1d35d2093fe614f342952ff57b412c0dbbd6781
Author: ossareh <ossareh at justin.tv>
Date:   Sun Oct 2 18:28:15 2011 -0700

    support logging via /dev/log (Linux) or /var/run/syslog (Darwin)
---
 index.js     | 107 +++++++++++++++++++++++++++++++++++++++++++++++------------
 package.json |   6 +++-
 2 files changed, 90 insertions(+), 23 deletions(-)

diff --git a/index.js b/index.js
index efbc21a..06fac93 100644
--- a/index.js
+++ b/index.js
@@ -5,6 +5,59 @@ var nodeConsole = console;
 var DefaultHostname = require("os").hostname();
 var DefaultAddress = "127.0.0.1";
 
+var Transport = {
+    UDP: function(message, severity) {
+        var client = dgram.createSocket('udp4');
+
+        message = new Buffer('<' + (this.facility * 8 + severity) + '>' +
+            getDate() + ' ' + this.hostname + ' ' + 
+            this.tag + '[' + process.pid + ']:' + message);
+
+        client.send(message,
+                    0,
+                    message.length,
+                    this.port,
+                    this.address,
+                    this._logError
+        );
+        client.close();
+    },
+
+    file: (function() {
+        var logTarget ;
+
+        switch(require('os').type()) {
+            case 'Darwin': 
+                logTarget = '/var/run/syslog' ;
+                break ;
+
+            case 'Linux':
+                logTarget = '/dev/log' ;
+                break ;
+
+            default:
+                throw new Error('Unknown OS Type: ' + require('os').type()) ;
+
+        }
+
+        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,
+                        0,
+                        message.length,
+                        logTarget,
+                        this._logError
+            );
+            client.close() ;
+
+        }
+    })()
+};
+
 var Facility = {
     kern:   0,
     user:   1,
@@ -38,6 +91,7 @@ var Severity = {
 
 // Format RegExp
 var formatRegExp = /%[sdj]/g;
+
 /**
  * Just copy from node.js console
  * @param f
@@ -112,26 +166,45 @@ function SysLogger() {
     this._times = {};
     this._logError = function(err, other) {
       if(err){
-        nodeConsole.error('Cannot connect to %s:%d', this.hostname, this.port);
+        nodeConsole.error('Cannot log message via %s:%s:%d', this.transport, this.hostname, this.port);
       }
     }.bind(this);
 }
 
 /**
- * Init function. All arguments is optional
- * @param {String} tag By default is __filename
- * @param {Facility|Number|String} By default is "user"
- * @param {String} hostname By default is require("os").hostname()
+ * Init function, takes a configuration object. If a hostname is provided the transport is assumed
+ * to be Transport.UDP
+ * @param {Object} configuration object with the following keys:
+ *          - {String} tag By default is __filename
+ *          - {Facility|Number|String} By default is "user"
+ *          - {String} hostname By default is require("os").hostname()
+ *          - {Number} port, defaults to 514
+ *          - {Transport|String} transport to use, defaults to Transport.UDP
  */
-SysLogger.prototype.set = function(tag, facility, hostname, port) {
-    this.setTag(tag);
-    this.setFacility(facility);
-    this.setHostname(hostname);
-    this.setPort(port);
+SysLogger.prototype.set = function(config) {
+    config = config || {} ;
+
+    this.setTag(config.tag);
+    this.setFacility(config.facility);
+    this.setHostname(config.hostname);
+    this.setPort(config.port);
+    if (config.hostname) {
+        this.setTransport(Transport.UDP) ;
+    } else {
+        this.setTransport(config.transport) ;
+    }
     
     return this;
 };
 
+SysLogger.prototype.setTransport = function(transport) {
+    this.transport = transport || Transport.UDP;
+    if (typeof this.transport == 'string') {
+        this.transport = Transport[this.transport] ;
+    }
+    return this;
+};
+
 SysLogger.prototype.setTag = function(tag) {
     this.tag = tag || __filename;
     return this;
@@ -167,24 +240,14 @@ SysLogger.prototype.get = function() {
     newLogger.set.apply(newLogger, arguments);
     return newLogger;
 };
+
 /**
  * Send message
  * @param {String} message
  * @param {Severity} severity
  */
 SysLogger.prototype._send = function(message, severity) {
-    var client = dgram.createSocket('udp4');
-    message = new Buffer('<' + (this.facility * 8 + severity) + '>' +
-        getDate() + ' ' + this.hostname + ' ' + 
-        this.tag + '[' + process.pid + ']:' + message);
-    client.send(message,
-                0,
-                message.length,
-                this.port,
-                this.address,
-                this._logError
-    );
-    client.close();
+    this.transport(message, severity) ;
 };
 
 /**
diff --git a/package.json b/package.json
index f3e414c..7a92c27 100644
--- a/package.json
+++ b/package.json
@@ -1,7 +1,7 @@
 {
     "name" : "ain2",
     "description" : "Syslog logging for node.js. Continuation of ain",
-    "version" : "0.0.3",
+    "version" : "0.0.4",
     "main" : "./index",
     "author" : "Alexander Dorofeev <aka.spin at gmail.com>",
     "contributors" : [
@@ -16,6 +16,10 @@
         {
             "name" : "Mark Wubben",
             "email" : "mark at novemberborn.net"
+        },
+        {
+            "name" : "Parham Michael Ossareh",
+            "email" : "ossareh at gmail.com"
         }
     ],
     "repository" : {

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