From d129949739e898696a2808103ff86b4dd614a208 Mon Sep 17 00:00:00 2001 From: alberto467 Date: Fri, 28 Apr 2017 21:23:23 +0200 Subject: [PATCH 1/8] Use meta information in messages You can now use meta information and send messages like: "[{level}] {message}{meta.url} ERROR: {meta.err}". To access the object properties and a lot more the sf node module is used to format the messages instead of the built-in format function. --- lib/winston-telegram.js | 31 ++----------------------------- 1 file changed, 2 insertions(+), 29 deletions(-) diff --git a/lib/winston-telegram.js b/lib/winston-telegram.js index 7be4231..9689208 100644 --- a/lib/winston-telegram.js +++ b/lib/winston-telegram.js @@ -8,6 +8,7 @@ var util = require('util'); var request = require('request'); var winston = require('winston'); +var format = require('sf'); var nargs = /\{([0-9a-zA-Z_]+)\}/g; /** @@ -65,7 +66,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, meta : meta}), disable_notification : this.disableNotification } }, function(error, response, body){ @@ -79,31 +80,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 - } - }) -} \ No newline at end of file From 488104df339f75ad9a8c66656db6c965e56376e9 Mon Sep 17 00:00:00 2001 From: alberto467 Date: Fri, 28 Apr 2017 21:25:29 +0200 Subject: [PATCH 2/8] Added sf dependency --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index b6f6aa1..fbd7f98 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,8 @@ "bot" ], "dependencies": { - "request": "^2.79.0" + "request": "^2.79.0", + "sf": "^0.1.8" }, "devDependencies": { "winston": "", From 1f7564a20b850c30c980f43fe7e15b6f2df59889 Mon Sep 17 00:00:00 2001 From: alberto467 Date: Fri, 28 Apr 2017 21:32:34 +0200 Subject: [PATCH 3/8] Updated documentation --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2c062f4..c8949ee 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ String template is based on named arguments: ``` js '{level}' -> level of messages '{message}' -> text of messages +'{meta}' -> meta 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: @@ -103,12 +104,12 @@ winston.add(winston.transports.Telegram, { chatId : 'CHAT_ID', level : 'error', unique : true, - template : '[{level}] [{message}]' + template : '[{level}] [{message}] [{meta.property1}] [{meta.property2}]' }); -winston.log('error', 'Redrum. Redrum. Redrum.'); +winston.log('error', 'Redrum. Redrum. Redrum.', { property1: 'foo', property2: 'bar' }); -//Output: [error] [Redrum. Redrum. Redrum.] +//Output: [error] [Redrum. Redrum. Redrum.] [foo] [bar] ``` ## Change history @@ -137,4 +138,4 @@ winston.log('error', 'Redrum. Redrum. Redrum.'); - First version [0]: https://telegram.org/ -[1]: https://github.com/flatiron/winston \ No newline at end of file +[1]: https://github.com/flatiron/winston From a632861df6f87f00f000e120c50c0317242b9998 Mon Sep 17 00:00:00 2001 From: Ivan Marban Date: Tue, 2 May 2017 08:50:39 +0200 Subject: [PATCH 4/8] Update dependencies --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index fbd7f98..c645b17 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "bot" ], "dependencies": { - "request": "^2.79.0", + "request": "^2.81.0", "sf": "^0.1.8" }, "devDependencies": { From 76edd1c2b54b0dd1191e083e6a2ac371fcce2fad Mon Sep 17 00:00:00 2001 From: Ivan Marban Date: Tue, 2 May 2017 08:51:21 +0200 Subject: [PATCH 5/8] Code refactoring --- lib/winston-telegram.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/winston-telegram.js b/lib/winston-telegram.js index 9689208..db6c0c4 100644 --- a/lib/winston-telegram.js +++ b/lib/winston-telegram.js @@ -5,11 +5,10 @@ * 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 format = require('sf'); -var nargs = /\{([0-9a-zA-Z_]+)\}/g; /** * @constructs @@ -22,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; @@ -66,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, meta : meta}), + text : format(this.template,{level : level, message : msg, metadata : meta}), disable_notification : this.disableNotification } }, function(error, response, body){ From e8d9c994c12c25d5617f98301a67a0f1cf1970a8 Mon Sep 17 00:00:00 2001 From: Ivan Marban Date: Tue, 2 May 2017 08:51:42 +0200 Subject: [PATCH 6/8] Update README --- README.md | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c8949ee..6aa2c22 100644 --- a/README.md +++ b/README.md @@ -39,10 +39,10 @@ String template is based on named arguments: ``` js '{level}' -> level of messages '{message}' -> text of messages -'{meta}' -> meta object 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 @@ -104,16 +104,21 @@ winston.add(winston.transports.Telegram, { chatId : 'CHAT_ID', level : 'error', unique : true, - template : '[{level}] [{message}] [{meta.property1}] [{meta.property2}]' + template : '[{level}] [{message}] [{meta.name}] [{meta.surname}]' }); -winston.log('error', 'Redrum. Redrum. Redrum.', { property1: 'foo', property2: 'bar' }); +winston.log('error', 'Redrum. Redrum. Redrum.', { name: 'Danny', surname: 'Torrance' }); -//Output: [error] [Redrum. Redrum. Redrum.] [foo] [bar] +//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 From d7a0d5625145ff6d05c8a80079c8f3edd022808a Mon Sep 17 00:00:00 2001 From: Ivan Marban Date: Tue, 2 May 2017 08:52:57 +0200 Subject: [PATCH 7/8] Update README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6aa2c22..91bd792 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,7 @@ winston.add(winston.transports.Telegram, { chatId : 'CHAT_ID', level : 'error', unique : true, - template : '[{level}] [{message}] [{meta.name}] [{meta.surname}]' + template : '[{level}] [{message}] [{metadata.name}] [{metadata.surname}]' }); winston.log('error', 'Redrum. Redrum. Redrum.', { name: 'Danny', surname: 'Torrance' }); From 995d93e6389c4aa14ba6d61a39b719da3eec7a7c Mon Sep 17 00:00:00 2001 From: Ivan Marban Date: Tue, 2 May 2017 09:37:46 +0200 Subject: [PATCH 8/8] Bump version number --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c645b17..1481bfa 100644 --- a/package.json +++ b/package.json @@ -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",