Skip to content

Commit

Permalink
Merge branch 'release/v1.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanmarban committed May 3, 2018
2 parents 7edf89e + 4cbcbb8 commit ccbccff
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ build/Release
# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

.idea
35 changes: 33 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ Options are the following:
* __unique:__ Whether to log only the declared level and none above. *[boolean]* *[optional]*
* __silent:__ Whether to suppress output. *[boolean]* *[optional]*
* __disableNotification:__ Sends the message silently. *[boolean]* *[optional]*
* __template:__ Format output message. *[optional]*
* __template:__ Format output message. *[string]* *[optional]*
* __formatMessage:__ Format output message by own method. *[function]* *[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"]*
Expand Down Expand Up @@ -114,6 +115,32 @@ winston.log('error', 'Redrum. Redrum. Redrum.', { name: 'Danny', surname: 'Torra
//Output: [error] [Redrum. Redrum. Redrum.] [Danny] [Torrance]
```

Using custom format message:
```js
var winston = require('winston');

require('winston-telegram').Telegram;

winston.add(winston.transports.Telegram, {
token : 'TELEGRAM_TOKEN',
chatId : 'CHAT_ID',
level : 'error',
unique : true,
formatMessage : function(opts) {
var message = opts.message;

if (opts.level === 'warn') {
message += '[Warning] ';
}
return message;
}
});

winston.warn('Some warning!!');

//Output: [Warning] Some warning!!
```

Using batching of messages to avoid exceeding rate limits:
``` js
var winston = require('winston');
Expand Down Expand Up @@ -159,6 +186,9 @@ setTimeout(function() {

## Change history

### v1.3.0 (2018/05/03)
- [#10](https://github.com/ivanmarban/winston-telegram/pull/10) Add formatMessage property ([@noveogroup-amorgunov][6])

### v1.2.1 (2017/07/26)
- [#9](https://github.com/ivanmarban/winston-telegram/pull/9) Add error description in case of error ([@dutu][5])
- Update sf library
Expand Down Expand Up @@ -199,4 +229,5 @@ setTimeout(function() {
[2]: https://github.com/speedone
[3]: https://github.com/alberto467
[4]: https://github.com/JustinOng
[5]: https://github.com/dutu
[5]: https://github.com/dutu
[6]: https://github.com/noveogroup-amorgunov
14 changes: 13 additions & 1 deletion lib/winston-telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ var Telegram = exports.Telegram = function (options) {
if (!options.token || !options.chatId){
throw new Error('winston-telegram requires \'token\' and \'chatId\' property');
}
if (options.formatMessage && typeof options.formatMessage !== 'function'){
throw new Error('winston-telegram \'formatMessage\' property should be function');
}

this.token = options.token;
this.chatId = options.chatId;
this.level = options.level || 'info';
Expand All @@ -30,6 +34,7 @@ var Telegram = exports.Telegram = function (options) {
this.disableNotification = options.disableNotification || false;
this.name = options.name || this.name;
this.template = options.template || '[{level}] {message}';
this.formatMessage = options.formatMessage;
this.batchingDelay = options.batchingDelay || 0;
this.batchingSeparator = options.batchingSeparator || "\n\n";

Expand Down Expand Up @@ -65,7 +70,14 @@ Telegram.prototype.log = function (level, msg, meta, callback) {
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});
var messageText = null;
var formatOptions = {level : level, message : msg, metadata : meta};

if (this.formatMessage) {
messageText = this.formatMessage(formatOptions)
} else {
messageText = format(this.template, formatOptions)
}

if (this.batchingDelay) {
this.batchedMessages.push(messageText);
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.2.1",
"version": "1.3.0",
"author": "Ivan Marban",
"repository": {
"type": "git",
Expand Down

0 comments on commit ccbccff

Please sign in to comment.