Skip to content

Commit 07223ea

Browse files
authored
fix: improve logs (#2018)
1 parent 12a984e commit 07223ea

File tree

12 files changed

+120
-54
lines changed

12 files changed

+120
-54
lines changed

src/lib/jobs/refresh-conversation-stats.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ async function shouldComputeStats(): Promise<boolean> {
1919
export async function computeAllStats() {
2020
for (const span of ["day", "week", "month"] as const) {
2121
computeStats({ dateField: "updatedAt", type: "conversation", span }).catch((e) =>
22-
logger.error(e)
22+
logger.error(e, "Error computing conversation stats for updatedAt")
2323
);
2424
computeStats({ dateField: "createdAt", type: "conversation", span }).catch((e) =>
25-
logger.error(e)
25+
logger.error(e, "Error computing conversation stats for createdAt")
26+
);
27+
computeStats({ dateField: "createdAt", type: "message", span }).catch((e) =>
28+
logger.error(e, "Error computing message stats for createdAt")
2629
);
27-
computeStats({ dateField: "createdAt", type: "message", span }).catch((e) => logger.error(e));
2830
}
2931
}
3032

src/lib/migrations/migrations.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ export async function checkAndRunMigrations() {
9292
result = await migration.up(await Database.getInstance());
9393
});
9494
} catch (e) {
95-
logger.debug(`[MIGRATIONS] "${migration.name}" failed!`);
96-
logger.error(e);
95+
logger.error(e, `[MIGRATIONS] "${migration.name}" failed!`);
9796
} finally {
9897
await session.endSession();
9998
}

src/lib/server/abortedGenerations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class AbortedGenerations {
3636
aborts.map((abort) => [abort.conversationId.toString(), abort.createdAt])
3737
);
3838
} catch (err) {
39-
logger.error(err);
39+
logger.error(err, "Error updating aborted generations list");
4040
}
4141
}
4242
}

src/lib/server/api/routes/groups/user.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ export const userGroup = new Elysia()
197197
currentBillingOrg: isCurrentOrgValid ? currentBillingOrg : undefined,
198198
};
199199
} catch (err) {
200-
logger.error("Error fetching billing orgs:", err);
200+
logger.error(err, "Error fetching billing orgs:");
201201
set.status = 500;
202202
return { error: "Internal server error" };
203203
}

src/lib/server/auth.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export async function findUser(
153153

154154
session.oauth = updatedOAuth;
155155
} catch (err) {
156-
logger.error("Error during token refresh:", err);
156+
logger.error(err, "Error during token refresh:");
157157
return { user: null, invalidateSession: true };
158158
} finally {
159159
await releaseLock(lockKey, lockId);
@@ -374,7 +374,7 @@ export async function validateAndParseCsrfToken(
374374
return { redirectUrl: data.redirectUrl, next: sanitizeReturnPath(data.next) };
375375
}
376376
} catch (e) {
377-
logger.error(e);
377+
logger.error(e, "Error validating and parsing CSRF token");
378378
}
379379
return null;
380380
}

src/lib/server/database.ts

Lines changed: 98 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class Database {
7272
this.client.db(config.MONGODB_DB_NAME + (import.meta.env.MODE === "test" ? "-test" : ""));
7373
await this.initDatabase();
7474
} catch (err) {
75-
logger.error(err, "Connection error");
75+
logger.error(err, "Error connecting to database");
7676
process.exit(1);
7777
}
7878

