Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

init

  • Loading branch information...
commit 788f0404cc8cbde67c9a99c03f1e32582b4b774b 0 parents
@cloudhead authored
Showing with 240 additions and 0 deletions.
  1. +20 −0 LICENSE
  2. +80 −0 README.md
  3. +128 −0 lib/node-syslog.js
  4. +12 −0 package.json
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2010 Alexis Sellier
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
80 README.md
@@ -0,0 +1,80 @@
+
+node-syslog
+===========
+
+> Syslog-ng TCP client for node, with basic fault-tolerance.
+
+installation
+------------
+
+ $ npm install syslog
+
+synopsis
+--------
+
+ var syslog = require('syslog');
+ var logger = syslog.createClient(514, 'localhost');
+
+ logger.info("ping!");
+
+log levels
+----------
+
+In increasing order of severity:
+
+- debug
+- info
+- notice
+- warning
+- error
+- crit
+- alert
+- emerg
+
+These are available as methods on Client, ex: `logger.crit()`.
+
+You may also call the `log` method, and pass the level as the 2nd argument:
+
+ logger.log('fnord!', syslog.LOG_CRIT);
+
+The default level is `info`.
+
+configuration
+-------------
+
+You will have to configure your syslog server to accept TCP connections.
+This is usually done in `/etc/syslog-ng.conf`. Let's say you have an app called `fnord`,
+the configuration would look something like this:
+
+ source tcp_s {
+ tcp(ip(0.0.0.0) port(514) max-connections(256));
+ };
+ destination fnord_d {
+ file("/var/log/fnord.log");
+ };
+ log { source(tcp_s); destination(fnord_d); };
+
+If you have multiple apps which need to log via TCP, you can specify filters, as such:
+
+ filter fnord_f { program("fnord"); };
+
+Then modify the log statement to read:
+
+ log { source(tcp_s); filter(fnord_f); destination(fnord_d); };
+
+Now if you have another app, called `bnord`, create similar `destination` and `filter` configurations for it,
+and specify a new log statement, with the same `source`:
+
+ log { source(tcp_s); filter(bnord_f); destination(bnord_d); };
+
+For this to work, you have to make sure you set the `process.title` variable in your node app.
+
+ process.title = 'fnord';
+
+license
+-------
+
+See `LICENSE` file.
+
+Copyright (c) 2011 Alexis Sellier
+
128 lib/node-syslog.js
@@ -0,0 +1,128 @@
+var net = require('net');
+var os = require('os');
+//
+// node-syslog.js - TCP syslog-ng client
+//
+
+// Message severity levels
+this.LOG_EMERG = 0;
+this.LOG_ALERT = 1;
+this.LOG_CRIT = 2;
+this.LOG_ERROR = 3;
+this.LOG_WARNING = 4;
+this.LOG_NOTICE = 5;
+this.LOG_INFO = 6;
+this.LOG_DEBUG = 7;
+
+this.FACULTY_USER = 1;
+
+this.DEFAULT_OPTIONS = {
+ faculty: exports.FACULTY_USER,
+ name: null
+};
+
+var hostname = os.hostname();
+
+this.Client = function (port, host, options) {
+ this.port = port || 514;
+ this.host = host || 'localhost';
+ this.options = options || {};
+
+ for (var k in exports.DEFAULT_OPTIONS) {
+ if (this.options[k] === undefined) { this.options[k] = exports.DEFAULT_OPTIONS[k] }
+ }
+
+ // We need to set this option here, incase the module is loaded before `process.title` is set.
+ if (! this.options.name) { this.options.name = process.title || process.argv.join(' ') }
+
+ this.socket = null;
+ this.retries = 0;
+ this.queue = [];
+};
+this.Client.prototype = new(function () {
+ var that = this;
+
+ // Generate logging methods, such as `info`, `debug`, ...
+ for (var k in exports) {
+ if (/^LOG/.test(k)) {
+ (function (level, name) {
+ that[name] = function (msg) {
+ this.log(msg, exports[level]);
+ };
+ })(k, k.match(/^LOG_([A-Z]+)/)[1].toLowerCase());
+ }
+ }
+
+ this.log = function (msg, severity) {
+ var that = this;
+ msg = msg.trim();
+ severity = severity !== undefined ? severity : exports.LOG_INFO;
+
+ this.connect(function (e) {
+ var pri = '<' + ((that.options.faculty * 8) + severity) + '>'; // Message priority
+ var entry = pri + [
+ new(Date)().toJSON(),
+ hostname,
+ that.options.name + '[' + process.pid + ']:',
+ msg
+ ].join(' ') + '\n';
+
+ //
+ // If there's a connection problem,
+ // queue the message for later processing.
+ //
+ if (e) {
+ that.queue.push(entry);
+ // Write the entry to the socket
+ } else {
+ that.socket.write(entry, 'utf8', function (e) {
+ if (e) { that.queue.push(entry) }
+ });
+ }
+ });
+ };
+ this.connect = function (callback) {
+ var that = this;
+
+ callback = callback || function () {};
+
+ if (this.socket) {
+ if (this.socket.readyState === 'open') {
+ callback(null);
+ } else {
+ callback(true);
+ }
+ } else {
+ callback(true);
+
+ this.socket = net.createConnection(this.port, this.host);
+ this.socket.setKeepAlive(true);
+ this.socket.setNoDelay();
+ this.socket.on('connect', function () {
+ that.socket.write(that.queue.join(''));
+ that.queue = [];
+ that.retries = 0;
+ that.connected = true;
+ }).on('error', function (e) {
+ console.log(e.message);
+ }).on('end', function (e) {
+ }).on('close', function (e) {
+ var interval = Math.pow(2, that.retries);
+ that.connected = false;
+ setTimeout(function () {
+ that.retries ++;
+ that.socket.connect(that.port, that.host);
+ }, interval * 1000);
+ }).on('timeout', function () {
+ if (that.socket.readyState !== 'open') {
+ that.socket.destroy();
+ }
+ });
+ }
+ };
+});
+
+this.createClient = function (port, host, options) {
+ return new(this.Client)(port, host, options);
+};
+
12 package.json
@@ -0,0 +1,12 @@
+{
+ "name" : "syslog",
+ "description" : "Syslog-ng TCP client, with basic fault-tolerance.",
+ "url" : "http://github.com/cloudhead/node-syslog",
+ "keywords" : ["syslog", "logger"],
+ "author" : "Alexis Sellier <self@cloudhead.net>",
+ "contributors" : [],
+ "version" : "0.1.0",
+ "main" : "./lib/node-syslog",
+ "directories" : { "lib": "./lib" },
+ "engines" : { "node": ">=0.3.6" }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.