Skip to content

Commit

Permalink
Merge branch 'release/v1.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanmarban committed Jun 3, 2017
2 parents e253bad + 382dd46 commit 19e3285
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 12 deletions.
63 changes: 57 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Options are the following:
* __disableNotification:__ Sends the message silently. *[boolean]* *[optional]*
* __template:__ Format output message. *[optional]*
* __handleExceptions:__ Handle uncaught exceptions. *[boolean]* *[optional]*
* __batchingDelay:__ Time in ms within which to batch messages together. *[integer]* *[optional]* *[default 0 or disabled]*
* __batchingSeparator:__ String with which to join batched messages with *[string]* *[default "\n\n"]*

String template is based on named arguments:
``` js
Expand All @@ -48,7 +50,7 @@ Due applying some coding style, you must change these option properties if you'r
- disable_notificacion to disableNotification

## Examples
Using the Default Logger
Using the Default Logger
``` js
var winston = require('winston');

Expand All @@ -58,7 +60,7 @@ winston.add(winston.transports.Telegram, {
token : 'TELEGRAM_TOKEN',
chatId : 'CHAT_ID',
level : 'error',
unique : true
unique : true
});

winston.log('error', 'Heeere’s Johnny!');
Expand All @@ -76,7 +78,7 @@ var logger = new (winston.Logger)({
token : 'TELEGRAM_TOKEN',
chatId : 'CHAT_ID_1',
level : 'error',
unique : true
unique : true
}),
new (winston.transports.Telegram)({
name: 'info-channel',
Expand Down Expand Up @@ -112,15 +114,61 @@ winston.log('error', 'Redrum. Redrum. Redrum.', { name: 'Danny', surname: 'Torra
//Output: [error] [Redrum. Redrum. Redrum.] [Danny] [Torrance]
```

Using batching of messages to avoid exceeding rate limits:
``` js
var winston = require('winston');

require('winston-telegram').Telegram;

winston.add(winston.transports.Telegram, {
token : 'TELEGRAM_TOKEN',
chatId : 'CHAT_ID',
level : 'info',
batchingDelay: 1000
});

// first message triggers a new batchingDelay wait
winston.info('First message: '+(new Date()).toString());
// second message is within the batchingDelay wait triggered by the first
// message, so will be batched
winston.info('Second message: '+(new Date()).toString());

setTimeout(function() {
// third message is also within the wait, so also batched
winston.info('Third message: '+(new Date()).toString());
}, 500);

setTimeout(function() {
// fourth message is not within the wait, will be sent separately
winston.info('Fourth message: '+(new Date()).toString());
}, 1500);

/*
* Output on Telegram:
* Sent at 5:22:49PM:
* [info] First message: Tue May 30 2017 17:22:47 GMT+0800 (+08)
*
* [info] Second message: Tue May 30 2017 17:22:47 GMT+0800 (+08)
*
* [info] Third message: Tue May 30 2017 17:22:47 GMT+0800 (+08)
*
* Sent at 5:22:50PM:
* [info] Fourth message: Tue May 30 2017 17:22:48 GMT+0800 (+08)
*/
```

## Change history

### v1.2.0 (2017/03/06)
- [#8](https://github.com/ivanmarban/winston-telegram/pull/8) Add batching of messages sent within a certain interval ([@JustinOng][4])

### v1.1.0 (2017/05/02)
- [#7](https://github.com/ivanmarban/winston-telegram/pull/7) Use metadata information in messages (@alberto467)
- [#7](https://github.com/ivanmarban/winston-telegram/pull/7) Replace built-in format function by sf node module (@alberto467)
- [#7](https://github.com/ivanmarban/winston-telegram/pull/7) Use metadata information in messages ([@alberto467][3])
- [#7](https://github.com/ivanmarban/winston-telegram/pull/7) Replace built-in format function by sf node module ([@alberto467][3])
- Update dependencies

### v1.0.0 (2016/12/05)
- [#6](https://github.com/ivanmarban/winston-telegram/pull/6) Add optional handleExceptions param (@speedone)
- [#6](https://github.com/ivanmarban/winston-telegram/pull/6) Add optional handleExceptions param ([@speedone][2])
- Node.js coding style
- Change option properties for matching coding style

Expand All @@ -144,3 +192,6 @@ winston.log('error', 'Redrum. Redrum. Redrum.', { name: 'Danny', surname: 'Torra

[0]: https://telegram.org/
[1]: https://github.com/flatiron/winston
[2]: https://github.com/speedone
[3]: https://github.com/alberto467
[4]: https://github.com/JustinOng
45 changes: 40 additions & 5 deletions lib/winston-telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var winston = require('winston');
var format = require('sf');

/**
* @constructs
* @constructs
* @param {object} options
* @param {String} options.token Telegram bot authentication token
* @param {String} options.chatId Telegram unique identifier for chat
Expand All @@ -30,6 +30,11 @@ var Telegram = exports.Telegram = function (options) {
this.disableNotification = options.disableNotification || false;
this.name = options.name || this.name;
this.template = options.template || '[{level}] {message}';
this.batchingDelay = options.batchingDelay || 0;
this.batchingSeparator = options.batchingSeparator || "\n\n";

this.batchedMessages = [];
this.batchingTimeout = 0;
};

/** @extends winston.Transport */
Expand Down Expand Up @@ -59,13 +64,44 @@ 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);


var messageText = format(this.template,{level : level, message : msg, metadata : meta});

if (this.batchingDelay) {
this.batchedMessages.push(messageText);

if (!this.batchingTimeout) {
this.batchingTimeout = setTimeout(function() {
var combinedMessages = self.batchedMessages.join(self.batchingSeparator);
self.send(combinedMessages);

self.batchedMessages = [];
self.batchingTimeout = 0;
}, this.batchingDelay);
}
}
else {
self.send(messageText);
}

callback(null, true);
};

/**
* Actual method that sends the given message to Telegram
* @function send
* @member Telegram
* @param messageText {string} Formatted text to log
*/
Telegram.prototype.send = function (messageText) {
var self = this;

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, metadata : meta}),
text : messageText,
disable_notification : this.disableNotification
}
}, function(error, response, body){
Expand All @@ -76,6 +112,5 @@ Telegram.prototype.log = function (level, msg, meta, callback) {
self.emit('error', response.statusCode);
}
self.emit('logged');
callback(null, true);
});
};
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "winston-telegram",
"description": "A Telegram transport for winston",
"version": "1.1.0",
"version": "1.2.0",
"author": "Ivan Marban",
"repository": {
"type": "git",
Expand Down

0 comments on commit 19e3285

Please sign in to comment.