Skip to content

Instantly share code, notes, and snippets.

@flammenmensch
Created July 27, 2012 07:28
Show Gist options
  • Select an option

  • Save flammenmensch/3186652 to your computer and use it in GitHub Desktop.

Select an option

Save flammenmensch/3186652 to your computer and use it in GitHub Desktop.

Revisions

  1. flammenmensch revised this gist Jul 27, 2012. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion pushover-transport.js
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,6 @@ PushoverLogger.prototype.log = function (level, msg, meta, callback) {
    req = https.request({
    method: 'POST',
    hostname: 'api.pushover.net',
    port: this.port,
    path: '/1/messages.json',
    headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
  2. flammenmensch created this gist Jul 27, 2012.
    59 changes: 59 additions & 0 deletions pushover-transport.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    'use strict';

    var util = require('util'),
    https = require('https'),
    winston = require('winston'),
    querystring = require('querystring');

    var PushoverLogger = winston.transports.PushoverLogger = function (options) {
    this.token = options.token;
    this.user = options.user;

    this.name = 'Pushover.net Logger';
    this.level = options.level || 'error';
    };

    util.inherits(PushoverLogger, winston.Transport);

    PushoverLogger.prototype.log = function (level, msg, meta, callback) {
    if (!callback) {
    callback = function () {};
    }

    var parameters = querystring.stringify({
    priority: 1,
    token: this.token,
    user: this.user,
    message: 'An error has occurred: ' + msg
    }),
    req = https.request({
    method: 'POST',
    hostname: 'api.pushover.net',
    port: this.port,
    path: '/1/messages.json',
    headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': parameters.length
    }
    }, function (res) {
    var output = "";

    res.setEncoding('utf8');

    res.on('data', function (chunk) { output += chunk; });

    res.on('end', function () {
    try {
    var jsonOutput = JSON.parse(output);
    callback(null, jsonOutput);
    } catch (err) {
    callback(err);
    }
    });
    }).on('error', function (err) {
    callback(err);
    });

    req.write(parameters);
    req.end();
    };