Permalink
Browse files

Initial commit

  • Loading branch information...
wavded committed Nov 28, 2011
0 parents commit 68cb20e6300c1a91614a4863f419d2596ad95ca0
Showing with 135 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +72 −0 lib/winston-mail.js
  3. +22 −0 package.json
  4. +8 −0 test/fixtures/test-config.json
  5. +31 −0 test/winston-mail-test.js
@@ -0,0 +1,2 @@
+node_modules
+npm-debug.log
@@ -0,0 +1,72 @@
+/*
+ * winston-mail.js: Transport for outputting logs to email
+ *
+ * (C) 2010 Marc Harter
+ * MIT LICENCE
+ */
+
+var util = require('util');
+var mail = require('mail');
+var winston = require('winston');
+
+/**
+ * @constructs Mail
+ * @param {object} options hash of options
+ */
+
+var Mail = exports.Mail = function (options) {
+ options = options || {};
+
+ if(!options.to){
+ throw "winston-mail requires 'to' property";
+ }
+
+ this.name = 'mail';
+ this.to = options.to;
+ this.from = options.from || "no-reply@winston.com";
+ this.collection = options.collection || "log";
+ this.safe = options.safe || true;
+ this.level = options.level || 'info';
+ this.silent = options.silent || false;
+ this.state = 'unopened';
+ this.pending = [];
+
+ this.client = mail.Mail(options);
+};
+
+/** @extends winston.Transport */
+util.inherits(Mail, winston.Transport);
+
+/**
+ * Define a getter so that `winston.transports.MongoDB`
+ * is available and thus backwards compatible.
+ */
+winston.transports.Mail = Mail;
+
+/**
+ * Core logging method exposed to Winston. Metadata is optional.
+ * @function log
+ * @member Mail
+ * @param level {string} Level at which to log the message
+ * @param msg {string} Message to log
+ * @param meta {Object} **Optional** Additional metadata to attach
+ * @param callback {function} Continuation to respond to when complete.
+ */
+Mail.prototype.log = function (level, msg, meta, callback) {
+ var self = this;
+ if (this.silent) return callback(null, true);
+
+ var body = meta ? msg + "\n\n" + JSON.stringify(meta) : msg;
+
+ this.client.message({
+ from: this.from,
+ to: this.to,
+ subject: "winston: " + level + " " + msg
+ })
+ .body(body)
+ .send(function (err) {
+ if (err) self.emit('error', err);
+ self.emit('logged');
+ callback(null, true);
+ });
+};
@@ -0,0 +1,22 @@
+{
+ "name": "winston-mail",
+ "description": "A mail transport for winston",
+ "version": "0.0.1",
+ "author": "Marc Harter <wavded@gmail.com>",
+ "contributors": [],
+ "repository": {
+ "type": "git",
+ "url": "http://github.com/wavded/winston-mail.git"
+ },
+ "keywords": ["logging", "sysadmin", "tools", "winston", "email"],
+ "dependencies": {
+ "mail": ""
+ },
+ "devDependencies": {
+ "winston": "0.4.x",
+ "vows": "0.5.x"
+ },
+ "main": "./lib/winston-mail",
+ "scripts": { "test": "vows test/*-test.js --spec" },
+ "engines": { "node": ">= 0.4.0" }
+}
@@ -0,0 +1,8 @@
+{
+ "transports": {
+ "mail": {
+ "to": "wavded@gmail.com",
+ "host": "172.25.16.96"
+ }
+ }
+}
@@ -0,0 +1,31 @@
+/*
+ * mongodb-test.js: Tests for instances of the MongoDB transport
+ *
+ * (C) 2011 Marc Harter
+ * MIT LICENSE
+ */
+var vows = require('vows');
+var assert = require('assert');
+var winston = require('winston');
+var helpers = require('winston/test/helpers');
+var Mail = require('../lib/winston-mail').Mail;
+
+function assertMail (transport) {
+ assert.instanceOf(transport, Mail);
+ assert.isFunction(transport.log);
+};
+
+var config = helpers.loadConfig(__dirname)
+var transport = new (Mail)(config.transports.mail);
+
+vows.describe('winston-mail').addBatch({
+ "An instance of the Mail Transport": {
+ "should have the proper methods defined": function () {
+ assertMail(transport);
+ },
+ "the log() method": helpers.testNpmLevels(transport, "should log messages to Mail", function (ign, err, logged) {
+ assert.isTrue(!err);
+ assert.isTrue(logged);
+ })
+ }
+}).export(module);

0 comments on commit 68cb20e

Please sign in to comment.