Skip to content

Commit

Permalink
feat(logger): add email transport to logger
Browse files Browse the repository at this point in the history
  • Loading branch information
schmooky committed Aug 31, 2019
1 parent 498cd3c commit 28ae253
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"axios": "^0.19.0",
"discord.js": "^11.5.1",
"dotenv": "^8.0.0",
"emailjs": "^2.2.0",
"minimist": "^1.2.0",
"node-opus": "^0.3.2",
"nodemailer": "^6.3.0",
Expand Down
9 changes: 9 additions & 0 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import winston from 'winston';
import dotenv from 'dotenv';

import SMTPTransport from './winstonSMTPTransport';

dotenv.config();

const logger = winston.createLogger({
Expand All @@ -22,6 +24,13 @@ const logger = winston.createLogger({
],
});

logger.add(new SMTPTransport({
level: 'error',
format: winston.format.combine(
winston.format.timestamp(),
),
}));

if (process.env.NODE_ENV !== 'production') {
logger.add(new winston.transports.Console({
level: 'debug',
Expand Down
28 changes: 16 additions & 12 deletions src/utils/winstonSMTPTransport.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
import nodeMailer from 'nodemailer';
import TransportStream from 'winston-transport';
import dotenv from 'dotenv';

dotenv.config();

class SMTPTransport extends TransportStream {
private transporter = nodeMailer.createTransport({
host: 'smtp.yandex.ru',
port: 465,
secure: true,
auth: {
user: '[email protected]',
pass: '{discord-yamusic}',
user: process.env.EMAIL,
pass: process.env.PASS,
},
});

private mailOptions = {
from: '"discord-yamusic" <[email protected]>',
to: '[email protected]',
subject: 'Hello ✔',
text: 'Hello world?',
html: '<b>Hello world?</b>',
};

// eslint-disable-next-line no-useless-constructor
public constructor(options: TransportStream.TransportStreamOptions) {
super(options);
}

public log(info: string, callback: any): void {
this.transporter.sendMail(this.mailOptions);
// eslint-disable-next-line no-explicit-any
public log(info: any, callback: any): void {
const mailOptions = {
from: '"discord-yamusic" <[email protected]>',
to: '[email protected]',
subject: `${info.level}`,
text: info.message,
html: `${info.timestamp} ${info.message}`,
};

this.transporter.sendMail(mailOptions);

if (callback) {
setImmediate(callback);
Expand Down

0 comments on commit 28ae253

Please sign in to comment.