Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
schmooky committed Sep 4, 2019
2 parents ed54b46 + db4a0e5 commit b2c4a48
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 21 deletions.
25 changes: 14 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
"dotenv": "^8.0.0",
"minimist": "^1.2.0",
"node-opus": "^0.3.2",
"nodemailer": "^6.3.0",
"winston": "^3.2.1",
"winston-daily-rotate-file": "^3.10.0",
"winston-mail": "^2.0.0",
"winston-timestamp-colorize": "^1.0.4",
"winston-transport": "^4.3.0"
},
Expand All @@ -44,11 +44,10 @@
"@types/dotenv": "^6.1.1",
"@types/jest": "^24.0.15",
"@types/minimist": "^1.2.0",
"@types/node": "^12.0.10",
"@types/winston": "^2.4.4",
"@types/winston-mail": "^1.5.1",
"@types/node": "^12.0.10",
"@types/nodemailer": "^6.2.1",
"@typescript-eslint/eslint-plugin": "^1.11.0",
"@typescript-eslint/parser": "^1.11.0",
"@typescript-eslint/parser": "^1.11.0",
"cross-env": "^5.2.0",
"eslint": "^5.3.0",
"eslint-config-airbnb-base": "^13.1.0",
Expand Down
39 changes: 34 additions & 5 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,53 @@
import winston from 'winston';
import dotenv from 'dotenv';

import SMTPTransport from './winstonSMTPTransport';

dotenv.config();

const logger = winston.createLogger({
level: 'info',
transports: [
new winston.transports.File({
filename: './log/pretty.log',
filename: './log/warn.log',
level: 'warn',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.metadata(),
winston.format.json(),
),
}),
new winston.transports.File({
filename: './log/error.log',
level: 'error',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.metadata(),
winston.format.json(),
),
}),
new winston.transports.File({
filename: './log/fatal.log',
level: 'fatal',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.metadata(),
winston.format.json(),
),
handleExceptions: true,
}),
new winston.transports.File({ filename: './log/warn.log', level: 'warn' }),
new winston.transports.File({ filename: './log/error.log', level: 'error' }),
new winston.transports.File({ filename: './log/fatal.log', level: 'fatal' }),
new winston.transports.File({ filename: './log/combined.log' }),
],
});

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

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

dotenv.config();

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

public log(info: winston.LogEntry): void {
const mailOptions = {
from: process.env.EMAIL,
to: process.env.EMAIL,
subject: `${info.level} ${info.metadata.timestamp}`,
text: `${info.message} \n ${info.metadata.stack}`,
html: `${info.message} \n ${info.metadata.stack}`,
};

const stack = info.metadata.stack.split(/\r?\n[ \t]+/);

const messages: string[] = stack.map((message: string): string => `<b> ${message} </b>`);

mailOptions.html = messages.join('<br>');

this.transporter.sendMail(mailOptions);
}
}

export default SMTPTransport;

0 comments on commit b2c4a48

Please sign in to comment.