Skip to content

Commit

Permalink
Adding winston plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
marktheunissen committed Sep 4, 2012
1 parent 6559508 commit adcd8f7
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 84 deletions.
20 changes: 16 additions & 4 deletions README.md
Original file line number Original file line Diff line number Diff line change
@@ -1,13 +1,19 @@
node-systemd node-systemd
============ ============


Node.js module for native access to the journald facilities in recent versions of systemd. In particular, this capability includes passing key/value pairs as fields that journald can use for filtering. Node.js module for native access to the journald facilities in recent
versions of systemd. In particular, this capability includes passing
key/value pairs as fields that journald can use for filtering.

Also includes a plugin for [winston][0]


Usage Usage
===== =====


var journald = require('journald') ``` js
journald.send('MESSAGE=hello world', 'ARG1=first_argument', 'ARG2=second_argument'); var journald = require('journald')
journald.send('MESSAGE=hello world', 'ARG1=first_argument', 'ARG2=second_argument');
```


Developing Developing
========== ==========
Expand All @@ -16,7 +22,11 @@ Install node-gyp to build the extension:


sudo npm install -g node-gyp sudo npm install -g node-gyp


Build: Use npm to build the extension:

npm install

Or, build the C++ extension manually:


node-gyp configure && node-gyp build node-gyp configure && node-gyp build


Expand All @@ -30,3 +40,5 @@ Viewing Output
Quick way to view output with all fields as it comes in: Quick way to view output with all fields as it comes in:


sudo journalctl -f --output=json sudo journalctl -f --output=json

