Skip to content

Commit

Permalink
Add some comments, improve support for winston
Browse files Browse the repository at this point in the history
  • Loading branch information
marktheunissen committed Sep 4, 2012
1 parent 9803db4 commit 5c7695f
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 44 deletions.
20 changes: 20 additions & 0 deletions LICENSE
@@ -0,0 +1,20 @@
Copyright (c) 2012 Mark Theunissen

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.
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -44,3 +44,10 @@ Quick way to view output with all fields as it comes in:
sudo journalctl -f --output=json

[0]: https://github.com/flatiron/winston

LICENSE
-------

(c) 2012 Mark Theunissen

MIT (EXPAT)
8 changes: 8 additions & 0 deletions lib/journald.js
@@ -1,3 +1,11 @@
/**
* Wraps the two plugins provided, the winston plugin and the native logger.
*
* (C) 2012 Mark Theunissen
* MIT (EXPAT) LICENCE
*
*/

var log_journald = require('./log_journald'),
winston_journald = require('./winston_journald');

Expand Down
55 changes: 31 additions & 24 deletions lib/log_journald.js
@@ -1,7 +1,38 @@
/**
* Logging to the systemd journal.
*
* (C) 2012 Mark Theunissen
* MIT (EXPAT) LICENCE
*
*/

var journald_cpp = require('../build/Release/journald_cpp');

var noop = function() {};

/**
* Exported functionality of the log module.
*/
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);
}
}

var journald = {
send: function(args) {
// Send log messages by passing an object as the first argument,
Expand Down Expand Up @@ -52,27 +83,3 @@ var journald = {
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() {
// }
}
36 changes: 26 additions & 10 deletions lib/winston_journald.js
@@ -1,5 +1,4 @@
/**
*
* Winston Transport for outputting to the systemd journal.
*
* (C) 2012 Mark Theunissen
Expand All @@ -12,24 +11,41 @@ var util = require('util'),
Transport = require('winston').Transport,
journald = require('./log_journald');

/**
* The module's exports.
*/
var Journald = exports.Journald = function (options) {
options = options || {};

this.name = 'journald';
};

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

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

/**
* Write to the log. The priority and level are added to the log message.
*
* Any additional fields can be passed in the 'meta' parameter, and are added
* to the message.
*/
Journald.prototype.log = function (level, msg, meta, callback) {
journald.log(msg);
meta = meta || {};
meta.LEVEL = level;

msg_split = msg.split('=', 2);
if (msg_split.length > 1) {
meta[msg_split[0]] = msg_split[1];
}
else {
meta.MSG = msg_split[0];
}

callback(null, true);
journald.log(meta, callback);
};
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"description": "Log to the systemd journal",
"version": "0.0.1",
"version": "0.0.2",
"name": "journald",
"author": "Mark Theunissen",
"homepage": "https://github.com/systemd/node-systemd",
Expand Down
10 changes: 10 additions & 0 deletions src/journald_cpp.cc
@@ -1,3 +1,13 @@
/**
* Native extension that logs to the systemd journal asyncronously. It also
* supports a sync mode but that isn't yet implemented in the journald.js
* lib.
*
* (C) 2012 Mark Theunissen
* MIT (EXPAT) LICENCE
*
*/

#include <node.h>
#include <v8.h>
#include <systemd/sd-journal.h>
Expand Down
34 changes: 25 additions & 9 deletions test.js
@@ -1,21 +1,37 @@
/**
* Example code for the systemd journal node extension.
*
* (C) 2012 Mark Theunissen
* MIT (EXPAT) LICENCE
*
*/

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.
// Log using Winston, first add the journald transport and then send.
// This will also print to the console as that is the default winston
// log transport.
winston.add(journald_transport.Journald);
winston.info('MSG=Hello again distributed logs');
winston.info('Simple message format');
winston.info('CUSTOMKEY=Specified a key');

// Now log directly using the journald log.
var callback = function(err, result) {
console.warn(result);
}
winston.log('error', 'MSG=Multiple messages can be sent', {
STATE: 'System crashing',
BECAUSE: 'Someone unplugged it'
});

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

// Or pass as an object.
// Or pass as an object and optionally end with a callback function. This
// one outputs the result of the call to the console, this is taken directly
// from the C call to systemd, so will be 0 (pass) or 1 (fail).
var callback = function(err, result) {
console.warn(result);
}
journald.log({
MESSAGE: 'hello world',
ARG1: 'first argument here',
Expand Down

0 comments on commit 5c7695f

Please sign in to comment.