Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor winston-posix-syslog in order to be consistent with other winst... #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ A Syslog transport for [winston][0] using posix.
### Installing winston-syslog

``` bash
$ npm install winston
$ npm install winston-posix-syslog
$ npm install winston --save
$ npm install winston-posix-syslog --save
```

## Motivation
Expand All @@ -20,9 +20,9 @@ To use the PosixSyslog transport in [winston][0], you simply need to require it

``` js
var winston = require('winston');
var PosixSyslog = require('winston-posix-syslog').PosixSyslog;
require('winston-posix-syslog');

winston.add(PosixSyslog, options);
winston.add(winston.transports.PosixSyslog, options);

winston.log('info', 'I AM the one who knocks.');
```
Expand Down
93 changes: 52 additions & 41 deletions lib/winston-posix-syslog.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,80 @@
var util = require('util')
var util = require('util')
, winston = require('winston')
, posix = require('posix')
;
, posix = require('posix')
, common = require('winston/lib/winston/common');

var syslogLevels = {
debug: 'debug',
info: 'info',
notice: 'notice',
warn: 'warning',
error: 'err',
crit: 'crit',
alert: 'alert'
debug : 'debug',
info : 'info',
notice : 'notice',
warn : 'warning',
error : 'err',
crit : 'crit',
alert : 'alert'
};

var getMasks = function() {
var masks = {};
var PosixSyslog = exports.PosixSyslog = function (options) {
winston.Transport.call(this, options);

for (var level in syslogLevels) {
var mask = syslogLevels[level];
masks[mask] = true;
}
options = options || {};

return masks;
}

var PosixSyslog = winston.transports.PosixSyslog = function (options) {
options = options || {};

this.identity = options.identity || process.title;
this.facility = options.facility || 'local0';
this.timestamp = typeof options.timestamp !== 'undefined' ? options.timestamp : false;
this.prettyPrint = options.prettyPrint || false;
this.label = options.label || null;
this.identity = options.identity || process.title;
this.facility = options.facility || 'local0';

this.openLogOptions = {
cons: options.cons || true,
ndelay: options.ndelay || true,
pid: options.pid || true,
nowait: options.nowait || true,
odelay: options.odelay || false
cons : options.cons || true,
ndelay : options.ndelay || true,
pid : options.pid || true,
nowait : options.nowait || true,
odelay : options.odelay || false
}
};

var buildMessage = function(msg, meta) {
if (meta) {
return util.format("%s: %s", msg, JSON.stringify(meta));
}

return msg;
};

util.inherits(PosixSyslog, winston.Transport);

winston.transports.PosixSyslog = PosixSyslog;

PosixSyslog.prototype.name = 'posixSyslog';

PosixSyslog.prototype.log = function (level, msg, meta, callback) {
var self = this;
var self = this
, output = common.log({
message : msg,
meta : meta,
colorize : false,
json : false,
level : level,
message : msg,
meta : meta,
stringify : this.stringify,
timestamp : this.timestamp,
prettyPrint : this.prettyPrint,
raw : this.raw,
label : this.label
});

// We ignore any incompatible levels
if (level in syslogLevels) {
posix.openlog(self.identity, self.openLogOptions, self.facility);
posix.setlogmask(getMasks());
posix.syslog(syslogLevels[level], buildMessage(msg, meta));
posix.setlogmask(self.getMasks());
posix.syslog(syslogLevels[level], output);
posix.closelog();
self.emit('logged');
}

callback(null, true);
};

exports.PosixSyslog = PosixSyslog;
PosixSyslog.prototype.getMasks = function() {
var masks = {};

for (var level in syslogLevels) {
var mask = syslogLevels[level];
masks[mask] = true;
}

return masks;
};
33 changes: 23 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
{
"name": "winston-posix-syslog",
"description": "A syslog transport for winston that uses posix",
"version": "0.0.7",
"author": {
"name": "Anton Nguyen",
"email": "[email protected]"
},
"description": "An awesome syslog transport for winston that uses posix",
"version": "0.0.8",
"author": "Anton Nguyen <[email protected]>",
"preferGlobal": false,
"dependencies": {
"util": "0.4.x",
Expand All @@ -15,12 +12,28 @@
"devDependencies": {
"vows": "0.7.x"
},
"repository" : {
"type": "git",
"url": "https://github.com/AntonNguyen/winston-posix-syslog.git"
"repository": {
"type": "git",
"url": "https://github.com/AntonNguyen/winston-posix-syslog.git"
},
"main": "./lib/winston-posix-syslog",
"engines": {
"node": ">= 0.4.0"
}
},
"bugs": {
"url": "https://github.com/AntonNguyen/winston-posix-syslog/issues"
},
"homepage": "https://github.com/AntonNguyen/winston-posix-syslog",
"directories": {
"test": "test"
},
"scripts": {
"test": "vows test/*.js"
},
"keywords": [
"winston",
"syslog",
"posix"
],
"license": "ISC"
}
14 changes: 7 additions & 7 deletions test/winston-posix-syslog-test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
var path = require('path')
, vows = require('vows')
, assert = require('assert')
, winston = require('winston')
, helpers = require('winston/test/helpers')
, PosixSyslog = require('../lib/winston-posix-syslog').PosixSyslog
, npmTransport = new (PosixSyslog)()
var path = require('path')
, vows = require('vows')
, assert = require('assert')
, winston = require('winston')
, helpers = require('winston/test/helpers')
, PosixSyslog = require('../lib/winston-posix-syslog').PosixSyslog
, npmTransport = new (PosixSyslog)()
, posixSyslogTransport = new (PosixSyslog)({ levels: winston.config.syslog.levels })
;

Expand Down