[0]: https://github.com/flatiron/winston
4 changes: 2 additions & 2 deletions binding.gyp
Original file line number Original file line Diff line number Diff line change
@@ -1,8 +1,8 @@
{ {
"targets": [ "targets": [
{ {
"target_name": "journald_lib", "target_name": "journald_cpp",
"sources": [ "src/journald_lib.cc" ], "sources": [ "src/journald_cpp.cc" ],
'libraries': [ '/lib64/libsystemd-journal.so' ] 'libraries': [ '/lib64/libsystemd-journal.so' ]
} }
] ]
Expand Down
79 changes: 4 additions & 75 deletions lib/journald.js
Original file line number Original file line Diff line number Diff line change
@@ -1,78 +1,7 @@
var journald_lib = require('../build/Release/journald_lib') var log_journald = require('./log_journald'),

winston_journald = require('./winston_journald');
var noop = function() {};

var journald = {
send: function(args) {
// Send log messages by passing an object as the first argument,
// in the form:
//
// journald.send({
// MSG: 'Hello',
// MSG2: 'World'
// });
//
if (typeof args[0] == 'object') {
var strings = [];
for (key in args[0]) {
if (args[0].hasOwnProperty(key)) {
if (typeof args[0][key] == 'string') {
strings.push(key + '=' + args[0][key]);
}
else {
throw {
name: 'ArgumentsError',
message: 'Object has non-string properties'
}
}
}
}
if (strings.length > 1) {
if (typeof args[1] == 'function') {
strings.push(args[1]);
}
journald_lib.send.apply(this, strings);
}
return;
}

// Arguments given as individual strings:
//
// journald.send('MSG=Hello', 'MSG2=World')
//
for (i = 0; i < args.length; i++) {
if (typeof args[i] != 'string' && typeof args[i] != 'function') {
throw {
name: 'ArgumentsError',
message: 'Non-string arguments given'
}
}
}
journald_lib.send.apply(this, args);
return;
}
};


module.exports = { module.exports = {
log: function() { Log: log_journald,
if (arguments.length < 1) { WinstonTransport: winston_journald
throw {
name: 'ArgumentsError',
message: 'No arguments given'
}
}
var args = [];
for (i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
// If no callback is provided, add an empty one.
if (typeof arguments[arguments.length-1] !== 'function') {
args.push(noop);
}
journald.send(args);
},

// We don't support a sync version right now. But here's where it would go.
// logSync: function() {
// }
} }
78 changes: 78 additions & 0 deletions lib/log_journald.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,78 @@
var journald_cpp = require('../build/Release/journald_cpp');

var noop = function() {};

var journald = {
send: function(args) {
// Send log messages by passing an object as the first argument,
// in the form:
//
// journald.send({
// MSG: 'Hello',
// MSG2: 'World'
// });
//
if (typeof args[0] == 'object') {
var strings = [];
for (key in args[0]) {
if (args[0].hasOwnProperty(key)) {
if (typeof args[0][key] == 'string') {
strings.push(key + '=' + args[0][key]);
}
else {
throw {
name: 'ArgumentsError',
message: 'Object has non-string properties'
}
}
}
}
if (strings.length > 1) {
if (typeof args[1] == 'function') {
strings.push(args[1]);
}
journald_cpp.send.apply(this, strings);
}
return;
}

// Arguments given as individual strings:
//
// journald.send('MSG=Hello', 'MSG2=World')
//
for (i = 0; i < args.length; i++) {
if (typeof args[i] != 'string' && typeof args[i] != 'function') {
throw {
name: 'ArgumentsError',
message: 'Non-string arguments given'
}
}
}
journald_cpp.send.apply(this, args);
return;
}
};

module.exports = {
log: function() {
if (arguments.length < 1) {
throw {
name: 'ArgumentsError',
message: 'No arguments given'
}
}
var args = [];
for (i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
// If no callback is provided, add an empty one.
if (typeof arguments[arguments.length-1] !== 'function') {
args.push(noop);
}
journald.send(args);
},

// We don't support a sync version right now. But here's where it would go.
// logSync: function() {
// }
}
35 changes: 35 additions & 0 deletions lib/winston_journald.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
*
* Winston Transport for outputting to the systemd journal.
*
* (C) 2012 Mark Theunissen
* MIT (EXPAT) LICENCE
*
*/

var util = require('util'),
winston = require('winston'),
Transport = require('winston').Transport,
journald = require('./log_journald');

var Journald = exports.Journald = function (options) {
options = options || {};

this.name = 'journald';
};

//
// Inherit from `winston.Transport`.
//
util.inherits(Journald, Transport);

//
// Expose the name of this Transport on the prototype
//
Journald.prototype.name = 'journald';

Journald.prototype.log = function (level, msg, meta, callback) {
journald.log(msg);

callback(null, true);
};
12 changes: 11 additions & 1 deletion package.json
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,5 +4,15 @@
"name": "journald", "name": "journald",
"author": "Mark Theunissen", "author": "Mark Theunissen",
"homepage": "https://github.com/systemd/node-systemd", "homepage": "https://github.com/systemd/node-systemd",
"main": "./lib/journald" "directories": ["./lib", "./src"],
"main": "./lib/journald",

"dependencies": {
"winston": "*"
},

"scripts": {
"preinstall": "node-gyp clean && node-gyp configure",
"install": "node-gyp build"
}
} }
2 changes: 1 addition & 1 deletion src/journald_lib.cc → src/journald_cpp.cc
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -146,4 +146,4 @@ void init(Handle<Object> target) {
target->Set(String::NewSymbol("send"), FunctionTemplate::New(SdJournalSend)->GetFunction()); target->Set(String::NewSymbol("send"), FunctionTemplate::New(SdJournalSend)->GetFunction());
} }


NODE_MODULE(journald_lib, init) NODE_MODULE(journald_cpp, init)
12 changes: 11 additions & 1 deletion test.js
Original file line number Original file line Diff line number Diff line change
@@ -1,11 +1,21 @@
var journald = require('./lib/journald') var winston = require('winston'),
journald = require('./lib/journald').Log,
journald_transport = require('./lib/journald').WinstonTransport;


// Log using Winston, first add the transport and then send.
winston.add(journald_transport.Journald);
winston.info('MSG=Hello again distributed logs');

// Now log directly using the journald log.
var callback = function(err, result) { var callback = function(err, result) {
console.warn(result); console.warn(result);
} }


// You can pass as many string parameters as you like, and optionally end
// with a callback function.
journald.log('MESSAGE=strings sent individually', 'ARG1=as arguments'); journald.log('MESSAGE=strings sent individually', 'ARG1=as arguments');


// Or pass as an object.
journald.log({ journald.log({
MESSAGE: 'hello world', MESSAGE: 'hello world',
ARG1: 'first argument here', ARG1: 'first argument here',
Expand Down

0 comments on commit adcd8f7

Please sign in to comment.