[Pkg-javascript-commits] [node-ain2] 01/102: Initial commit
Jonas Smedegaard
js at moszumanska.debian.org
Tue Apr 29 11:59:44 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 1a8d27d53b9dcef5e1842246f512fb305a5b58a7
Author: Alexander Dorofeev <aka.spin at gmail.com>
Date: Sun Dec 5 05:38:49 2010 +0500
Initial commit
---
.gitignore | 2 +
index.js | 166 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
readme.md | 19 +++++++
3 files changed, 187 insertions(+)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..541e887
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+/.project
+/.settings
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..58eb092
--- /dev/null
+++ b/index.js
@@ -0,0 +1,166 @@
+var dgram = require('dgram');
+var Buffer = require('buffer').Buffer;
+
+var Facility = {
+ kern: 0,
+ user: 1,
+ mail: 2,
+ daemon: 3,
+ auth: 4,
+ syslog: 5,
+ lpr: 6,
+ news: 7,
+ uucp: 8,
+ local0: 16,
+ local1: 17,
+ local2: 18,
+ local3: 19,
+ local4: 20,
+ local5: 21,
+ local6: 22,
+ local7: 23
+};
+
+var Severity = {
+ emerg: 0,
+ alert: 1,
+ crit: 2,
+ err: 3,
+ warn: 4,
+ notice: 5,
+ info: 6,
+ debug: 7
+};
+
+// Format RegExp
+var formatRegExp = /%[sdj]/g;
+/**
+ * Just copy from node.js console
+ * @param f
+ * @returns
+ */
+function format(f) {
+ if (typeof f !== 'string') {
+ var objects = [], util = require('util');
+ for (var i = 0; i < arguments.length; i++) {
+ objects.push(util.inspect(arguments[i]));
+ }
+ return objects.join(' ');
+ }
+
+ var i = 1;
+ var args = arguments;
+ var str = String(f).replace(formatRegExp, function(x) {
+ switch (x) {
+ case '%s': return args[i++];
+ case '%d': return +args[i++];
+ case '%j': return JSON.stringify(args[i++]);
+ default:
+ return x;
+ }
+ });
+ for (var len = args.length; i < len; ++i) {
+ str += ' ' + args[i];
+ }
+ return str;
+}
+
+/**
+ * Get current date in syslog format. Thanks https://github.com/kordless/lodge
+ * @returns {String}
+ */
+function getDate() {
+ var dt = new Date();
+ var hours = dt.getHours();
+ var minutes = dt.getMinutes();
+ var seconds = dt.getSeconds();
+ var month = dt.getMonth();
+ var day = dt.getDate();
+ var months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
+ 'Sep', 'Oct', 'Nov', 'Dec' ];
+ return months[month] + " " + day + " " + hours + ":" + minutes
+ + ":" + seconds;
+}
+
+/**
+ * Syslog logger
+ * @constructor
+ * @returns {SysLogger}
+ */
+function SysLogger() {
+}
+/**
+ * Init function
+ * @param {Facility|Number|String} facility
+ * @param {String} name Optional name. By default is process.argv[1]
+ * @param {String} hostname Optional hostname.
+ */
+SysLogger.prototype.init = function(facility, name, hostname) {
+ this.facility = facility || Facility.user;
+ if (typeof this.facility == 'string')
+ this.facility = Facility[this.facility];
+
+ this.name = name || process.argv[1];
+ this.hostname = hostname || 'localhost';
+};
+/**
+ * Get new instance of SysLogger.
+ * @param {Facility|Number|String} facility
+ * @param {String} name Optional name. By default is process.argv[1]
+ * @param {String} hostname Optional hostname.
+ * @returns {SysLogger}
+ */
+SysLogger.prototype.get = function() {
+ var newLogger = new SysLogger();
+ newLogger.init.apply(newLogger, arguments);
+ return newLogger;
+};
+/**
+ * Send message
+ * @param {String} message
+ * @param {Severity} severity
+ */
+SysLogger.prototype._send = function(message, severity) {
+ var client = dgram.createSocket('udp4');
+ var message = new Buffer('<' + this.facility * 8 + severity + '>' +
+ getDate() + ' ' + this.hostname + ' ' +
+ this.name + '[' + process.pid + ']:' + message);
+ client.send(message, 0, message.length, 514, '127.0.0.1',
+ function(err) {
+ if (err) console.error('Can\'t connect to localhost:514');
+ client.close();
+ });
+};
+
+/**
+ * Send formatted message to syslog
+ * @param {String} message
+ * @param {Number|String} severity
+ */
+SysLogger.prototype.send = function(message, severity) {
+ severity = severity || Severity.notice;
+ if (typeof severity == 'string') severity = Severity[severity];
+ this._send(message, severity);
+};
+
+SysLogger.prototype.log = function() {
+ this._send(format.apply(this, arguments), Severity.notice);
+};
+SysLogger.prototype.info = function() {
+ this._send(format.apply(this, arguments), Severity.info);
+};
+SysLogger.prototype.warn = function() {
+ this._send(format.apply(this, arguments), Severity.warn);
+};
+SysLogger.prototype.error = function() {
+ this._send(format.apply(this, arguments), Severity.err);
+};
+
+SysLogger.prototype.dir = function(object) {
+ var util = require('util');
+ this._send(util.inspect(object) + '\n', Severity.notice);
+};
+
+
+var logger = new SysLogger();
+module.exports = logger;
\ No newline at end of file
diff --git a/readme.md b/readme.md
new file mode 100644
index 0000000..264aa86
--- /dev/null
+++ b/readme.md
@@ -0,0 +1,19 @@
+# ain
+
+Brain-free node.js logger for syslog.
+
+*Ain* written with full compatibility with *node.js* `console` module. It
+implements all `console` functions and formatting. Also *ain* supports UTF-8
+(tested on Debian Testing/Sid).
+
+## Usage
+
+Usage of *ain* is very similar to *node.js* console. Following example
+demonstrates the replacement of the console:
+
+ var console = require('ain');
+ console.init(logger.LEVEL0, 'node-test-app');
+
+ console.log('notice level by number %d', Date.now());
+ console.info('info level');
+
\ No newline at end of file
--
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