@@ -182,24 +182,32 @@ export class Database {
182182
{ sessionId: 1, updatedAt: -1 },
183183
{ partialFilterExpression: { sessionId: { $exists: true } } }
184184
)
185-
.catch((e) => logger.error(e));
185+
.catch((e) =>
186+
logger.error(e, "Error creating index for conversations by sessionId and updatedAt")
187+
);
186188
conversations
187189
.createIndex(
188190
{ userId: 1, updatedAt: -1 },
189191
{ partialFilterExpression: { userId: { $exists: true } } }
190192
)
191-
.catch((e) => logger.error(e));
193+
.catch((e) =>
194+
logger.error(e, "Error creating index for conversations by userId and updatedAt")
195+
);
192196
conversations
193197
.createIndex(
194198
{ "message.id": 1, "message.ancestors": 1 },
195199
{ partialFilterExpression: { userId: { $exists: true } } }
196200
)
197-
.catch((e) => logger.error(e));
201+
.catch((e) =>
202+
logger.error(e, "Error creating index for conversations by messageId and ancestors")
203+
);
198204
// Not strictly necessary, could use _id, but more convenient. Also for stats
199205
// To do stats on conversation messages
200206
conversations
201207
.createIndex({ "messages.createdAt": 1 }, { sparse: true })
202-
.catch((e) => logger.error(e));
208+
.catch((e) =>
209+
logger.error(e, "Error creating index for conversations by messages createdAt")
210+
);
203211
// Unique index for stats
204212
conversationStats
205213
.createIndex(
@@ -212,82 +220,139 @@ export class Database {
212220
},
213221
{ unique: true }
214222
)
215-
.catch((e) => logger.error(e));
223+
.catch((e) =>
224+
logger.error(
225+
e,
226+
"Error creating index for conversationStats by type, date.field and date.span"
227+
)
228+
);
216229
// Allow easy check of last computed stat for given type/dateField
217230
conversationStats
218231
.createIndex({
219232
type: 1,
220233
"date.field": 1,
221234
"date.at": 1,
222235
})
223-
.catch((e) => logger.error(e));
236+
.catch((e) => logger.error(e, "Error creating index for abortedGenerations by updatedAt"));
224237
abortedGenerations
225238
.createIndex({ updatedAt: 1 }, { expireAfterSeconds: 30 })
226-
.catch((e) => logger.error(e));
239+
.catch((e) =>
240+
logger.error(
241+
e,
242+
"Error creating index for abortedGenerations by updatedAt and expireAfterSeconds"
243+
)
244+
);
227245
abortedGenerations
228246
.createIndex({ conversationId: 1 }, { unique: true })
229-
.catch((e) => logger.error(e));
247+
.catch((e) =>
248+
logger.error(e, "Error creating index for abortedGenerations by conversationId")
249+
);
230250
sharedConversations.createIndex({ hash: 1 }, { unique: true }).catch((e) => logger.error(e));
231251
settings
232252
.createIndex({ sessionId: 1 }, { unique: true, sparse: true })
233-
.catch((e) => logger.error(e));
253+
.catch((e) => logger.error(e, "Error creating index for settings by sessionId"));
234254
settings
235255
.createIndex({ userId: 1 }, { unique: true, sparse: true })
236-
.catch((e) => logger.error(e));
237-
settings.createIndex({ assistants: 1 }).catch((e) => logger.error(e));
238-
users.createIndex({ hfUserId: 1 }, { unique: true }).catch((e) => logger.error(e));
256+
.catch((e) => logger.error(e, "Error creating index for settings by userId"));
257+
settings
258+
.createIndex({ assistants: 1 })
259+
.catch((e) => logger.error(e, "Error creating index for settings by assistants"));
260+
users
261+
.createIndex({ hfUserId: 1 }, { unique: true })
262+
.catch((e) => logger.error(e, "Error creating index for users by hfUserId"));
239263
users
240264
.createIndex({ sessionId: 1 }, { unique: true, sparse: true })
241-
.catch((e) => logger.error(e));
265+
.catch((e) => logger.error(e, "Error creating index for users by sessionId"));
242266
// No unicity because due to renames & outdated info from oauth provider, there may be the same username on different users
243-
users.createIndex({ username: 1 }).catch((e) => logger.error(e));
267+
users
268+
.createIndex({ username: 1 })
269+
.catch((e) => logger.error(e, "Error creating index for users by username"));
244270
messageEvents
245271
.createIndex({ expiresAt: 1 }, { expireAfterSeconds: 1 })
246-
.catch((e) => logger.error(e));
272+
.catch((e) => logger.error(e, "Error creating index for messageEvents by expiresAt"));
247273
sessions.createIndex({ expiresAt: 1 }, { expireAfterSeconds: 0 }).catch((e) => logger.error(e));
248-
sessions.createIndex({ sessionId: 1 }, { unique: true }).catch((e) => logger.error(e));
249-
assistants.createIndex({ createdById: 1, userCount: -1 }).catch((e) => logger.error(e));
250-
assistants.createIndex({ userCount: 1 }).catch((e) => logger.error(e));
251-
assistants.createIndex({ review: 1, userCount: -1 }).catch((e) => logger.error(e));
252-
assistants.createIndex({ modelId: 1, userCount: -1 }).catch((e) => logger.error(e));
253-
assistants.createIndex({ searchTokens: 1 }).catch((e) => logger.error(e));
254-
assistants.createIndex({ last24HoursCount: 1 }).catch((e) => logger.error(e));
274+
sessions
275+
.createIndex({ sessionId: 1 }, { unique: true })
276+
.catch((e) => logger.error(e, "Error creating index for sessions by sessionId"));
277+
assistants
278+
.createIndex({ createdById: 1, userCount: -1 })
279+
.catch((e) =>
280+
logger.error(e, "Error creating index for assistants by createdById and userCount")
281+
);
282+
assistants
283+
.createIndex({ userCount: 1 })
284+
.catch((e) => logger.error(e, "Error creating index for assistants by userCount"));
285+
assistants
286+
.createIndex({ review: 1, userCount: -1 })
287+
.catch((e) => logger.error(e, "Error creating index for assistants by review and userCount"));
288+
assistants
289+
.createIndex({ modelId: 1, userCount: -1 })
290+
.catch((e) =>
291+
logger.error(e, "Error creating index for assistants by modelId and userCount")
292+
);
293+
assistants
294+
.createIndex({ searchTokens: 1 })
295+
.catch((e) => logger.error(e, "Error creating index for assistants by searchTokens"));
296+
assistants
297+
.createIndex({ last24HoursCount: 1 })
298+
.catch((e) => logger.error(e, "Error creating index for assistants by last24HoursCount"));
255299
assistants
256300
.createIndex({ last24HoursUseCount: -1, useCount: -1, _id: 1 })
257-
.catch((e) => logger.error(e));
301+
.catch((e) =>
302+
logger.error(e, "Error creating index for assistants by last24HoursUseCount and useCount")
303+
);
258304
assistantStats
259305
// Order of keys is important for the queries
260306
.createIndex({ "date.span": 1, "date.at": 1, assistantId: 1 }, { unique: true })
261-
.catch((e) => logger.error(e));
262-
reports.createIndex({ assistantId: 1 }).catch((e) => logger.error(e));
263-
reports.createIndex({ createdBy: 1, assistantId: 1 }).catch((e) => logger.error(e));
307+
.catch((e) =>
308+
logger.error(
309+
e,
310+
"Error creating index for assistantStats by date.span and date.at and assistantId"
311+
)
312+
);
313+
reports
314+
.createIndex({ assistantId: 1 })
315+
.catch((e) => logger.error(e, "Error creating index for reports by assistantId"));
316+
reports
317+
.createIndex({ createdBy: 1, assistantId: 1 })
318+
.catch((e) =>
319+
logger.error(e, "Error creating index for reports by createdBy and assistantId")
320+
);
264321

