This is mail transport for Winston logger on top of the Nodemailer. Send log e-mails from Node.js - easy as cake! 🍰✉️
- 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 intoWinstonNodemailer
class.
npm i winston nodemailer @walandemar/winston-nodemailer
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.
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:)')
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]
This project is licensed under the MIT License - see the LICENSE file for details.