From bef8fc0a24ed473f1a2426eea2a7bcd9e73f7a24 Mon Sep 17 00:00:00 2001 From: Alexander Morgunov Date: Mon, 30 Apr 2018 16:30:24 +0700 Subject: [PATCH 1/2] Add formatMessage property --- .gitignore | 2 ++ README.md | 29 ++++++++++++++++++++++++++++- lib/winston-telegram.js | 14 +++++++++++++- package-lock.json | 6 +++--- 4 files changed, 46 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 123ae94..d532efc 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/README.md b/README.md index 2160256..fa2148b 100644 --- a/README.md +++ b/README.md @@ -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"]* @@ -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'); diff --git a/lib/winston-telegram.js b/lib/winston-telegram.js index ad86011..44d31b9 100644 --- a/lib/winston-telegram.js +++ b/lib/winston-telegram.js @@ -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'; @@ -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"; @@ -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); diff --git a/package-lock.json b/package-lock.json index 903dc7e..7d45590 100644 --- a/package-lock.json +++ b/package-lock.json @@ -497,9 +497,9 @@ } }, "winston": { - "version": "2.3.1", - "resolved": "https://registry.npmjs.org/winston/-/winston-2.3.1.tgz", - "integrity": "sha1-C0hCDZeMAYBM8CMLZIhhWYIloRk=", + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/winston/-/winston-2.4.2.tgz", + "integrity": "sha512-4S/Ad4ZfSNl8OccCLxnJmNISWcm2joa6Q0YGDxlxMzH0fgSwWsjMt+SmlNwCqdpaPg3ev1HKkMBsIiXeSUwpbA==", "requires": { "async": "1.0.0", "colors": "1.0.3", From 4cbcbb8bacc96d61f6b5f2b7aaf666f4b8dcea62 Mon Sep 17 00:00:00 2001 From: Ivan Marban Date: Thu, 3 May 2018 20:10:29 +0200 Subject: [PATCH 2/2] Bump version number. Update README --- README.md | 6 +++++- package-lock.json | 2 +- package.json | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index fa2148b..17b102f 100644 --- a/README.md +++ b/README.md @@ -186,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 @@ -226,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 \ No newline at end of file +[5]: https://github.com/dutu +[6]: https://github.com/noveogroup-amorgunov \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 7d45590..f73b9f3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "winston-telegram", - "version": "1.2.1", + "version": "1.3.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 69757d8..af788b9 100644 --- a/package.json +++ b/package.json @@ -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",