265322
// Unique index for semaphore and migration results
266323
semaphores.createIndex({ key: 1 }, { unique: true }).catch((e) => logger.error(e));
267324
semaphores
268325
.createIndex({ deleteAt: 1 }, { expireAfterSeconds: 1 })
269-
.catch((e) => logger.error(e));
326+
.catch((e) => logger.error(e, "Error creating index for semaphores by deleteAt"));
270327
tokenCaches
271328
.createIndex({ createdAt: 1 }, { expireAfterSeconds: 5 * 60 })
272-
.catch((e) => logger.error(e));
273-
tokenCaches.createIndex({ tokenHash: 1 }).catch((e) => logger.error(e));
329+
.catch((e) => logger.error(e, "Error creating index for tokenCaches by createdAt"));
330+
tokenCaches
331+
.createIndex({ tokenHash: 1 })
332+
.catch((e) => logger.error(e, "Error creating index for tokenCaches by tokenHash"));
274333
// Tools removed: skipping tools indexes
275334

276335
conversations
277336
.createIndex({
278337
"messages.from": 1,
279338
createdAt: 1,
280339
})
281-
.catch((e) => logger.error(e));
340+
.catch((e) =>
341+
logger.error(e, "Error creating index for conversations by messages from and createdAt")
342+
);
282343

