Skip to content

Commit

Permalink
Merge branch 'release/v1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanmarban committed May 2, 2017
2 parents 59acdc5 + 995d93e commit e253bad
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 41 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ String template is based on named arguments:
``` js
'{level}' -> level of messages
'{message}' -> text of messages
'{metadata}' -> metadata object of messages
```

Due applying some coding style, you must change these option properties if you're updating from lower versions to 1.0.0:
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
Expand Down Expand Up @@ -103,16 +104,21 @@ winston.add(winston.transports.Telegram, {
chatId : 'CHAT_ID',
level : 'error',
unique : true,
template : '[{level}] [{message}]'
template : '[{level}] [{message}] [{metadata.name}] [{metadata.surname}]'
});

winston.log('error', 'Redrum. Redrum. Redrum.');
winston.log('error', 'Redrum. Redrum. Redrum.', { name: 'Danny', surname: 'Torrance' });

//Output: [error] [Redrum. Redrum. Redrum.]
//Output: [error] [Redrum. Redrum. Redrum.] [Danny] [Torrance]
```

## Change history

### 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)
- Update dependencies

### v1.0.0 (2016/12/05)
- [#6](https://github.com/ivanmarban/winston-telegram/pull/6) Add optional handleExceptions param (@speedone)
- Node.js coding style
Expand All @@ -137,4 +143,4 @@ winston.log('error', 'Redrum. Redrum. Redrum.');
- First version

[0]: https://telegram.org/
[1]: https://github.com/flatiron/winston
[1]: https://github.com/flatiron/winston
40 changes: 6 additions & 34 deletions lib/winston-telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
* MIT LICENCE
*/

var util = require('util');
var request = require('request');
var winston = require('winston');
var nargs = /\{([0-9a-zA-Z_]+)\}/g;
var util = require('util');
var request = require('request');
var winston = require('winston');
var format = require('sf');

/**
* @constructs
Expand All @@ -21,7 +21,7 @@ var Telegram = exports.Telegram = function (options) {
if (!options.token || !options.chatId){
throw new Error('winston-telegram requires \'token\' and \'chatId\' property');
}
this.token = options.token;
this.token = options.token;
this.chatId = options.chatId;
this.level = options.level || 'info';
this.handleExceptions = options.handleExceptions || false;
Expand Down Expand Up @@ -65,7 +65,7 @@ Telegram.prototype.log = function (level, msg, meta, callback) {
method : 'POST',
json : {
chat_id : this.chatId,
text : format(this.template,{level : level, message : msg}),
text : format(this.template,{level : level, message : msg, metadata : meta}),
disable_notification : this.disableNotification
}
}, function(error, response, body){
Expand All @@ -79,31 +79,3 @@ Telegram.prototype.log = function (level, msg, meta, callback) {
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
}
})
}
5 changes: 3 additions & 2 deletions 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.0.0",
"version": "1.1.0",
"author": "Ivan Marban",
"repository": {
"type": "git",
Expand All @@ -16,7 +16,8 @@
"bot"
],
"dependencies": {
"request": "^2.79.0"
"request": "^2.81.0",
"sf": "^0.1.8"
},
"devDependencies": {
"winston": "",
Expand Down

0 comments on commit e253bad

Please sign in to comment.