mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 03:19:46 +00:00
Add logi, start using it in KyselyLogger
This commit is contained in:
parent
224d7bfef9
commit
2a6f954df1
3 changed files with 31 additions and 11 deletions
|
|
@ -52,6 +52,7 @@
|
|||
"@scure/base": "npm:@scure/base@^1.1.6",
|
||||
"@sentry/deno": "https://deno.land/x/sentry@7.112.2/index.mjs",
|
||||
"@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/stickynotes": "jsr:@soapbox/stickynotes@^0.4.0",
|
||||
"@std/assert": "jsr:@std/assert@^0.225.1",
|
||||
|
|
|
|||
5
deno.lock
generated
5
deno.lock
generated
|
|
@ -49,6 +49,7 @@
|
|||
"jsr:@nostrify/types@0.36": "0.36.0",
|
||||
"jsr:@nostrify/types@~0.30.1": "0.30.1",
|
||||
"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/stickynotes@0.4": "0.4.0",
|
||||
"jsr:@std/assert@0.223": "0.223.0",
|
||||
|
|
@ -526,6 +527,9 @@
|
|||
"npm:kysely@~0.27.4"
|
||||
]
|
||||
},
|
||||
"@soapbox/logi@0.1.2": {
|
||||
"integrity": "2fbba613a4dbc092e534097729a729ace772fd67a855cd049e1139ee1facd89f"
|
||||
},
|
||||
"@soapbox/safe-fetch@2.0.0": {
|
||||
"integrity": "f451d686501c76a0faa058fe9d2073676282a8a42c3b93c59159eb9191f11b5f",
|
||||
"dependencies": [
|
||||
|
|
@ -2353,6 +2357,7 @@
|
|||
"jsr:@nostrify/policies@~0.36.1",
|
||||
"jsr:@nostrify/types@0.36",
|
||||
"jsr:@soapbox/kysely-pglite@1",
|
||||
"jsr:@soapbox/logi@~0.1.2",
|
||||
"jsr:@soapbox/safe-fetch@2",
|
||||
"jsr:@soapbox/stickynotes@0.4",
|
||||
"jsr:@std/assert@~0.225.1",
|
||||
|
|
|
|||
|
|
@ -1,22 +1,36 @@
|
|||
import { Stickynotes } from '@soapbox/stickynotes';
|
||||
import { logi } from '@soapbox/logi';
|
||||
import { Logger } from 'kysely';
|
||||
|
||||
import { dbQueriesCounter, dbQueryDurationHistogram } from '@/metrics.ts';
|
||||
|
||||
/** Log the SQL for queries. */
|
||||
export const KyselyLogger: Logger = (event) => {
|
||||
const console = new Stickynotes('ditto:sql');
|
||||
|
||||
const { query, queryDurationMillis } = event;
|
||||
const { sql, parameters } = query;
|
||||
const { sql } = query;
|
||||
|
||||
const queryDurationSeconds = queryDurationMillis / 1000;
|
||||
const duration = queryDurationMillis / 1000;
|
||||
|
||||
dbQueriesCounter.inc();
|
||||
dbQueryDurationHistogram.observe(queryDurationSeconds);
|
||||
dbQueryDurationHistogram.observe(duration);
|
||||
|
||||
console.debug(
|
||||
sql,
|
||||
JSON.stringify(parameters),
|
||||
`\x1b[90m(${(queryDurationSeconds / 1000).toFixed(2)}s)\x1b[0m`,
|
||||
);
|
||||
/** Parameters serialized to JSON. */
|
||||
const parameters = query.parameters.map((parameter) => {
|
||||
try {
|
||||
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 });
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue