Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

winston-telegram

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

winston-telegram

A Telegram transport for winston

  • 2.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.2K
decreased by-16.06%
Maintainers
1
Weekly downloads
 
Created
Source

winston-telegram

NPM

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' }});

// Output: [error] [Redrum. Redrum. Redrum.] [Danny] [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!!');

// Output: [Warning] 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
}));

// first message triggers a new batchingDelay wait
logger.info('First message: '+(new Date()).toString());
// second message is within the batchingDelay wait triggered by the first
// message, so will be batched
logger.info('Second message: '+(new Date()).toString());

setTimeout(function() {
  // third message is also within the wait, so also batched
  logger.info('Third message: '+(new Date()).toString());
}, 500);

setTimeout(function() {
  // fourth message is not within the wait, will be sent separately
  logger.info('Fourth message: '+(new Date()).toString());
}, 1500);

/*
 * Output on Telegram:
 * Sent at 5:22:49PM:
 *   [info] First message: Tue May 30 2017 17:22:47 GMT+0800 (+08)
 *
 *   [info] Second message: Tue May 30 2017 17:22:47 GMT+0800 (+08)
 *
 *   [info] Third message: Tue May 30 2017 17:22:47 GMT+0800 (+08)
 *
 * Sent at 5:22:50PM:
 *   [info] Fourth message: Tue May 30 2017 17:22:48 GMT+0800 (+08)
 */

Change history

v2.0.0 (2019/01/07)

  • winston@3 support

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)

  • Fix typos

v0.2.0 (2016/03/08)

  • #1 Add log level option

v0.1.0 (2015/11/12)

  • First version

Keywords

FAQs

Package last updated on 07 Jan 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc