winston-telegram
Advanced tools
Comparing version 0.4.0 to 1.0.0
@@ -8,5 +8,5 @@ /* | ||
var util = require('util'); | ||
var request = require('request'); | ||
var winston = require('winston'); | ||
var util = require('util'); | ||
var request = require('request'); | ||
var winston = require('winston'); | ||
var nargs = /\{([0-9a-zA-Z_]+)\}/g; | ||
@@ -18,17 +18,18 @@ | ||
* @param {String} options.token Telegram bot authentication token | ||
* @param {String} options.chatid Telegram unique identifier for chat | ||
* @param {String} options.chatId Telegram unique identifier for chat | ||
*/ | ||
var Telegram = exports.Telegram = function (options) { | ||
options = options || {}; | ||
if (!options.token || !options.chatid){ | ||
throw new Error('winston-telegram requires \'token\' and \'chatid\' property'); | ||
} | ||
this.token = options.token; | ||
this.chatid = options.chatid; | ||
this.level = options.level || 'info'; | ||
this.unique = options.unique || false; | ||
this.silent = options.silent || false; | ||
this.disable_notification = options.disable_notification || false; | ||
this.name = options.name || this.name; | ||
this.template = options.template || '[{level}] {message}'; | ||
options = options || {}; | ||
if (!options.token || !options.chatId){ | ||
throw new Error('winston-telegram requires \'token\' and \'chatId\' property'); | ||
} | ||
this.token = options.token; | ||
this.chatId = options.chatId; | ||
this.level = options.level || 'info'; | ||
this.handleExceptions = options.handleExceptions || false; | ||
this.unique = options.unique || false; | ||
this.silent = options.silent || false; | ||
this.disableNotification = options.disableNotification || false; | ||
this.name = options.name || this.name; | ||
this.template = options.template || '[{level}] {message}'; | ||
}; | ||
@@ -60,52 +61,52 @@ | ||
Telegram.prototype.log = function (level, msg, meta, callback) { | ||
var self = this; | ||
if (this.silent) return callback(null, true); | ||
if (this.unique && this.level != level) return callback(null, true); | ||
request({ | ||
url : 'https://api.telegram.org/bot'+this.token+'/sendMessage', | ||
method : 'POST', | ||
json : { | ||
chat_id : this.chatid, | ||
text : format(this.template,{level : level, message : msg}), | ||
disable_notification : this.disable_notification | ||
} | ||
}, function(error, response, body){ | ||
if (error) { | ||
self.emit('error', error); | ||
} | ||
if (response && response.statusCode != 200) { | ||
self.emit('error', response.statusCode); | ||
} | ||
self.emit('logged'); | ||
callback(null, true); | ||
}); | ||
var self = this; | ||
if (this.silent) return callback(null, true); | ||
if (this.unique && this.level != level) return callback(null, true); | ||
request({ | ||
url : 'https://api.telegram.org/bot'+this.token+'/sendMessage', | ||
method : 'POST', | ||
json : { | ||
chat_id : this.chatId, | ||
text : format(this.template,{level : level, message : msg}), | ||
disable_notification : this.disableNotification | ||
} | ||
}, function(error, response, body){ | ||
if (error) { | ||
self.emit('error', error); | ||
} | ||
if (response && response.statusCode != 200) { | ||
self.emit('error', response.statusCode); | ||
} | ||
self.emit('logged'); | ||
callback(null, true); | ||
}); | ||
}; | ||
function format(string) { | ||
var args | ||
if (arguments.length === 2 && typeof arguments[1] === 'object') { | ||
args = arguments[1] | ||
} else { | ||
args = new Array(arguments.length - 1) | ||
for (var i = 1; i < arguments.length; ++i) { | ||
args[i - 1] = arguments[i] | ||
} | ||
} | ||
if (!args || !args.hasOwnProperty) { | ||
args = {} | ||
} | ||
return string.replace(nargs, function replaceArg(match, i, index) { | ||
var result | ||
if (string[index - 1] === '{' && | ||
string[index + match.length] === '}') { | ||
return i | ||
} else { | ||
result = args.hasOwnProperty(i) ? args[i] : null | ||
if (result === null || result === undefined) { | ||
return '' | ||
} | ||
return result | ||
} | ||
}) | ||
var args | ||
if (arguments.length === 2 && typeof arguments[1] === 'object') { | ||
args = arguments[1] | ||
} else { | ||
args = new Array(arguments.length - 1) | ||
for (var i = 1; i < arguments.length; ++i) { | ||
args[i - 1] = arguments[i] | ||
} | ||
} | ||
if (!args || !args.hasOwnProperty) { | ||
args = {} | ||
} | ||
return string.replace(nargs, function replaceArg(match, i, index) { | ||
var result | ||
if (string[index - 1] === '{' && | ||
string[index + match.length] === '}') { | ||
return i | ||
} else { | ||
result = args.hasOwnProperty(i) ? args[i] : null | ||
if (result === null || result === undefined) { | ||
return '' | ||
} | ||
return result | ||
} | ||
}) | ||
} |
{ | ||
"name": "winston-telegram", | ||
"description": "A Telegram transport for winston", | ||
"version": "0.4.0", | ||
"version": "1.0.0", | ||
"author": "Ivan Marban", | ||
@@ -19,3 +19,3 @@ "repository": { | ||
"dependencies": { | ||
"request": "^2.75.0" | ||
"request": "^2.79.0" | ||
}, | ||
@@ -22,0 +22,0 @@ "devDependencies": { |
@@ -30,8 +30,9 @@ # winston-telegram | ||
* __token:__ The Telegram bot authentication token. *[required]* | ||
* __chatid:__ The chatid you want to send to. *[required]* | ||
* __chatId:__ The chatid you want to send to. *[required]* | ||
* __level:__ Level of messages that this transport should log. *[optional]* *[default info]* | ||
* __unique:__ Whether to log only the declared level and none above. *[boolean]* *[optional]* | ||
* __silent:__ Whether to suppress output. *[boolean]* *[optional]* | ||
* __disable_notification:__ Sends the message silently. *[boolean]* *[optional]* | ||
* __disableNotification:__ Sends the message silently. *[boolean]* *[optional]* | ||
* __template:__ Format output message. *[optional]* | ||
* __handleExceptions:__ Handle uncaught exceptions. *[boolean]* *[optional]* | ||
@@ -44,2 +45,7 @@ String template is based on named arguments: | ||
Due applying some coding style, you must change these option properties if you're updating from lower versions to 1.0.0: | ||
- chatid to chatId | ||
- disable_notificacion to disableNotification | ||
## Examples | ||
@@ -54,3 +60,3 @@ Using the Default Logger | ||
token : 'TELEGRAM_TOKEN', | ||
chatid : 'CHAT_ID', | ||
chatId : 'CHAT_ID', | ||
level : 'error', | ||
@@ -73,3 +79,3 @@ unique : true | ||
token : 'TELEGRAM_TOKEN', | ||
chatid : 'CHAT_ID_1', | ||
chatId : 'CHAT_ID_1', | ||
level : 'error', | ||
@@ -81,6 +87,6 @@ unique : true | ||
token : 'TELEGRAM_TOKEN', | ||
chatid : 'CHAT_ID_2', | ||
chatId : 'CHAT_ID_2', | ||
level : 'info', | ||
unique : true, | ||
disable_notification: true | ||
disableNotification: true | ||
}) | ||
@@ -102,3 +108,3 @@ ] | ||
token : 'TELEGRAM_TOKEN', | ||
chatid : 'CHAT_ID', | ||
chatId : 'CHAT_ID', | ||
level : 'error', | ||
@@ -114,3 +120,28 @@ unique : true, | ||
## Change history | ||
### v1.0.0 (2016/12/05) | ||
- [#6](https://github.com/ivanmarban/winston-telegram/pull/6) Add optional handleExceptions param (@speedone) | ||
- Node.js coding style | ||
- Change option properties for matching coding style | ||
### v0.4.0 (2016/09/26) | ||
- [#5](https://github.com/ivanmarban/winston-telegram/issues/5) Add message template option | ||
- Update dependencies | ||
- Remove peer dependecies | ||
### v0.3.0 (2016/07/17) | ||
- [#2](https://github.com/ivanmarban/winston-telegram/issues/2) Allow multiple transports, send messages silently | ||
- Update dependencies | ||
### v0.2.1 (2016/03/30) | ||
- Fix typos | ||
### v0.2.0 (2016/03/08) | ||
- [#1](https://github.com/ivanmarban/winston-telegram/issues/1) Add log level option | ||
### v0.1.0 (2015/11/12) | ||
- First version | ||
[0]: https://telegram.org/ | ||
[1]: https://github.com/flatiron/winston |
@@ -7,32 +7,32 @@ /* | ||
*/ | ||
var vows = require('vows'), | ||
assert = require('assert'), | ||
winston = require('winston'), | ||
helpers = require('winston/test/helpers'), | ||
Telegram = require('../lib/winston-telegram').Telegram; | ||
var vows = require('vows'), | ||
assert = require('assert'), | ||
winston = require('winston'), | ||
helpers = require('winston/test/helpers'), | ||
Telegram = require('../lib/winston-telegram').Telegram; | ||
var TelegramTransport; | ||
TelegramTransport = new (Telegram)({ | ||
token : '177492804:AAG318J_PjC03-okUmqQV652EDbf_Rr0vTo', | ||
chatid : '-50115750' | ||
TelegramTransport = new(Telegram)({ | ||
token: '177492804:AAG318J_PjC03-okUmqQV652EDbf_Rr0vTo', | ||
chatId: '-50115750' | ||
}); | ||
function assertTelegram(transport) { | ||
assert.instanceOf(transport, Telegram); | ||
assert.isFunction(transport.log); | ||
assert.instanceOf(transport, Telegram); | ||
assert.isFunction(transport.log); | ||
} | ||
vows.describe('winston-telegram').addBatch({ | ||
'An instance of the Telegram Transport': { | ||
'when passed an options': { | ||
'should have the proper methods defined': function () { | ||
assertTelegram(TelegramTransport); | ||
}, | ||
'the log() method': helpers.testNpmLevels(TelegramTransport, 'should log messages to Telegram', function (ign, err, logged) { | ||
assert.isNull(err); | ||
assert.isTrue(logged); | ||
}) | ||
} | ||
} | ||
'An instance of the Telegram Transport': { | ||
'when passed an options': { | ||
'should have the proper methods defined': function() { | ||
assertTelegram(TelegramTransport); | ||
}, | ||
'the log() method': helpers.testNpmLevels(TelegramTransport, 'should log messages to Telegram', function(ign, err, logged) { | ||
assert.isNull(err); | ||
assert.isTrue(logged); | ||
}) | ||
} | ||
} | ||
}).export(module); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
10039
132
1
140
Updatedrequest@^2.79.0