-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogRequestToFile.ts
More file actions
44 lines (37 loc) · 1.44 KB
/
logRequestToFile.ts
File metadata and controls
44 lines (37 loc) · 1.44 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
import * as fs from "fs/promises";
import * as path from "path";
// Working directory env variable Cursor sets that we can access.
// __dirname doesnt work, it's undefined.
export const workspaceFolderPath = process.env.WORKSPACE_FOLDER_PATHS ?? "";
// Function to log requests to the variable-extractor endpoint
export const logRequestToFile = async (requestData: any, endpoint: string): Promise<void> => {
// Only log if RETURN0_LOGGING environment variable is set
if (!process.env.RETURN0_LOGGING) {
return;
}
try {
const timestamp = new Date().toISOString();
const logEntry = {
timestamp,
endpoint,
request: requestData,
headers: {
"Content-Type": "application/json",
"api-key": "demo",
"github-url": "https://github.com/Amir-K/ComplexAPI",
},
dirname: workspaceFolderPath,
processCwd: process.cwd(),
};
const logContent = JSON.stringify(logEntry, null, 2) + "\n---\n";
// Create logs directory if it doesn't exist (within the repository)
const logsDir = path.join(workspaceFolderPath, "logs", "localmcp");
await fs.mkdir(logsDir, { recursive: true });
// Append to the log file
const logFilePath = path.join(logsDir, "variable-extractor-requests.log");
await fs.appendFile(logFilePath, logContent, "utf-8");
console.log(`Request logged to: ${logFilePath}`);
} catch (error) {
console.warn("Failed to log request:", error);
}
};