80 lines
2.0 KiB
TypeScript
80 lines
2.0 KiB
TypeScript
import * as moment from 'moment'
|
|
import * as config from './config'
|
|
import * as nodemailer from 'nodemailer'
|
|
|
|
|
|
enum Level {
|
|
All,
|
|
NoDebug,
|
|
NoDebugNoInfo,
|
|
NoDebugNoInfoNoWarning
|
|
}
|
|
|
|
var level = Level.NoDebug
|
|
|
|
function timestamp(): string {
|
|
return moment().format('HH:mm:ss.SSS')
|
|
}
|
|
|
|
export function setLevel(value: string): void {
|
|
switch (value) {
|
|
case 'info': level = Level.NoDebug; break
|
|
case 'warn': level = Level.NoDebugNoInfo; break
|
|
case 'error': level = Level.NoDebugNoInfoNoWarning; break
|
|
default: level = Level.All
|
|
}
|
|
}
|
|
|
|
export function sendAlarmMail(subject : string, message : string): void {
|
|
let transport = nodemailer.createTransport({
|
|
host: config.dict.smtpHost,
|
|
port: config.dict.smtpPort,
|
|
secure: false,
|
|
tls: {
|
|
rejectUnauthorized: false
|
|
}
|
|
});
|
|
|
|
let mail : nodemailer.SendMailOptions = {
|
|
from: config.dict.smtpSender,
|
|
to: config.dict.smtpReceiver,
|
|
subject: subject,
|
|
text: message
|
|
};
|
|
|
|
transport.sendMail(mail)
|
|
.then((v : nodemailer.SentMessageInfo) => {
|
|
info(`Mail sent, ${subject}, ${message}, ${v.response}`)
|
|
})
|
|
.catch((reason : any) => {
|
|
error(`Failure when sending alarm mail: ${message}, ${reason}`)
|
|
})
|
|
|
|
}
|
|
|
|
export function info(message: string): void {
|
|
if (level < Level.NoDebugNoInfo) {
|
|
console.log(`${timestamp()} [ II ] ${message}`)
|
|
}
|
|
}
|
|
|
|
export function warn(message: string): void {
|
|
if (level < Level.NoDebugNoInfoNoWarning) {
|
|
console.log(`${timestamp()} [ WW ] ${message}`)
|
|
}
|
|
}
|
|
|
|
export function error(message: string): void {
|
|
console.log(`${timestamp()} [ EE ] ${message}`)
|
|
}
|
|
|
|
export function success(message: string): void {
|
|
console.log(`${timestamp()} [ OK ] ${message}`)
|
|
}
|
|
|
|
export function debug(message: string): void {
|
|
if (level < Level.NoDebug) {
|
|
console.log(`${timestamp()} [ DB ] ${message}`)
|
|
}
|
|
}
|