-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.js
35 lines (33 loc) · 1.16 KB
/
logger.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
import "dotenv/config";
import "colors";
import * as webhookHandler from "./webhookHandler.js";
import fs from "fs";
import { getDateStr } from "./dataUtils.js";
/**
* Log text to stdin (when user configured log_level higher or equal to message's level) and file.
* @param {number} level - Log level. Higher number is more verbose.
* @param {string} content - Message.
*/
export function log(level, content) {
if(level > process.env.LOG_LEVEL) return;
const logLine = `[${level}] [${new Date().toISOString()}]: ${content}\n`;
fs.appendFileSync("logs/"+getDateStr()+".log", logLine, {encoding: "utf-8"});
console.log(logLine.slice(0,-1));
}
/**
* Log error to stdout, file and discord webhook.
* @param {Error} content - Error thrown by code.
*/
export function error(content) {
const logLine = `[ERROR] [${new Date().toISOString()}]: ${content}\n`;
fs.appendFileSync("logs/"+getDateStr()+".log", logLine, {encoding: "utf-8"});
webhookHandler.sendEmbed(
webhookHandler.webhooks.errors,
"Error",
[{
title: content.name,
content: content.message
}]
);
console.log(logLine.slice(0,-1).red);
}