-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.ts
More file actions
83 lines (79 loc) · 2.11 KB
/
Copy pathlogger.ts
File metadata and controls
83 lines (79 loc) · 2.11 KB
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
82
83
import pino from "pino";
import { appconfig } from "./config";
let USE_NTFY = appconfig.logger.use_ntfy;
const NTFY_URL = appconfig.logger.ntfy_url;
export async function notify(
level: string,
message: string,
priority: string = "3",
title: string = "kitisbot notify",
tags: string = "",
) {
try {
const token = process.env.TOKEN ?? "bot_token_here:123456789";
await fetch(NTFY_URL, {
method: "POST",
headers: {
Title: level === "error" ? "kitisbot error" : title,
Priority: level === "error" ? "5" : priority,
Tags: tags,
},
body: message.replace(token, "<BOT_TOKEN>"),
});
} catch (err) {
const e = err as Error;
console.error(
`failed to send notification - ${e.message}.\nDisabled notifications until next launch`,
);
appconfig.logger.use_ntfy = false;
USE_NTFY = false;
}
}
export const log = pino({
level: "debug",
hooks: {
logMethod(inputArgs, method, level) {
const [msg, ...args] = inputArgs;
const formatted = typeof msg === "string" ? msg : JSON.stringify(msg);
if (level >= 40 && USE_NTFY) {
notify(pino.levels.labels[level] ?? "", formatted);
}
method.apply(this, [msg, ...args]);
},
},
transport: {
targets: [
{
level: "debug",
target: "pino-pretty",
options: {
colorize: appconfig.logger.use_colors,
translateTime: appconfig.logger.time_format,
ignore: appconfig.logger.ignore,
},
},
{
level: "info",
target: "pino-pretty",
options: {
destination: "info.log",
colorize: appconfig.logger.use_colors,
translateTime: appconfig.logger.time_format,
ignore: appconfig.logger.ignore,
},
},
{
level: "debug",
target: "pino-pretty",
options: {
destination: "debug.log",
colorize: appconfig.logger.use_colors,
translateTime: appconfig.logger.time_format,
ignore: appconfig.logger.ignore,
},
},
],
},
});
log.info("---");
log.debug("logger - ok");