Skip to content

Commit

Permalink
Merge branch 'release/v0.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanmarban committed Sep 26, 2016
2 parents 3d671ee + f4693d9 commit 082805a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ Options are the following:
* __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]*
* __template:__ Format output message. *[optional]*

String template is based on named arguments:
``` js
'{level}' -> level of messages
'{message}' -> text of messages
```

## Examples
Using the Default Logger
Expand Down Expand Up @@ -79,5 +86,24 @@ logger.error('All work and no play makes Jack a dull boy.');
logger.info('Come play with us, Danny. Forever... and ever... and ever.');
```

Using template output:
``` js
var winston = require('winston');

require('winston-telegram').Telegram;

winston.add(winston.transports.Telegram, {
token : 'TELEGRAM_TOKEN',
chatid : 'CHAT_ID',
level : 'error',
unique : true,
template : '[{level}] [{message}]'
});

winston.log('error', 'Redrum. Redrum. Redrum.');

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

[0]: https://telegram.org/
[1]: https://github.com/flatiron/winston
34 changes: 32 additions & 2 deletions lib/winston-telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
var util = require('util');
var request = require('request');
var winston = require('winston');
var nargs = /\{([0-9a-zA-Z_]+)\}/g;

/**
* @constructs
Expand All @@ -27,6 +28,7 @@ var Telegram = exports.Telegram = function (options) {
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 */
Expand Down Expand Up @@ -62,7 +64,7 @@ Telegram.prototype.log = function (level, msg, meta, callback) {
method : 'POST',
json : {
chat_id : this.chatid,
text : '['+level+'] '+msg,
text : format(this.template,{level : level, message : msg}),
disable_notification : this.disable_notification
}
}, function(error, response, body){
Expand All @@ -75,4 +77,32 @@ Telegram.prototype.log = function (level, msg, meta, callback) {
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
}
})
}
7 changes: 2 additions & 5 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": "0.3.0",
"version": "0.4.0",
"author": "Ivan Marban",
"repository": {
"type": "git",
Expand All @@ -16,12 +16,9 @@
"bot"
],
"dependencies": {
"request": "^2.73.0"
"request": "^2.75.0"
},
"devDependencies": {
"winston": ""
},
"peerDependencies": {
"winston": "",
"vows": ""
},
Expand Down

0 comments on commit 082805a

Please sign in to comment.