1.0.4 • Published 7 years ago

@walandemar/winston-nodemailer v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
7 years ago

@walandemar/winston-nodemailer

NPM

This is mail transport for Winston logger on top of the Nodemailer. Send log e-mails from Node.js - easy as cake! 🍰✉️

Prerequisites

  • This package does't require and install any external dependencies by itself, but it uses your current project's dependency of winston.
  • You need to install nodemailer in order to create (or use existance) transporter and pass it into WinstonNodemailer class.

Installing

npm i winston nodemailer @walandemar/winston-nodemailer

Usage

const WinstonNodemailer = require('@walandemar/winston-nodemailer')

const logger = new winston.Logger({
  transports: [
    new WinstonNodemailer(options)
  ]
})

// or

const logger = new winston.Logger()

logger.add(WinstonNodemailer, options)

See API for list of available options.

Example

const winston = require('winston')

const nodemailer = require('nodemailer')

const WinstonNodemailer = require('@walandemar/winston-nodemailer')

const logger = new winston.Logger({
  transports: [
    new winston.transports.Console(),
    new WinstonNodemailer({
      level: 'warn',
      handleExceptions: true,

      /* Set mail options as Object */
      mailOptions: {
        from: '"EXAMPLE ERRORS" errors@example.com',
        to: 'developer@example.com',
        subject: 'Winston logger: new warning occurred'
      },

      /* OR set mail options as function */
      mailOptions: (level, msg, meta) => {
        from: '"EXAMPLE ERRORS" errors@example.com',
        to: 'developer@example.com',
        subject: `Winston logger: new ${level} occurred`,
        html: `${msg}</br></br>${JSON.stringify(meta || {})}</br></br>`
      },

      transporter: nodemailer.createTransport({
        host: 'smtp.example.com',
        port: 465,
        secure: true,
        auth: {user: 'errors@example.com', pass: 'SuperSecretPassword'}
      })
    })
  ]
})

logger.info('This message will use only Console transport')

logger.warn('This message will use both Console and WinstonNodemailer transports. Pls check your e-mail inbox:)')

API

WinstonNodemailer extends winston.Transport class with it's standart available options, such as: level, silent, handleExceptions, etc. There are also package specific options:

  • mailOptions: Object or Function(level, msg, meta) that returns message configuration for nodemailer. See full list of available options at Nodemailer homepage. REQUIRED - from: the email address of the sender. REQUIRED - to: the email address of recipient/recipients. REQUIRED
  • transporter: valid Nodemailer transporter that is used to finally send email using mailOptions. REQUIRED

License

This project is licensed under the MIT License - see the LICENSE file for details.