283344
conversations
284345
.createIndex({
285346
userId: 1,
286347
sessionId: 1,
287348
})
288-
.catch((e) => logger.error(e));
349+
.catch((e) =>
350+
logger.error(e, "Error creating index for conversations by userId and sessionId")
351+
);
289352

290-
config.createIndex({ key: 1 }, { unique: true }).catch((e) => logger.error(e));
353+
config
354+
.createIndex({ key: 1 }, { unique: true })
355+
.catch((e) => logger.error(e, "Error creating index for config by key"));
291356
}
292357
}
293358

src/lib/server/exitHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function initExitHandler() {
3838
process.kill(process.pid, "SIGKILL");
3939
} else {
4040
exitHandler().catch((err) => {
41-
logger.error("Exit handler error:", err);
41+
logger.error(err, "Error in exit handler on SIGINT:");
4242
process.kill(process.pid, "SIGKILL");
4343
});
4444
}
@@ -51,7 +51,7 @@ export function initExitHandler() {
5151
process.kill(process.pid, "SIGKILL");
5252
} else {
5353
exitHandler().catch((err) => {
54-
logger.error("Exit handler error:", err);
54+
logger.error(err, "Error in exit handler on SIGTERM:");
5555
process.kill(process.pid, "SIGKILL");
5656
});
5757
}

src/lib/server/textGeneration/generate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export async function* generate(
125125
};
126126
} catch (e) {
127127
finalAnswer = text;
128-
logger.error(e);
128+
logger.error(e, "Error generating summary of reasoning");
129129
}
130130
} else if (modelReasoning && modelReasoning.type === "tokens") {
131131
// Remove the reasoning segment from final answer to avoid duplication
@@ -216,7 +216,7 @@ export async function* generate(
216216
status = summary;
217217
});
218218
} catch (e) {
219-
logger.error(e);
219+
logger.error(e, "Error generating summary of reasoning");
220220
}
221221
}
222222

src/lib/server/textGeneration/title.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export async function* generateTitleForConversation(
2323
title,
2424
};
2525
} catch (cause) {
26-
logger.error(Error("Failed whilte generating title for conversation", { cause }));
26+
logger.error(cause, "Failed while generating title for conversation");
2727
}
2828
}
2929

@@ -74,7 +74,7 @@ Return only the title text.`,
7474
return trimmed || firstFive;
7575
})
7676
.catch((e) => {
77-
logger.error(e);
77+
logger.error(e, "Error generating title");
7878
const firstFive = prompt.split(/\s+/g).slice(0, 5).join(" ");
7979
return firstFive;
8080
});

src/routes/admin/stats/compute/+server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { computeAllStats } from "$lib/jobs/refresh-conversation-stats";
66
// curl -X POST "http://localhost:5173/chat/admin/stats/compute" -H "Authorization: Bearer <ADMIN_API_SECRET>"
77

88
export async function POST() {
9-
computeAllStats().catch((e) => logger.error(e));
9+
computeAllStats().catch((e) => logger.error(e, "Error computing all stats"));
1010
return json(
1111
{
1212
message: "Stats job started",

0 commit comments

Comments
 (0)