-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlogsff-client.ts
More file actions
68 lines (59 loc) · 1.34 KB
/
Copy pathlogsff-client.ts
File metadata and controls
68 lines (59 loc) · 1.34 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
/**
* Logsff is a simple backend observability tool.
*
* This is the client for the Logsff service. Copy and paste into your project and use the `send` function to log requests and errors.
*
* @see https://degreat.co.uk/logsff
*/
type RequestLog = {
type: "request";
appId: string;
method: string;
path: string;
sessionId?: string;
meta?: Record<string, any>;
status: number;
timestamp: number;
duration: number;
};
type AppLog = {
type: "app";
appId: string;
level: "info" | "error" | "warn";
message: string;
timestamp: number;
duration?: number;
sessionId?: string;
meta?: Record<string, any>;
};
type Log = RequestLog | AppLog;
async function send(log: Exclude<Log, "appId">) {
if (
!process.env.LOGSFF_URL ||
!process.env.LOGSFF_TOKEN ||
!process.env.LOGSFF_APP_ID
) {
console.warn("Logsff is not configured");
return;
}
const res = await fetch(process.env.LOGSFF_URL, {
method: "POST",
body: JSON.stringify({ ...log, appId: process.env.LOGSFF_APP_ID }),
headers: {
"Content-Type": "application/json",
Authorization: process.env.LOGSFF_TOKEN!,
},
});
if (!res.ok) {
throw new LogsffError(await res.text(), res);
}
return await res.json();
}
class LogsffError extends Error {
response: any;
constructor(message: string, response: any) {
super(message);
this.response = response;
}
}
export { send };