From 28d68f77c639c51a3654aa40dda309968176c50f Mon Sep 17 00:00:00 2001 From: Andrew <5755685+arnidan@users.noreply.github.com> Date: Sat, 4 Sep 2021 17:39:41 +0200 Subject: [PATCH 1/2] pass full data to formatMessage method --- lib/winston-telegram.js | 2 +- test/winston-telegram.test.js | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/winston-telegram.js b/lib/winston-telegram.js index 6df90fa..aaf6174 100644 --- a/lib/winston-telegram.js +++ b/lib/winston-telegram.js @@ -75,7 +75,7 @@ module.exports = class Telegram extends Transport { } if (this.formatMessage) { - messageText = this.formatMessage(formatOptions) + messageText = this.formatMessage(formatOptions, info) } else { messageText = format(this.template, formatOptions) } diff --git a/test/winston-telegram.test.js b/test/winston-telegram.test.js index cc3ff7c..da2e3a9 100644 --- a/test/winston-telegram.test.js +++ b/test/winston-telegram.test.js @@ -211,17 +211,23 @@ describe('winston-telegram', function () { token: 'foo', chatId: 'bar', level: 'error', - formatMessage: function (options) { + formatMessage: function (options, info) { let message = options.message + if (options.level === 'error') { message = '[Error] ' + message } + + if (info.superUrgent) { + message += '!!!' + } + return message } }) ) - winston.error('Some error!!') - assert.strictEqual(JSON.parse(spy.getCalls()[0].args[1]).text, '[Error] Some error!!') + winston.error('Some error', {superUrgent: true}) + assert.strictEqual(JSON.parse(spy.getCalls()[0].args[1]).text, '[Error] Some error!!!') assert.ok(spy.callCount === 1) done() }) From 2b630efbb91d15345db1d9ecb76802b3eec72986 Mon Sep 17 00:00:00 2001 From: Andrew <5755685+arnidan@users.noreply.github.com> Date: Sun, 5 Sep 2021 16:58:20 +0200 Subject: [PATCH 2/2] update ts typings --- lib/winston-telegram.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/winston-telegram.d.ts b/lib/winston-telegram.d.ts index a7c8c1a..0289715 100644 --- a/lib/winston-telegram.d.ts +++ b/lib/winston-telegram.d.ts @@ -25,7 +25,7 @@ declare namespace WinstonTelegram { /** Format output message. (default "[{level}] [message]") */ template?: string /** Format output message by own method. */ - formatMessage?: (params: WinstonTelegram.FormatOptions) => string + formatMessage?: (params: WinstonTelegram.FormatOptions, info: any) => string /** Handle uncaught exceptions. (default true) */ handleExceptions?: boolean /** Time in ms within which to batch messages together. (default = 0) (0 = disabled) */