winston-telegram
A Telegram transport for winston.
winston-telegram@2
Installation:
$ 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.
Usage
const logger = require('winston');
const telegramLogger = require('winston-telegram');
logger.add(new telegramLogger({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. [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"]
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
const logger = require('winston');
const telegramLogger = require('winston-telegram');
logger.add(new telegramLogger({
token: 'TELEGRAM_TOKEN',
chatId: 'CHAT_ID',
level: 'error',
unique: true
}));
logger.error('Heeere’s Johnny!');
logger.log({level: 'error', message: 'Heeere’s Johnny!'});
Multiple transports, different chats, different options
const winston = require('winston');
const telegramLogger = require('winston-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
})
]
})
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:
const logger = require('winston');
const telegramLogger = require('winston-telegram');
logger.add(new telegramLogger( {
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' }});
Using custom format message:
const logger = require('winston');
const telegramLogger = require('winston-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;
}
}));
logger.warn('Some warning!!');
Using batching of messages to avoid exceeding rate limits:
const logger = require('winston');
const telegramLogger = require('winston-telegram');
logger.add(new telegramLogger( {
token : 'TELEGRAM_TOKEN',
chatId : 'CHAT_ID',
level : 'info',
batchingDelay: 1000
}));
logger.info('First message: '+(new Date()).toString());
logger.info('Second message: '+(new Date()).toString());
setTimeout(function() {
logger.info('Third message: '+(new Date()).toString());
}, 500);
setTimeout(function() {
logger.info('Fourth message: '+(new Date()).toString());
}, 1500);
Change history
v2.0.0 (2019/01/07)
v1.3.1 (2019/01/07)
v1.3.0 (2018/05/03)
v1.2.1 (2017/07/26)
- #9 Add error description in case of error (@dutu)
- Update sf library
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)