Skip to content

Commit

Permalink
first drop
Browse files Browse the repository at this point in the history
  • Loading branch information
jfromaniello committed Apr 7, 2012
1 parent 33e580e commit 786b35d
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
node_modules/*
winser*tgz
*tgz
npm-debug.log
75 changes: 75 additions & 0 deletions lib/eventlog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* eventlog.js: Transport for outputting to the Windows Event Log
*
* (C) 2012 Jose Fernando Romaniello
* MIT LICENCE
*
*/

var events = require('events'),
util = require('util'),
Transport = require('winston').Transport,
exec = require("child_process").exec,
levelsMap = {"info": "information", "warn": "warning", "warning": "warning", "error": "error"},
isWindows = require("os").platform() === "win32";

//
// ### function EventLog (options)
// #### @options {Object} Options for this instance.
// Constructor function for the EventLog transport object responsible
// for persisting log messages and metadata to the windows event log.
//
var EventLog = exports.EventLog = function (options) {
Transport.call(this, options);
options = options || {};

this.name = 'eventlog';
this.eventlog = options.eventlog || "application"; //system is also valid.
this.appName = options.appName || "node"; //system is also valid
this.json = options.json || false;
this.timestamp = typeof options.timestamp !== 'undefined' ? options.timestamp : false;

if (this.json) {
this.stringify = options.stringify || function (obj) {
return JSON.stringify(obj, null, 2);
};
}
};

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

//
// Expose the name of this Transport on the prototype
//
EventLog.prototype.name = 'eventlog';

//
// ### function log (level, msg, [meta], callback)
// #### @level {string} Level at which to log the message.
// #### @msg {string} Message to log
// #### @meta {Object} **Optional** Additional metadata to attach
// #### @callback {function} Continuation to respond to when complete.
// Core logging method exposed to Winston. Metadata is optional.
//
EventLog.prototype.log = function (level, msg, meta, callback) {
if (this.silent || !isWindows) {
return callback(null, true);
}

var self = this,
output,
message = msg + (meta ? (" " + "metadata: " + JSON.stringify(meta)) : ""),
mapedlevel = levelsMap[level] || "information";

exec("eventcreate /t " + mapedlevel + " /id 100 /l " + this.eventlog + " /d \"" + message + "\" /so " + this.appName, function(err, stdout, stderr){
self.emit('logged');
});


callback(null, true);
};

exports.config = require("./windowslogs-config");
21 changes: 21 additions & 0 deletions lib/windowslogs-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* windowslogs-config.js: Config that conform to commonly used Windows logging levels.
*
* (C) 2012 Jose Fernando Romaniello
* MIT LICENCE
*
*/

var winlogconfig = exports;

winlogconfig.levels = {
info: 0,
warning: 1,
error: 2
};

winlogconfig.colors = {
info: 'green',
warning: 'yellow',
error: 'red'
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
},
"dependencies": {
"winston" : "*"
}
},
"main": "./lib/eventlog"
}

0 comments on commit 786b35d

Please sign in to comment.