Skip to content

Commit

Permalink
feat(logger): add stack trace to smtp logger
Browse files Browse the repository at this point in the history
  • Loading branch information
schmooky committed Sep 4, 2019
1 parent 24a5946 commit 0b513c6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
10 changes: 10 additions & 0 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import SMTPTransport from './winstonSMTPTransport';

dotenv.config();

const errorTraceFormat = winston.format((info: any): any => { //! Найти типы info или задать
const modifiedInfo = info;
if (info.meta && info.meta instanceof Error) {
modifiedInfo.message = `${info.message} ${info.meta.stack}`;
}
return modifiedInfo;
});

const logger = winston.createLogger({
level: 'info',
transports: [
Expand All @@ -28,6 +36,8 @@ logger.add(new SMTPTransport({
level: 'error',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.metadata(),
winston.format.json(),
),
}));

Expand Down
24 changes: 12 additions & 12 deletions src/utils/winstonSMTPTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ class SMTPTransport extends TransportStream {
},
});

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

// eslint-disable-next-line no-explicit-any
public log(info: any, callback: any): void {
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.message,
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);

if (callback) {
Expand Down

0 comments on commit 0b513c6

Please sign in to comment.