-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.ts
67 lines (58 loc) · 2.08 KB
/
logging.ts
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
import { Colors, fs, path } from "./deps.ts"
import { getTimeStamp, getDate } from "./timespamp.ts";
let logDir = Deno.cwd()
export function setLogDir(dir: string): void {
logDir = dir
fs.ensureDirSync(logDir)
// add an empty line at each start for seperation
Deno.writeTextFileSync(path.join(logDir, logFileName()), "\n", { append: true })
}
export function debug(msg: string): void {
console.log(Colors.blue("[DEBUG] ") + msg)
addToLogFile("[DEBUG] " + msg)
}
export function info(msg: string, server?: string): void {
const prefix = server ? `[INFO - ${server}] ` : `[INFO] `
console.log(Colors.green(prefix) + msg)
addToLogFile(prefix + msg)
}
export function infoWebhook(msg: string, server: string, webhook: string): void {
console.log(Colors.green(`[INFO - ${server}] `) + msg)
addToLogFile(`[INFO - ${server}] ` + msg)
if (webhook !== "" && !fs.existsSync("./silent")) {
try {
fetch(webhook, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
content: msg,
username: `${server} [Astro Starter]`,
avatar_url: "https://cdn.glitch.com/21049ce3-c04d-43f4-9653-0d83cc66504c%2Fastroleague_bot.jpg?v=1616962135777",
allowed_mentions: { parse: [] }
})
})
} catch (_) { _ }
}
}
export function warn(msg: string): void {
console.warn(Colors.yellow("[WARN] ") + msg)
addToLogFile("[WARN] " + msg)
}
export function error(msg: string): void {
console.error(Colors.red("[ERROR] ") + msg)
addToLogFile("[ERROR] " + msg)
}
export function critical(msg: string): void {
console.error(Colors.bold(Colors.red("[CRITICAL] ")) + msg)
addToLogFile("[CRITICAL] " + msg)
}
function addToLogFile(msg: string) {
Deno.writeTextFileSync(path.join(logDir, logFileName()),
`[${getTimeStamp()}]${msg}\n`
, { append: true })
}
function logFileName() {
return `log_${getDate()}.txt`
}