Permalink
Browse files

Adding winston plugin

  • Loading branch information...
marktheunissen committed Sep 4, 2012
1 parent 6559508 commit adcd8f7bab3b50a72b3908066f611aff84047dba
Showing with 158 additions and 84 deletions.
  1. +16 −4 README.md
  2. +2 −2 binding.gyp
  3. +4 −75 lib/journald.js
  4. +78 −0 lib/log_journald.js
  5. +35 −0 lib/winston_journald.js
  6. +11 −1 package.json
  7. +1 −1 src/{journald_lib.cc → journald_cpp.cc}
  8. +11 −1 test.js
View
@@ -1,13 +1,19 @@
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
=====
-var journald = require('journald')
-journald.send('MESSAGE=hello world', 'ARG1=first_argument', 'ARG2=second_argument');
+``` js
+ var journald = require('journald')
+ journald.send('MESSAGE=hello world', 'ARG1=first_argument', 'ARG2=second_argument');
+```
Developing
==========
@@ -16,7 +22,11 @@ Install node-gyp to build the extension:
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
@@ -30,3 +40,5 @@ Viewing Output
Quick way to view output with all fields as it comes in:
sudo journalctl -f --output=json
+
+[0]: https://github.com/flatiron/winston
View
@@ -1,8 +1,8 @@
{
"targets": [
{
- "target_name": "journald_lib",
- "sources": [ "src/journald_lib.cc" ],
+ "target_name": "journald_cpp",
+ "sources": [ "src/journald_cpp.cc" ],
'libraries': [ '/lib64/libsystemd-journal.so' ]
}
]
View
@@ -1,78 +1,7 @@
-var journald_lib = require('../build/Release/journald_lib')
-
-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;
- }
-};
+var log_journald = require('./log_journald'),
+ winston_journald = require('./winston_journald');
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() {
- // }
+ Log: log_journald,
+ WinstonTransport: winston_journald
}
View
@@ -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() {
+ // }
+}
View
@@ -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);
+};
View
@@ -4,5 +4,15 @@
"name": "journald",
"author": "Mark Theunissen",
"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"
+ }
}
@@ -146,4 +146,4 @@ void init(Handle<Object> target) {
target->Set(String::NewSymbol("send"), FunctionTemplate::New(SdJournalSend)->GetFunction());
}
-NODE_MODULE(journald_lib, init)
+NODE_MODULE(journald_cpp, init)
View
12 test.js
@@ -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) {
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');
+// Or pass as an object.
journald.log({
MESSAGE: 'hello world',
ARG1: 'first argument here',

0 comments on commit adcd8f7

Please sign in to comment.