-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogMailer.js
81 lines (70 loc) · 2.43 KB
/
logMailer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const nodemailer = require('nodemailer');
const fs = require('fs');
const address = process.env.SMTP_ADDRESS;
const port = parseInt(process.env.SMTP_PORT);
const user = process.env.SMTP_USER;
const password = process.env.SMTP_PASSWORD;
const email = process.env.LOG_RECIPIENT_EMAIL;
let date = new Date();
// Set date to day before
date = new Date(date - 1000 * 60 * 60 * 24);
const logName = `full_log_${date.getFullYear()}-${date.getMonth() +
1}-${date.getDate()}.log`;
(async () => {
let log = await fs.readFileSync(`./logs/${logName}`, 'utf8');
log = log
.replace(/}/gi, '},')
.replace(/level:/gi, "'level':")
.replace(/message:/gi, "'message':")
.replace(/timestamp:/gi, "'timestamp':")
.replace(/'/gi, '"');
log = log.slice(0, log.length - 2);
log = '[' + log + ']';
log = JSON.parse(log);
//console.log(JSON.parse(log));
//console.log(log);
let htmlBody =
'<table><tr><th colspan="2">Amazon/Lightspeed sync logs for ' +
`${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}` +
'</th></tr>';
log.forEach((logObject, index) => {
if (logObject.level === 'info') {
htmlBody += '<tr style="background: #33cc33">';
} else if (logObject.level === 'warn') {
htmlBody += '<tr style="background: #cccc00">';
} else {
htmlBody += '<tr style="background: #cc0000">';
}
htmlBody += `<td>${logObject.message}</td>`;
htmlBody += `<td>${logObject.timestamp}</td>`;
htmlBody += '</tr>';
});
htmlBody += '</table>';
let testAccount = await nodemailer.createTestAccount();
let transporter = nodemailer.createTransport({
host: address,
port: 587,
secure: false,
auth: {
user: user,
pass: password
},
tls: {
rejectUnauthorized: false
}
});
let info = await transporter.sendMail({
from: `"Sync Logs" <${user}>`, // sender address
to: email, // list of receivers
subject:
'Amazon/Lightspeed sync logs for ' +
`${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`, // Subject line
text: JSON.stringify(log), // plain text body
html: htmlBody // html body
});
//console.log('Message sent: %s', info);
// Message sent: <[email protected]>
// Preview only available when sending through an Ethereal account
//console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
})();