83 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-07-22 23:39:14 +02:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const chalk = require("chalk");
const moment = require("moment");
2017-08-28 12:39:53 +02:00
const config = require("./config");
const nodemailer = require("nodemailer");
2017-07-22 23:39:14 +02:00
var Level;
(function (Level) {
Level[Level["All"] = 0] = "All";
Level[Level["NoDebug"] = 1] = "NoDebug";
Level[Level["NoDebugNoInfo"] = 2] = "NoDebugNoInfo";
Level[Level["NoDebugNoInfoNoWarning"] = 3] = "NoDebugNoInfoNoWarning";
})(Level || (Level = {}));
var level = Level.NoDebug;
function timestamp() {
return moment().format('HH:mm:ss.SSS');
}
function setLevel(value) {
switch (value) {
case 'info':
level = Level.NoDebug;
break;
case 'warn':
level = Level.NoDebugNoInfo;
break;
case 'error':
level = Level.NoDebugNoInfoNoWarning;
break;
default: level = Level.All;
}
}
exports.setLevel = setLevel;
2017-09-05 22:52:38 +02:00
function sendAlarmMail(subject, message) {
2017-08-28 12:39:53 +02:00
let transport = nodemailer.createTransport({
host: config.dict.smtpHost,
port: config.dict.smtpPort,
secure: false,
tls: {
rejectUnauthorized: false
}
});
let mail = {
from: config.dict.smtpSender,
to: config.dict.smtpReceiver,
2017-09-05 22:52:38 +02:00
subject: subject,
2017-08-28 12:39:53 +02:00
text: message
};
transport.sendMail(mail)
.then((v) => {
2017-09-05 22:52:38 +02:00
info(`Mail sent, ${subject}, ${message}, ${v.response}`);
2017-08-28 12:39:53 +02:00
})
.catch((reason) => {
error(`Failure when sending alarm mail: ${message}, ${reason}`);
});
}
exports.sendAlarmMail = sendAlarmMail;
2017-07-22 23:39:14 +02:00
function info(message) {
if (level < Level.NoDebugNoInfo) {
console.log(`${timestamp()} ${chalk.bold.cyan('[ II ]')} ${message}`);
}
}
exports.info = info;
function warn(message) {
if (level < Level.NoDebugNoInfoNoWarning) {
console.log(`${timestamp()} ${chalk.bold.yellow('[ WW ]')} ${message}`);
}
}
exports.warn = warn;
function error(message) {
console.log(`${timestamp()} ${chalk.bold.red('[ EE ]')} ${message}`);
}
exports.error = error;
function success(message) {
console.log(`${timestamp()} ${chalk.bold.green('[ OK ]')} ${message}`);
}
exports.success = success;
function debug(message) {
if (level < Level.NoDebug) {
console.log(`${timestamp()} ${chalk.bold.magenta('[ DB ]')} ${message}`);
}
}
exports.debug = debug;
//# sourceMappingURL=log.js.map