From e9c63576c17678438db77529ed7ca72b61ef2a09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ge=C3=B6rger?= Date: Fri, 14 Oct 2016 23:49:15 +0200 Subject: [PATCH 1/6] add optional handleExceptions param --- lib/winston-telegram.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/winston-telegram.js b/lib/winston-telegram.js index 2ea86cc..995c188 100644 --- a/lib/winston-telegram.js +++ b/lib/winston-telegram.js @@ -24,6 +24,7 @@ var Telegram = exports.Telegram = function (options) { 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.disable_notification = options.disable_notification || false; From aeab8303a440d9942d68478b76a4f0fd0d32de9a Mon Sep 17 00:00:00 2001 From: Ivan Marban Date: Thu, 1 Dec 2016 15:42:17 +0100 Subject: [PATCH 2/6] Coding style: two spaces for indenting --- lib/winston-telegram.js | 126 +++++++++++++++++----------------- test/winston-telegram-test.js | 42 ++++++------ 2 files changed, 84 insertions(+), 84 deletions(-) diff --git a/lib/winston-telegram.js b/lib/winston-telegram.js index 995c188..b9a1117 100644 --- a/lib/winston-telegram.js +++ b/lib/winston-telegram.js @@ -5,9 +5,9 @@ * MIT LICENCE */ -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; /** @@ -17,19 +17,19 @@ var nargs = /\{([0-9a-zA-Z_]+)\}/g; * @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.handleExceptions = options.handleExceptions || false; - 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.disable_notification = options.disable_notification || false; + this.name = options.name || this.name; + this.template = options.template || '[{level}] {message}'; }; /** @extends winston.Transport */ @@ -56,54 +56,54 @@ Telegram.prototype.name = 'telegram'; * @param callback {function} Continuation to respond to when complete. */ 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.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); + }); }; 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 + } + }) } \ No newline at end of file diff --git a/test/winston-telegram-test.js b/test/winston-telegram-test.js index 5b91ff7..56b4675 100644 --- a/test/winston-telegram-test.js +++ b/test/winston-telegram-test.js @@ -4,34 +4,34 @@ * (C) 2015 Ivan Marban * MIT LICENSE */ -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); \ No newline at end of file From ae0ad159b70c96a6dfb696e9f3eb6b486c2baebe Mon Sep 17 00:00:00 2001 From: Ivan Marban Date: Thu, 1 Dec 2016 15:44:09 +0100 Subject: [PATCH 3/6] Coding style: lowerCamelCase for variables --- lib/winston-telegram.js | 14 +++++++------- test/winston-telegram-test.js | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/winston-telegram.js b/lib/winston-telegram.js index b9a1117..7be4231 100644 --- a/lib/winston-telegram.js +++ b/lib/winston-telegram.js @@ -14,20 +14,20 @@ var nargs = /\{([0-9a-zA-Z_]+)\}/g; * @constructs * @param {object} options * @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'); + if (!options.token || !options.chatId){ + throw new Error('winston-telegram requires \'token\' and \'chatId\' property'); } this.token = options.token; - this.chatid = options.chatid; + 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.disable_notification = options.disable_notification || false; + this.disableNotification = options.disableNotification || false; this.name = options.name || this.name; this.template = options.template || '[{level}] {message}'; }; @@ -64,9 +64,9 @@ Telegram.prototype.log = function (level, msg, meta, callback) { url : 'https://api.telegram.org/bot'+this.token+'/sendMessage', method : 'POST', json : { - chat_id : this.chatid, + chat_id : this.chatId, text : format(this.template,{level : level, message : msg}), - disable_notification : this.disable_notification + disable_notification : this.disableNotification } }, function(error, response, body){ if (error) { diff --git a/test/winston-telegram-test.js b/test/winston-telegram-test.js index 56b4675..fc2429a 100644 --- a/test/winston-telegram-test.js +++ b/test/winston-telegram-test.js @@ -14,7 +14,7 @@ var TelegramTransport; TelegramTransport = new(Telegram)({ token: '177492804:AAG318J_PjC03-okUmqQV652EDbf_Rr0vTo', - chatid: '-50115750' + chatId: '-50115750' }); function assertTelegram(transport) { From 51bbdd9c4a6f3c36ae5ab2f95cb75c39d57d5268 Mon Sep 17 00:00:00 2001 From: Ivan Marban Date: Thu, 1 Dec 2016 15:46:55 +0100 Subject: [PATCH 4/6] Update dependencies --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6ee21b5..88ec15f 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "bot" ], "dependencies": { - "request": "^2.75.0" + "request": "^2.79.0" }, "devDependencies": { "winston": "", From 21051f5bccbf94819c183b017f93ee1e5c8d5788 Mon Sep 17 00:00:00 2001 From: Ivan Marban Date: Mon, 5 Dec 2016 09:22:07 +0100 Subject: [PATCH 5/6] Update README --- README.md | 45 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index cc618e7..2c062f4 100644 --- a/README.md +++ b/README.md @@ -27,12 +27,13 @@ winston.add(winston.transports.Telegram, options); Options are the following: * __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]* String template is based on named arguments: ``` js @@ -40,6 +41,11 @@ String template is based on named arguments: '{message}' -> text of messages ``` +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 Using the Default Logger ``` js @@ -49,7 +55,7 @@ require('winston-telegram').Telegram; winston.add(winston.transports.Telegram, { token : 'TELEGRAM_TOKEN', - chatid : 'CHAT_ID', + chatId : 'CHAT_ID', level : 'error', unique : true }); @@ -67,17 +73,17 @@ var logger = new (winston.Logger)({ new (winston.transports.Telegram)({ name: 'error-channel', token : 'TELEGRAM_TOKEN', - chatid : 'CHAT_ID_1', + chatId : 'CHAT_ID_1', level : 'error', unique : true }), new (winston.transports.Telegram)({ name: 'info-channel', token : 'TELEGRAM_TOKEN', - chatid : 'CHAT_ID_2', + chatId : 'CHAT_ID_2', level : 'info', unique : true, - disable_notification: true + disableNotification: true }) ] }); @@ -94,7 +100,7 @@ require('winston-telegram').Telegram; winston.add(winston.transports.Telegram, { token : 'TELEGRAM_TOKEN', - chatid : 'CHAT_ID', + chatId : 'CHAT_ID', level : 'error', unique : true, template : '[{level}] [{message}]' @@ -105,5 +111,30 @@ winston.log('error', 'Redrum. Redrum. Redrum.'); //Output: [error] [Redrum. Redrum. Redrum.] ``` +## 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 \ No newline at end of file From c03d71028377389de5df71eb5d057f4170c17559 Mon Sep 17 00:00:00 2001 From: Ivan Marban Date: Mon, 5 Dec 2016 09:23:44 +0100 Subject: [PATCH 6/6] Bump version number --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 88ec15f..b6f6aa1 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "winston-telegram", "description": "A Telegram transport for winston", - "version": "0.4.0", + "version": "1.0.0", "author": "Ivan Marban", "repository": { "type": "git",