Add logi, start using it in KyselyLogger

This commit is contained in:
Alex Gleason 2025-01-27 15:43:29 -06:00
parent 224d7bfef9
commit 2a6f954df1
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
3 changed files with 31 additions and 11 deletions

View file

@ -52,6 +52,7 @@
"@scure/base": "npm:@scure/base@^1.1.6", "@scure/base": "npm:@scure/base@^1.1.6",
"@sentry/deno": "https://deno.land/x/sentry@7.112.2/index.mjs", "@sentry/deno": "https://deno.land/x/sentry@7.112.2/index.mjs",
"@soapbox/kysely-pglite": "jsr:@soapbox/kysely-pglite@^1.0.0", "@soapbox/kysely-pglite": "jsr:@soapbox/kysely-pglite@^1.0.0",
"@soapbox/logi": "jsr:@soapbox/logi@^0.1.2",
"@soapbox/safe-fetch": "jsr:@soapbox/safe-fetch@^2.0.0", "@soapbox/safe-fetch": "jsr:@soapbox/safe-fetch@^2.0.0",
"@soapbox/stickynotes": "jsr:@soapbox/stickynotes@^0.4.0", "@soapbox/stickynotes": "jsr:@soapbox/stickynotes@^0.4.0",
"@std/assert": "jsr:@std/assert@^0.225.1", "@std/assert": "jsr:@std/assert@^0.225.1",

5
deno.lock generated
View file

@ -49,6 +49,7 @@
"jsr:@nostrify/types@0.36": "0.36.0", "jsr:@nostrify/types@0.36": "0.36.0",
"jsr:@nostrify/types@~0.30.1": "0.30.1", "jsr:@nostrify/types@~0.30.1": "0.30.1",
"jsr:@soapbox/kysely-pglite@1": "1.0.0", "jsr:@soapbox/kysely-pglite@1": "1.0.0",
"jsr:@soapbox/logi@~0.1.2": "0.1.2",
"jsr:@soapbox/safe-fetch@2": "2.0.0", "jsr:@soapbox/safe-fetch@2": "2.0.0",
"jsr:@soapbox/stickynotes@0.4": "0.4.0", "jsr:@soapbox/stickynotes@0.4": "0.4.0",
"jsr:@std/assert@0.223": "0.223.0", "jsr:@std/assert@0.223": "0.223.0",
@ -526,6 +527,9 @@
"npm:kysely@~0.27.4" "npm:kysely@~0.27.4"
] ]
}, },
"@soapbox/logi@0.1.2": {
"integrity": "2fbba613a4dbc092e534097729a729ace772fd67a855cd049e1139ee1facd89f"
},
"@soapbox/safe-fetch@2.0.0": { "@soapbox/safe-fetch@2.0.0": {
"integrity": "f451d686501c76a0faa058fe9d2073676282a8a42c3b93c59159eb9191f11b5f", "integrity": "f451d686501c76a0faa058fe9d2073676282a8a42c3b93c59159eb9191f11b5f",
"dependencies": [ "dependencies": [
@ -2353,6 +2357,7 @@
"jsr:@nostrify/policies@~0.36.1", "jsr:@nostrify/policies@~0.36.1",
"jsr:@nostrify/types@0.36", "jsr:@nostrify/types@0.36",
"jsr:@soapbox/kysely-pglite@1", "jsr:@soapbox/kysely-pglite@1",
"jsr:@soapbox/logi@~0.1.2",
"jsr:@soapbox/safe-fetch@2", "jsr:@soapbox/safe-fetch@2",
"jsr:@soapbox/stickynotes@0.4", "jsr:@soapbox/stickynotes@0.4",
"jsr:@std/assert@~0.225.1", "jsr:@std/assert@~0.225.1",

View file

@ -1,22 +1,36 @@
import { Stickynotes } from '@soapbox/stickynotes'; import { logi } from '@soapbox/logi';
import { Logger } from 'kysely'; import { Logger } from 'kysely';
import { dbQueriesCounter, dbQueryDurationHistogram } from '@/metrics.ts'; import { dbQueriesCounter, dbQueryDurationHistogram } from '@/metrics.ts';
/** Log the SQL for queries. */ /** Log the SQL for queries. */
export const KyselyLogger: Logger = (event) => { export const KyselyLogger: Logger = (event) => {
const console = new Stickynotes('ditto:sql');
const { query, queryDurationMillis } = event; const { query, queryDurationMillis } = event;
const { sql, parameters } = query; const { sql } = query;
const queryDurationSeconds = queryDurationMillis / 1000; const duration = queryDurationMillis / 1000;
dbQueriesCounter.inc(); dbQueriesCounter.inc();
dbQueryDurationHistogram.observe(queryDurationSeconds); dbQueryDurationHistogram.observe(duration);
console.debug( /** Parameters serialized to JSON. */
sql, const parameters = query.parameters.map((parameter) => {
JSON.stringify(parameters), try {
`\x1b[90m(${(queryDurationSeconds / 1000).toFixed(2)}s)\x1b[0m`, return JSON.stringify(parameter);
); } catch {
return String(parameter);
}
});
if (event.level === 'query') {
logi({ level: 'debug', ns: 'ditto.sql', sql, parameters, duration });
}
if (event.level === 'error') {
const error = event.error instanceof Error
? { name: event.error.name, message: event.error.message }
: { name: 'unknown', message: 'Unknown error' };
logi({ level: 'error', ns: 'ditto.sql', sql, parameters, error, duration });
}
}; };