winston-telegram
A Telegram transport for winston.
Installation
$ npm install winston
$ npm install winston-telegram
Usage
var winston = require('winston');
require('winston-telegram').Telegram;
winston.add(winston.transports.Telegram, options);
Options are the following:
- token: The Telegram bot authentication token. [required]
- chatId: The chatid you want to send to. [required]
- level: Level of messages that this transport should log. [optional] [default info]
- 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]
- 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"]
String template is based on named arguments:
'{level}' -> level of messages
'{message}' -> text 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:
- chatid to chatId
- disable_notificacion to disableNotification
Examples
Using the Default Logger
var winston = require('winston');
require('winston-telegram').Telegram;
winston.add(winston.transports.Telegram, {
token : 'TELEGRAM_TOKEN',
chatId : 'CHAT_ID',
level : 'error',
unique : true
});
winston.log('error', 'Heeere’s Johnny!');
Multiple transports, different chats, different options
var winston = require('winston');
require('winston-telegram').Telegram;
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.');
logger.info('Come play with us, Danny. Forever... and ever... and ever.');
Using template output:
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}] [{metadata.name}] [{metadata.surname}]'
});
winston.log('error', 'Redrum. Redrum. Redrum.', { name: 'Danny', surname: 'Torrance' });
Using batching of messages to avoid exceeding rate limits:
var winston = require('winston');
require('winston-telegram').Telegram;
winston.add(winston.transports.Telegram, {
token : 'TELEGRAM_TOKEN',
chatId : 'CHAT_ID',
level : 'info',
batchingDelay: 1000
});
winston.info('First message: '+(new Date()).toString());
winston.info('Second message: '+(new Date()).toString());
setTimeout(function() {
winston.info('Third message: '+(new Date()).toString());
}, 500);
setTimeout(function() {
winston.info('Fourth message: '+(new Date()).toString());
}, 1500);
Change history
v1.2.0 (2017/03/06)
- #8 Add batching of messages sent within a certain interval (@JustinOng)
v1.1.0 (2017/05/02)
- #7 Use metadata information in messages (@alberto467)
- #7 Replace built-in format function by sf node module (@alberto467)
- Update dependencies
v1.0.0 (2016/12/05)
- #6 Add optional handleExceptions param (@speedone)
- Node.js coding style
- Change option properties for matching coding style
v0.4.0 (2016/09/26)
- #5 Add message template option
- Update dependencies
- Remove peer dependecies
v0.3.0 (2016/07/17)
- #2 Allow multiple transports, send messages silently
- Update dependencies
v0.2.1 (2016/03/30)
v0.2.0 (2016/03/08)
v0.1.0 (2015/11/12)