forked from Lilly-Protocol/agentlily-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime-logger.ts
More file actions
149 lines (123 loc) · 4.07 KB
/
Copy pathruntime-logger.ts
File metadata and controls
149 lines (123 loc) · 4.07 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
export type RuntimeLogLevel = "debug" | "info" | "warn" | "error";
export interface RuntimeLogger {
readonly level?: RuntimeLogLevel;
info(message: string, metadata?: Record<string, unknown>): void;
warn(message: string, metadata?: Record<string, unknown>): void;
debug(message: string, metadata?: Record<string, unknown>): void;
error(message: string, metadata?: Record<string, unknown>): void;
}
const LOG_LEVEL_PRIORITY: Record<RuntimeLogLevel, number> = {
debug: 0,
info: 1,
warn: 2,
error: 3
};
export interface ConsoleRuntimeLoggerOptions {
level?: RuntimeLogLevel;
redactKeys?: RegExp;
}
const DEFAULT_REDACT_KEYS = /(secret|token|password|api.?key|authorization)/i;
function redactValue(value: unknown, redactKeys: RegExp): unknown {
if (Array.isArray(value)) {
return value.map((item: unknown): unknown => {
if (item !== null && typeof item === "object") {
return redactValue(item, redactKeys);
}
return item;
});
}
if (value !== null && typeof value === "object") {
const result: Record<string, unknown> = {};
for (const [key, entry] of Object.entries(value)) {
if (redactKeys.test(key)) {
result[key] = "[REDACTED]";
} else {
result[key] = redactValue(entry, redactKeys);
}
}
return result;
}
return value;
}
export class ConsoleRuntimeLogger implements RuntimeLogger {
private readonly minimumLevel: RuntimeLogLevel;
private readonly redactKeys: RegExp;
public constructor(options: ConsoleRuntimeLoggerOptions = {}) {
this.minimumLevel = options.level ?? "info";
this.redactKeys = options.redactKeys ?? DEFAULT_REDACT_KEYS;
}
public info(message: string, metadata?: Record<string, unknown>): void {
if (this.shouldLog("info")) {
console.info(message, this.prepareMetadata(metadata));
}
}
public warn(message: string, metadata?: Record<string, unknown>): void {
if (this.shouldLog("warn")) {
console.warn(message, this.prepareMetadata(metadata));
}
}
public debug(message: string, metadata?: Record<string, unknown>): void {
if (this.shouldLog("debug")) {
console.debug(message, this.prepareMetadata(metadata));
}
}
public error(message: string, metadata?: Record<string, unknown>): void {
if (this.shouldLog("error")) {
console.error(message, this.prepareMetadata(metadata));
}
}
private shouldLog(level: RuntimeLogLevel): boolean {
return LOG_LEVEL_PRIORITY[level] >= LOG_LEVEL_PRIORITY[this.minimumLevel];
}
private prepareMetadata(
metadata?: Record<string, unknown>
): Record<string, unknown> {
return redactValue(metadata ?? {}, this.redactKeys) as Record<
string,
unknown
>;
}
}
export interface InMemoryRuntimeLoggerOptions {
maxEntries?: number;
}
interface InMemoryLogEntry {
level: RuntimeLogLevel;
message: string;
metadata: Record<string, unknown> | undefined;
}
export class InMemoryRuntimeLogger implements RuntimeLogger {
public readonly entries: InMemoryLogEntry[] = [];
private readonly maxEntries: number;
public constructor(options: InMemoryRuntimeLoggerOptions = {}) {
this.maxEntries = options.maxEntries ?? 5_000;
}
public info(message: string, metadata?: Record<string, unknown>): void {
this.appendEntry("info", message, metadata);
}
public warn(message: string, metadata?: Record<string, unknown>): void {
this.appendEntry("warn", message, metadata);
}
public debug(message: string, metadata?: Record<string, unknown>): void {
this.appendEntry("debug", message, metadata);
}
public error(message: string, metadata?: Record<string, unknown>): void {
this.appendEntry("error", message, metadata);
}
public clear(): void {
this.entries.length = 0;
}
public size(): number {
return this.entries.length;
}
private appendEntry(
level: RuntimeLogLevel,
message: string,
metadata?: Record<string, unknown>
): void {
if (this.maxEntries > 0 && this.entries.length >= this.maxEntries) {
this.entries.shift();
}
this.entries.push({ level, message, metadata });
}
}