-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.ts
45 lines (39 loc) · 1.05 KB
/
log.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
export enum LogLevel {
INFO = "INFO",
WARN = "WARN",
ERROR = "ERROR",
}
// Get the directory name for the job label
const JOB_LABEL =
new URL(".", import.meta.url).pathname.split("/").filter(Boolean).pop() ||
"smallweb-logs";
// Loki server URL
const LOKI_URL = "https://loki.tools.ejfox.com/loki/api/v1/push";
// Function to send logs to Loki
export async function log(
level: LogLevel,
message: string,
meta?: Record<string, unknown>
) {
const timestamp = BigInt(Date.now()) * 1000000n; // Current time in nanoseconds
const logEntry = {
streams: [
{
stream: { job: JOB_LABEL, level },
values: [[timestamp.toString(), JSON.stringify({ message, meta })]],
},
],
};
try {
const res = await fetch(LOKI_URL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(logEntry),
});
if (!res.ok) {
console.error(`Failed to send log to Loki: ${res.statusText}`);
}
} catch (error) {
console.error("Error sending log to Loki:", error);
}
}