|
1 | 1 | import { z } from "zod" |
2 | 2 | import { baseLogSchema } from "./schemas" |
3 | | - |
4 | | -function buildJson<T extends {}>(logs: Array<Partial<T>>) { |
5 | | - const logsObj: Record<string, unknown> = {} |
6 | | - for (let i = 0; i < logs.length; i++) { |
7 | | - Object.entries(logs[i]).forEach(([key, value]) => { |
8 | | - logsObj[key] = value |
9 | | - }) |
10 | | - } |
11 | | - return logsObj as T |
12 | | -} |
13 | | - |
14 | | -function buildText<T>(logs: Array<Partial<T>>) { |
15 | | - const jsonLine = buildJson(logs) |
16 | | - return Object.entries(jsonLine) |
17 | | - .map(([key, value]) => `${key}=${JSON.stringify(value)}`) |
18 | | - .join(" ") |
19 | | -} |
| 3 | +import { catchUncaughtAction, catchUncaughtRoute } from "./request-utils" |
| 4 | +import { buildJsonLog, buildTextLog } from "./utils" |
20 | 5 |
|
21 | 6 | export type FlytrapLogsOptions = { |
22 | 7 | /** |
@@ -88,46 +73,64 @@ export function createFlytrapLogger<T>({ |
88 | 73 |
|
89 | 74 | let logs: Array<Partial<Log>> = [defaultLog as Log] |
90 | 75 |
|
91 | | - return { |
92 | | - getContext() { |
93 | | - return logs |
94 | | - }, |
95 | | - addContext(context: Partial<Log>) { |
96 | | - logs.push(context) |
97 | | - }, |
98 | | - flush(level: FlushLevel = "log") { |
99 | | - try { |
100 | | - const logValue = |
101 | | - logFormat === "text" ? buildText(logs) : buildJson(logs) |
| 76 | + // Function defintions |
| 77 | + function getContext() { |
| 78 | + return logs |
| 79 | + } |
102 | 80 |
|
103 | | - if (flushMethod === "stdout") { |
104 | | - console[level]( |
105 | | - typeof logValue === "string" ? logValue : JSON.stringify(logValue) |
106 | | - ) |
107 | | - } |
108 | | - if (flushMethod === "api") { |
109 | | - fetch(logsEndpoint, { |
110 | | - method: "POST", |
111 | | - body: JSON.stringify(logValue), |
112 | | - headers: new Headers({ |
113 | | - "Content-Type": "application/json", |
114 | | - ...(flytrapPublicKey && { |
115 | | - Authorization: `Bearer ${flytrapPublicKey}`, |
116 | | - }), |
| 81 | + function addContext(context: Partial<Log>) { |
| 82 | + logs.push(context) |
| 83 | + } |
| 84 | + |
| 85 | + function flush(level: FlushLevel = "log") { |
| 86 | + try { |
| 87 | + const logValue = |
| 88 | + logFormat === "text" ? buildTextLog(logs) : buildJsonLog(logs) |
| 89 | + |
| 90 | + if (flushMethod === "stdout") { |
| 91 | + console[level]( |
| 92 | + typeof logValue === "string" ? logValue : JSON.stringify(logValue) |
| 93 | + ) |
| 94 | + } |
| 95 | + if (flushMethod === "api") { |
| 96 | + fetch(logsEndpoint, { |
| 97 | + method: "POST", |
| 98 | + body: JSON.stringify(logValue), |
| 99 | + headers: new Headers({ |
| 100 | + "Content-Type": "application/json", |
| 101 | + ...(flytrapPublicKey && { |
| 102 | + Authorization: `Bearer ${flytrapPublicKey}`, |
117 | 103 | }), |
118 | | - }).then(async (res) => { |
119 | | - if (res.ok === false) { |
120 | | - console.error( |
121 | | - "Flytrap Logs SDK: Failed to save logs to API. Error:" |
122 | | - ) |
123 | | - console.error(await res.text()) |
124 | | - } |
125 | | - }) |
126 | | - } |
127 | | - } catch (error) { |
128 | | - console.error("Flytrap Logs SDK: Error when flushing logs. Error:") |
129 | | - console.error(error) |
| 104 | + }), |
| 105 | + }).then(async (res) => { |
| 106 | + if (res.ok === false) { |
| 107 | + console.error( |
| 108 | + "Flytrap Logs SDK: Failed to save logs to API. Error:" |
| 109 | + ) |
| 110 | + console.error(await res.text()) |
| 111 | + } |
| 112 | + }) |
130 | 113 | } |
| 114 | + } catch (error) { |
| 115 | + console.error("Flytrap Logs SDK: Error when flushing logs. Error:") |
| 116 | + console.error(error) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return { |
| 121 | + getContext, |
| 122 | + addContext, |
| 123 | + flush, |
| 124 | + catchUncaughtAction<T extends (...args: any[]) => Promise<any>>( |
| 125 | + fn: T, |
| 126 | + options?: Partial<z.infer<typeof baseLogSchema>> |
| 127 | + ) { |
| 128 | + return catchUncaughtAction(fn, addContext, flush, options) |
| 129 | + }, |
| 130 | + catchUncaughtRoute<T extends { params: Record<string, unknown> }>( |
| 131 | + fn: (request: Request, context: T) => Promise<Response> | Response |
| 132 | + ) { |
| 133 | + return catchUncaughtRoute(fn, addContext, flush) |
131 | 134 | }, |
132 | 135 | } |
133 | 136 | } |
0 commit comments