bunyan-syslog
Advanced tools
Comparing version 0.1.2 to 0.2.0
@@ -5,2 +5,3 @@ // Copyright 2013 Mark Cavage, Inc. All rights reserved. | ||
var SyslogStream = require('./sys'); | ||
var TCPStream = require('./tcp'); | ||
@@ -11,2 +12,29 @@ var UDPStream = require('./udp'); | ||
///--- Globals | ||
var FACILITY = { | ||
kern: 0, | ||
user: 1, | ||
mail: 2, | ||
daemon: 3, | ||
auth: 4, | ||
syslog: 5, | ||
lpr: 6, | ||
news: 7, | ||
uucp: 8, | ||
authpriv: 10, | ||
ftp: 11, | ||
cron: 15, | ||
local0: 16, | ||
local1: 17, | ||
local2: 18, | ||
local3: 19, | ||
local4: 20, | ||
local5: 21, | ||
local6: 22, | ||
local7: 23 | ||
}; | ||
///--- Exports | ||
@@ -19,30 +47,27 @@ | ||
if (opts.type === 'tcp') | ||
return (new TCPStream(opts)); | ||
var type; | ||
return (new UDPStream(opts)); | ||
switch (opts.type) { | ||
case 'sys': | ||
type = SyslogStream; | ||
break; | ||
case 'tcp': | ||
type = TCPStream; | ||
break; | ||
case 'udp': | ||
default: | ||
type = UDPStream; | ||
break; | ||
} | ||
return (new type(opts)); | ||
}, | ||
facility: { | ||
kern: 0, | ||
user: 1, | ||
mail: 2, | ||
daemon: 3, | ||
auth: 4, | ||
syslog: 5, | ||
lpr: 6, | ||
news: 7, | ||
uucp: 8, | ||
authpriv: 10, | ||
ftp: 11, | ||
cron: 15, | ||
local0: 16, | ||
local1: 17, | ||
local2: 18, | ||
local3: 19, | ||
local4: 20, | ||
local5: 21, | ||
local6: 22, | ||
local7: 23 | ||
} | ||
facility: FACILITY | ||
}; | ||
Object.keys(FACILITY).forEach(function (k) { | ||
module.exports[k] = FACILITY[k]; | ||
}); |
@@ -10,4 +10,6 @@ // Copyright 2013 Mark Cavage, Inc. All rights reserved. | ||
var binding = require('../build/Release/syslog'); | ||
///--- Globals | ||
@@ -18,3 +20,2 @@ | ||
var HOSTNAME = os.hostname(); | ||
var STR_FMT = '[object %s<facility=%d, host=%s, port=%d, proto=%s>]'; | ||
@@ -46,5 +47,5 @@ // Harcoded from https://github.com/trentm/node-bunyan so this module | ||
} | ||
}; | ||
// Syslog Levels | ||
@@ -54,3 +55,3 @@ var LOG_EMERG = 0; | ||
var LOG_CRIT = 2; | ||
var LOG_ERROR = 3; | ||
var LOG_ERR = 3; | ||
var LOG_WARNING = 4; | ||
@@ -62,3 +63,2 @@ var LOG_NOTICE = 5; | ||
///--- Helpers | ||
@@ -76,3 +76,3 @@ | ||
case bunyan.ERROR: | ||
sysl = LOG_ERROR; | ||
sysl = LOG_ERR; | ||
break; | ||
@@ -108,4 +108,2 @@ | ||
assert.optionalNumber(opts.facility, 'options.facility'); | ||
assert.optionalString(opts.host, 'options.host'); | ||
assert.optionalNumber(opts.port, 'options.port'); | ||
assert.optionalString(opts.name, 'options.name'); | ||
@@ -116,10 +114,9 @@ | ||
this.facility = opts.facility || 1; | ||
this.host = opts.host || '127.0.0.1'; | ||
this.name = opts.name || process.title || process.argv[0]; | ||
this.port = opts.port || 514; | ||
this.writable = true; | ||
this.socket = dgram.createSocket('udp4'); | ||
this.socket.on('close', this.emit.bind(this, 'close')); | ||
this.socket.on('error', this.emit.bind(this, 'error')); | ||
if (this.constructor.name === 'SyslogStream') { | ||
binding.openlog(this.name, binding.LOG_CONS, 0); | ||
process.nextTick(this.emit.bind(this, 'connect')); | ||
} | ||
} | ||
@@ -130,2 +127,8 @@ util.inherits(SyslogStream, Stream); | ||
// Overriden by TCP/UDP | ||
SyslogStream.prototype.close = function close() { | ||
binding.closelog(); | ||
}; | ||
SyslogStream.prototype.destroy = function destroy() { | ||
@@ -151,3 +154,3 @@ this.writable = false; | ||
var h; | ||
var l = this.facility * 8; | ||
var l; | ||
var m; | ||
@@ -161,3 +164,3 @@ var t; | ||
h = r.hostname; | ||
l = (this.facility * 8) + level(r.level); | ||
l = level(r.level); | ||
m = JSON.stringify(r, bunyan.safeCycles()); | ||
@@ -171,4 +174,5 @@ t = time(r.time); | ||
l = (this.facility * 8) + (l !== undefined ? l : level(bunyan.INFO)); | ||
var hdr = sprintf('<%d>%s %s %s[%d]:', | ||
(l || (this.facility * 8) + level(bunyan.INFO)), | ||
l, | ||
(t || time()), | ||
@@ -179,3 +183,7 @@ (h || HOSTNAME), | ||
this._send(new Buffer(hdr + m, 'utf-8')); | ||
if (this._send) { | ||
this._send(hdr + m); | ||
} else { | ||
binding.syslog(l, m); | ||
} | ||
}; | ||
@@ -185,8 +193,14 @@ | ||
SyslogStream.prototype.toString = function toString() { | ||
return (sprintf(STR_FMT, | ||
'SyslogStream', | ||
this.facility, | ||
this.host, | ||
this.port, | ||
/UDP/.test(this.constructor.name) ? 'udp' : 'tcp')); | ||
var str = '[object SyslogStream<facility=' + this.facility; | ||
if (this.host) | ||
str += ', host=' + this.host; | ||
if (this.port) | ||
str += ', port=' + this.port; | ||
if (!/^Sys/.test(this.constructor.name)) { | ||
str += ', proto=' + | ||
(/UDP/.test(this.constructor.name) ? 'udp' : 'tcp'); | ||
} | ||
str += '>]'; | ||
return (str); | ||
}; |
@@ -51,6 +51,13 @@ // Copyright 2013 Mark Cavage, Inc. All rights reserved. | ||
function TCPStream(opts) { | ||
SyslogStream.call(this, opts); | ||
assert.object(opts, 'options'); | ||
assert.optionalString(opts.host, 'options.host'); | ||
assert.optionalNumber(opts.port, 'options.port'); | ||
var self = this; | ||
SyslogStream.call(this, opts); | ||
this.host = opts.host || '127.0.0.1'; | ||
this.port = opts.port || 10514; | ||
this.queue = []; | ||
@@ -107,5 +114,4 @@ | ||
TCPStream.prototype._send = function _send(buf) { | ||
this.socket.write(buf); | ||
this.socket.write('\n', 'utf8'); | ||
TCPStream.prototype._send = function _send(msg) { | ||
this.socket.write(new Buffer(msg + '\n', 'utf-8')); | ||
}; |
@@ -6,2 +6,4 @@ // Copyright 2013 Mark Cavage, Inc. All rights reserved. | ||
var assert = require('assert-plus'); | ||
var SyslogStream = require('./sys'); | ||
@@ -15,4 +17,11 @@ | ||
function UDPStream(opts) { | ||
assert.object(opts, 'options'); | ||
assert.optionalString(opts.host, 'options.host'); | ||
assert.optionalNumber(opts.port, 'options.port'); | ||
SyslogStream.call(this, opts); | ||
this.host = opts.host || '127.0.0.1'; | ||
this.port = opts.port || 514; | ||
this.socket = dgram.createSocket('udp4'); | ||
@@ -39,3 +48,4 @@ this.socket.on('close', this.emit.bind(this, 'close')); | ||
UDPStream.prototype._send = function _send(buf) { | ||
UDPStream.prototype._send = function _send(msg) { | ||
var buf = new Buffer(msg, 'utf-8'); | ||
var s = this.socket; | ||
@@ -42,0 +52,0 @@ var self = this; |
{ | ||
"name": "bunyan-syslog", | ||
"description": "Syslog Stream for Bunyan", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"author": "Mark Cavage", | ||
@@ -6,0 +6,0 @@ "main": "./lib/index.js", |
@@ -28,2 +28,3 @@ // Copyright 2013 Mark Cavage, Inc. All rights reserved. | ||
t.ok(STREAM); | ||
console.error(STREAM.toString()); | ||
@@ -30,0 +31,0 @@ LOG = bunyan.createLogger({ |
@@ -27,2 +27,3 @@ // Copyright 2013 Mark Cavage, Inc. All rights reserved. | ||
t.ok(STREAM); | ||
console.error(STREAM.toString()); | ||
@@ -29,0 +30,0 @@ LOG = bunyan.createLogger({ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3898481
151
535
4