[Pkg-javascript-commits] [node-ain2] 63/102: Imported Upstream version 1.1.0
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 41347b8553411de06ca797cabe753a3a57714041
Author: Jonas Smedegaard <dr at jones.dk>
Date: Thu Mar 1 20:11:58 2012 +0100
Imported Upstream version 1.1.0
---
CHANGELOG.md | 4 +++
index.js | 88 +++++++++++++++++++++++++++++------------------------
package.json | 2 +-
readme.md | 23 +++++++++++++-
test/integration.js | 51 +++++++++++++++++++++++++++++++
test/testServer.js | 15 ---------
6 files changed, 127 insertions(+), 56 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 e507928..04ce270 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.composerFunction(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.composerFunction(message, severity);
+ client.send(syslogMessage,
0,
- message.length,
+ syslogMessage.length,
logTarget,
this._logError
);
@@ -206,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 {
@@ -249,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
@@ -302,6 +275,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 +316,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/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
new file mode 100644
index 0000000..b314cef
--- /dev/null
+++ b/test/integration.js
@@ -0,0 +1,51 @@
+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.setMessageComposer(function(message, severity){
+ return new Buffer('<' + (this.facility * 8 + severity) + '>' +
+ this.getDate() + ' ' + '[' + process.pid + ']:' + message);
+ });
+ executeDifferentLogCalls(logger);
+
+ setTimeout(function(){
+ process.exit();
+ }, 500);
+
+}
+
+
+function setupServer(){
+ 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);
+}
+
+setupServer();
+
diff --git a/test/testServer.js b/test/testServer.js
deleted file mode 100644
index 48d2ddf..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(3000);
--
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