-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.js
More file actions
60 lines (50 loc) · 1.13 KB
/
logger.js
File metadata and controls
60 lines (50 loc) · 1.13 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
import { v4 as uuidv4 } from "uuid";
import { ContextModel, normalizeMemory } from "./mcp.model.js";
let dbInstance = null;
export function initLogger(db) {
dbInstance = db;
}
async function logToDB(log) {
if (!dbInstance) {
return;
}
try {
await dbInstance.collection("logs").insertOne({
id: uuidv4(),
...log,
createdAt: new Date()
});
} catch {}
}
export async function logError(error, context = {}) {
await logToDB({
type: "error",
message: error.message,
stack: error.stack,
context
});
if (!dbInstance) {
return;
}
try {
const memory = new ContextModel({
agent: context.agent || "system",
project: context.project || "global",
scope: "global",
type: "error",
content: error.message,
metadata: context,
tags: ["error", "debug"]
});
await dbInstance.collection("contexts").insertOne(normalizeMemory(memory));
} catch (err) {
process.stderr.write("Logger error: " + err.message + "\n");
}
}
export async function logInfo(message, context = {}) {
await logToDB({
type: "info",
message,
context
});
}