@walandemar/winston-nodemailer
v1.0.4
Published
Nodemailer transport for Winston
Downloads
17
Maintainers
Readme
@walandemar/winston-nodemailer
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 intoWinstonNodemailer
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" [email protected]',
to: '[email protected]',
subject: 'Winston logger: new warning occurred'
},
/* OR set mail options as function */
mailOptions: (level, msg, meta) => {
from: '"EXAMPLE ERRORS" [email protected]',
to: '[email protected]',
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: '[email protected]', 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.