winston-telegram
Advanced tools
Comparing version 1.3.1 to 2.0.0
@@ -8,6 +8,5 @@ /* | ||
var util = require('util'); | ||
var request = require('request'); | ||
var winston = require('winston'); | ||
var format = require('sf'); | ||
const request = require('request'); | ||
const format = require('sf'); | ||
const Transport = require('winston-transport'); | ||
@@ -20,111 +19,98 @@ /** | ||
*/ | ||
var Telegram = exports.Telegram = function (options) { | ||
options = options || {}; | ||
if (!options.token || !options.chatId){ | ||
throw new Error('winston-telegram requires \'token\' and \'chatId\' property'); | ||
module.exports = class Telegram extends Transport { | ||
constructor(options) { | ||
super(options); | ||
options = 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'; | ||
this.handleExceptions = options.handleExceptions || false; | ||
this.unique = options.unique || false; | ||
this.silent = options.silent || false; | ||
this.disableNotification = options.disableNotification || false; | ||
this.name = options.name || this.name || 'winston-telegram'; | ||
this.template = options.template || '[{level}] {message}'; | ||
this.formatMessage = options.formatMessage; | ||
this.batchingDelay = options.batchingDelay || 0; | ||
this.batchingSeparator = options.batchingSeparator || "\n\n"; | ||
this.batchedMessages = []; | ||
this.batchingTimeout = 0; | ||
} | ||
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'; | ||
this.handleExceptions = options.handleExceptions || false; | ||
this.unique = options.unique || false; | ||
this.silent = options.silent || false; | ||
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"; | ||
/** | ||
* Core logging method exposed to Winston. | ||
* @function log | ||
* @param info {Object} info - TODO: add param description. | ||
* @param callback {function} callback - TODO: add param description. | ||
*/ | ||
log(info, callback) { | ||
let self = this; | ||
if (this.silent) return callback(null, true); | ||
if (this.unique && this.level != info.level) return callback(null, true); | ||
this.batchedMessages = []; | ||
this.batchingTimeout = 0; | ||
}; | ||
let messageText = null; | ||
let formatOptions = {level : info.level, message : info.message, metadata : info.metadata}; | ||
/** @extends winston.Transport */ | ||
util.inherits(Telegram, winston.Transport); | ||
if (this.formatMessage) { | ||
messageText = this.formatMessage(formatOptions) | ||
} else { | ||
messageText = format(this.template, formatOptions) | ||
} | ||
/** | ||
* Define a getter so that `winston.transports.Telegram` | ||
* is available and thus backwards compatible. | ||
*/ | ||
winston.transports.Telegram = Telegram; | ||
if (this.batchingDelay) { | ||
this.batchedMessages.push(messageText); | ||
/** | ||
* Expose the name of this Transport on the prototype | ||
*/ | ||
Telegram.prototype.name = 'telegram'; | ||
if (!this.batchingTimeout) { | ||
this.batchingTimeout = setTimeout(function() { | ||
let combinedMessages = self.batchedMessages.join(self.batchingSeparator); | ||
self.send(combinedMessages); | ||
/** | ||
* Core logging method exposed to Winston. | ||
* @function log | ||
* @member Telegram | ||
* @param {string} level Level at which to log the message | ||
* @param {string} msg Message to log | ||
* @param {Object} meta **Optional** Additional metadata to attach | ||
* @param {function} callback Continuation to respond to when complete. | ||
*/ | ||
Telegram.prototype.log = function (level, msg, meta, callback) { | ||
var self = this; | ||
if (this.silent) return callback(null, true); | ||
if (this.unique && this.level != level) return callback(null, true); | ||
self.batchedMessages = []; | ||
self.batchingTimeout = 0; | ||
}, this.batchingDelay); | ||
} | ||
} | ||
else { | ||
self.send(messageText); | ||
} | ||
var messageText = null; | ||
var formatOptions = {level : level, message : msg, metadata : meta}; | ||
if (this.formatMessage) { | ||
messageText = this.formatMessage(formatOptions) | ||
} else { | ||
messageText = format(this.template, formatOptions) | ||
callback(null, true); | ||
} | ||
if (this.batchingDelay) { | ||
this.batchedMessages.push(messageText); | ||
/** | ||
* Actual method that sends the given message to Telegram. | ||
* @function send | ||
* @param messageText {string} formatted text to log. | ||
*/ | ||
send(messageText) { | ||
let self = this; | ||
if (!this.batchingTimeout) { | ||
this.batchingTimeout = setTimeout(function() { | ||
var combinedMessages = self.batchedMessages.join(self.batchingSeparator); | ||
self.send(combinedMessages); | ||
self.batchedMessages = []; | ||
self.batchingTimeout = 0; | ||
}, this.batchingDelay); | ||
} | ||
request({ | ||
url : 'https://api.telegram.org/bot'+this.token+'/sendMessage', | ||
method : 'POST', | ||
json : { | ||
chat_id : this.chatId, | ||
text : messageText, | ||
disable_notification : this.disableNotification | ||
} | ||
}, function(error, response, body){ | ||
if (error) { | ||
self.emit('error', error); | ||
} | ||
if (response && response.statusCode != 200) { | ||
self.emit('error', response.statusCode + (body && body.description && (': ' + body.description) || '')); | ||
} | ||
self.emit('logged'); | ||
}); | ||
} | ||
else { | ||
self.send(messageText); | ||
} | ||
callback(null, true); | ||
}; | ||
/** | ||
* Actual method that sends the given message to Telegram | ||
* @function send | ||
* @member Telegram | ||
* @param {string} messageText Formatted text to log | ||
*/ | ||
Telegram.prototype.send = function (messageText) { | ||
var self = this; | ||
request({ | ||
url : 'https://api.telegram.org/bot'+this.token+'/sendMessage', | ||
method : 'POST', | ||
json : { | ||
chat_id : this.chatId, | ||
text : messageText, | ||
disable_notification : this.disableNotification | ||
} | ||
}, function(error, response, body){ | ||
if (error) { | ||
self.emit('error', error); | ||
} | ||
if (response && response.statusCode != 200) { | ||
self.emit('error', response.statusCode + (body && body.description && (': ' + body.description) || '')); | ||
} | ||
self.emit('logged'); | ||
}); | ||
} | ||
} |
{ | ||
"name": "winston-telegram", | ||
"description": "A Telegram transport for winston", | ||
"version": "1.3.1", | ||
"version": "2.0.0", | ||
"author": "Ivan Marban", | ||
@@ -23,9 +23,5 @@ "repository": { | ||
"devDependencies": { | ||
"winston": "2.4.4", | ||
"vows": "" | ||
"winston": "^3.0.0" | ||
}, | ||
"main": "./lib/winston-telegram", | ||
"scripts": { | ||
"test": "vows test/*test.js --spec" | ||
}, | ||
"engines": { | ||
@@ -32,0 +28,0 @@ "node": ">= 0.10.0" |
170
README.md
@@ -7,20 +7,19 @@ # winston-telegram | ||
## Installation | ||
## winston-telegram@2 | ||
Installation: | ||
``` sh | ||
$ npm install winston@2 | ||
$ npm install winston-telegram@1 | ||
$ npm install winston@3 | ||
$ npm install winston-telegram@2 | ||
``` | ||
## Looking for `winston-telegram@1.x` ? | ||
Documentation below is for `winston-telegram@2`. [Read the `winston-telegram@1.x` documentation][8]. | ||
## Usage | ||
``` js | ||
var winston = require('winston'); | ||
const logger = require('winston'); | ||
const telegramLogger = require('winston-telegram'); | ||
/* | ||
* Requiring `winston-telegram` will expose | ||
* `winston.transports.Telegram` | ||
*/ | ||
require('winston-telegram').Telegram; | ||
winston.add(winston.transports.Telegram, options); | ||
logger.add(new telegramLogger({options})); | ||
``` | ||
@@ -57,41 +56,40 @@ | ||
``` js | ||
var winston = require('winston'); | ||
const logger = require('winston'); | ||
const telegramLogger = require('winston-telegram'); | ||
require('winston-telegram').Telegram; | ||
logger.add(new telegramLogger({ | ||
token: 'TELEGRAM_TOKEN', | ||
chatId: 'CHAT_ID', | ||
level: 'error', | ||
unique: true | ||
})); | ||
winston.add(winston.transports.Telegram, { | ||
token : 'TELEGRAM_TOKEN', | ||
chatId : 'CHAT_ID', | ||
level : 'error', | ||
unique : true | ||
}); | ||
winston.log('error', 'Heeere’s Johnny!'); | ||
logger.error('Heeere’s Johnny!'); | ||
logger.log({level: 'error', message: 'Heeere’s Johnny!'}); | ||
``` | ||
Multiple transports, different chats, different options | ||
``` js | ||
var winston = require('winston'); | ||
const winston = require('winston'); | ||
const telegramLogger = require('winston-telegram'); | ||
require('winston-telegram').Telegram; | ||
const logger = winston.createLogger({ | ||
transports: [ | ||
new telegramLogger({ | ||
name: 'error-channel', | ||
token: 'TELEGRAM_TOKEN', | ||
chatId: 'CHAT_ID', | ||
level: 'error', | ||
unique: true | ||
}), | ||
new telegramLogger({ | ||
name: 'info-channel', | ||
token: 'TELEGRAM_TOKEN', | ||
chatId: 'CHAT_ID', | ||
level: 'info', | ||
unique: true, | ||
disableNotification: true | ||
}) | ||
] | ||
}) | ||
var logger = new (winston.Logger)({ | ||
transports: [ | ||
new (winston.transports.Telegram)({ | ||
name: 'error-channel', | ||
token : 'TELEGRAM_TOKEN', | ||
chatId : 'CHAT_ID_1', | ||
level : 'error', | ||
unique : true | ||
}), | ||
new (winston.transports.Telegram)({ | ||
name: 'info-channel', | ||
token : 'TELEGRAM_TOKEN', | ||
chatId : 'CHAT_ID_2', | ||
level : 'info', | ||
unique : true, | ||
disableNotification: true | ||
}) | ||
] | ||
}); | ||
logger.error('All work and no play makes Jack a dull boy.'); | ||
@@ -103,16 +101,15 @@ logger.info('Come play with us, Danny. Forever... and ever... and ever.'); | ||
``` js | ||
var winston = require('winston'); | ||
const logger = require('winston'); | ||
const telegramLogger = require('winston-telegram'); | ||
require('winston-telegram').Telegram; | ||
logger.add(new telegramLogger( { | ||
token : 'TELEGRAM_TOKEN', | ||
chatId : 'CHAT_ID', | ||
level : 'error', | ||
unique : true, | ||
template : '[{level}] [{message}] [{metadata.name}] [{metadata.surname}]' | ||
})); | ||
winston.add(winston.transports.Telegram, { | ||
token : 'TELEGRAM_TOKEN', | ||
chatId : 'CHAT_ID', | ||
level : 'error', | ||
unique : true, | ||
template : '[{level}] [{message}] [{metadata.name}] [{metadata.surname}]' | ||
}); | ||
logger.log({ level: 'error', message: 'Redrum. Redrum. Redrum.', metadata: { name: 'Danny', surname: 'Torrance' }}); | ||
winston.log('error', 'Redrum. Redrum. Redrum.', { name: 'Danny', surname: 'Torrance' }); | ||
// Output: [error] [Redrum. Redrum. Redrum.] [Danny] [Torrance] | ||
@@ -123,23 +120,21 @@ ``` | ||
```js | ||
var winston = require('winston'); | ||
const logger = require('winston'); | ||
const telegramLogger = require('winston-telegram'); | ||
require('winston-telegram').Telegram; | ||
logger.add(new telegramLogger( { | ||
token : 'TELEGRAM_TOKEN', | ||
chatId : 'CHAT_ID', | ||
level : 'warn', | ||
unique : true, | ||
formatMessage : function(options) { | ||
let message = options.message; | ||
if (options.level === 'warn') { | ||
message = '[Warning] ' + message; | ||
} | ||
return message; | ||
} | ||
})); | ||
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; | ||
} | ||
}); | ||
logger.warn('Some warning!!'); | ||
winston.warn('Some warning!!'); | ||
// Output: [Warning] Some warning!! | ||
@@ -150,22 +145,21 @@ ``` | ||
``` js | ||
var winston = require('winston'); | ||
const logger = require('winston'); | ||
const telegramLogger = require('winston-telegram'); | ||
require('winston-telegram').Telegram; | ||
logger.add(new telegramLogger( { | ||
token : 'TELEGRAM_TOKEN', | ||
chatId : 'CHAT_ID', | ||
level : 'info', | ||
batchingDelay: 1000 | ||
})); | ||
winston.add(winston.transports.Telegram, { | ||
token : 'TELEGRAM_TOKEN', | ||
chatId : 'CHAT_ID', | ||
level : 'info', | ||
batchingDelay: 1000 | ||
}); | ||
// first message triggers a new batchingDelay wait | ||
winston.info('First message: '+(new Date()).toString()); | ||
logger.info('First message: '+(new Date()).toString()); | ||
// second message is within the batchingDelay wait triggered by the first | ||
// message, so will be batched | ||
winston.info('Second message: '+(new Date()).toString()); | ||
logger.info('Second message: '+(new Date()).toString()); | ||
setTimeout(function() { | ||
// third message is also within the wait, so also batched | ||
winston.info('Third message: '+(new Date()).toString()); | ||
logger.info('Third message: '+(new Date()).toString()); | ||
}, 500); | ||
@@ -175,3 +169,3 @@ | ||
// fourth message is not within the wait, will be sent separately | ||
winston.info('Fourth message: '+(new Date()).toString()); | ||
logger.info('Fourth message: '+(new Date()).toString()); | ||
}, 1500); | ||
@@ -195,2 +189,5 @@ | ||
### v2.0.0 (2019/01/07) | ||
- `winston@3` support | ||
### v1.3.1 (2019/01/07) | ||
@@ -245,2 +242,3 @@ - [#12](https://github.com/ivanmarban/winston-telegram/pull/12) Fix comments ([@is2ei][7]) | ||
[6]: https://github.com/noveogroup-amorgunov | ||
[7]: https://github.com/is2ei | ||
[7]: https://github.com/is2ei | ||
[8]: https://github.com/ivanmarban/winston-telegram/tree/1.x |
1
12321
4
98
236