Skip to content

Commit 57e1e1d

Browse files
committed
Logging
1 parent 39d907e commit 57e1e1d

File tree

7 files changed

+58
-5
lines changed

7 files changed

+58
-5
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
},
2727
"cSpell.words": [
2828
"fedify",
29+
"logtape",
2930
"unlisten",
3031
"UNLOGGED"
3132
]

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ Changelog
6868

6969
To be released.
7070

71+
- Added some logging using [LogTape] for the sake of debugging. The following
72+
categories are used:
73+
74+
- `["fedify", "postgres", "kv"]`
75+
- `["fedify", "postgres", "mq"]`
76+
77+
[LogTape]: https://logtape.org/
78+
7179
### Version 0.2.2
7280

7381
Released on November 18, 2024.

deno.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"imports": {
1111
"@deno/dnt": "jsr:@deno/dnt@^0.41.3",
1212
"@fedify/fedify": "jsr:@fedify/fedify@^1.0.0",
13+
"@logtape/logtape": "jsr:@logtape/logtape@^0.8.0",
1314
"@std/assert": "jsr:@std/assert@^0.226.0",
1415
"@std/async": "jsr:@std/async@^1.0.5",
1516
"postgres": "npm:postgres@^3.4.5"

deno.lock

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dnt.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ await Deno.writeTextFile(
1313
/^jsr:/,
1414
"npm:",
1515
),
16+
"@logtape/logtape": metadata.imports["@logtape/logtape"].replace(
17+
/^jsr:/,
18+
"npm:",
19+
),
1620
},
1721
}),
1822
);

src/kv.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import type { KvKey, KvStore, KvStoreSetOptions } from "@fedify/fedify";
2+
import { getLogger } from "@logtape/logtape";
23
import type { JSONValue, Parameter, Sql } from "postgres";
34
import { driverSerializesJson } from "./utils.ts";
45

6+
const logger = getLogger(["fedify", "postgres", "kv"]);
7+
58
/**
69
* Options for the PostgreSQL key-value store.
710
*/
@@ -110,6 +113,9 @@ export class PostgresKvStore implements KvStore {
110113
*/
111114
async initialize(): Promise<void> {
112115
if (this.#initialized) return;
116+
logger.debug("Initializing the key-value store table {tableName}...", {
117+
tableName: this.#tableName,
118+
});
113119
await this.#sql`
114120
CREATE UNLOGGED TABLE IF NOT EXISTS ${this.#sql(this.#tableName)} (
115121
key text[] PRIMARY KEY,
@@ -120,6 +126,9 @@ export class PostgresKvStore implements KvStore {
120126
`;
121127
this.#driverSerializesJson = await driverSerializesJson(this.#sql);
122128
this.#initialized = true;
129+
logger.debug("Initialized the key-value store table {tableName}.", {
130+
tableName: this.#tableName,
131+
});
123132
}
124133

125134
/**

src/mq.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ import type {
33
MessageQueueEnqueueOptions,
44
MessageQueueListenOptions,
55
} from "@fedify/fedify";
6+
import { getLogger } from "@logtape/logtape";
67
import type { JSONValue, Parameter, Sql } from "postgres";
78
import postgres from "postgres";
89
import { driverSerializesJson } from "./utils.ts";
910

11+
const logger = getLogger(["fedify", "postgres", "mq"]);
12+
1013
/**
1114
* Options for the PostgreSQL message queue.
1215
*/
@@ -85,14 +88,27 @@ export class PostgresMessageQueue implements MessageQueue {
8588
): Promise<void> {
8689
await this.initialize();
8790
const delay = options?.delay ?? Temporal.Duration.from({ seconds: 0 });
91+
if (options?.delay) {
92+
logger.debug("Enqueuing a message with a delay of {delay}...", {
93+
delay,
94+
message,
95+
});
96+
} else {
97+
logger.debug("Enqueuing a message...", { message });
98+
}
8899
await this.#sql`
89100
INSERT INTO ${this.#sql(this.#tableName)} (message, delay)
90101
VALUES (
91102
${this.#json(message)},
92103
${delay.toString()}
93104
);
94105
`;
106+
logger.debug("Enqueued a message.", { message });
95107
await this.#sql.notify(this.#channelName, delay.toString());
108+
logger.debug("Notified the message queue channel {channelName}.", {
109+
channelName: this.#channelName,
110+
message,
111+
});
96112
}
97113

98114
async listen(
@@ -166,6 +182,9 @@ export class PostgresMessageQueue implements MessageQueue {
166182
*/
167183
async initialize(): Promise<void> {
168184
if (this.#initialized) return;
185+
logger.debug("Initializing the message queue table {tableName}...", {
186+
tableName: this.#tableName,
187+
});
169188
try {
170189
await this.#sql`
171190
CREATE TABLE IF NOT EXISTS ${this.#sql(this.#tableName)} (
@@ -175,16 +194,22 @@ export class PostgresMessageQueue implements MessageQueue {
175194
created timestamp with time zone DEFAULT CURRENT_TIMESTAMP
176195
);
177196
`;
178-
} catch (e) {
197+
} catch (error) {
179198
if (
180-
!(e instanceof postgres.PostgresError &&
181-
e.constraint_name === "pg_type_typname_nsp_index")
199+
!(error instanceof postgres.PostgresError &&
200+
error.constraint_name === "pg_type_typname_nsp_index")
182201
) {
183-
throw e;
202+
logger.error("Failed to initialize the message queue table: {error}", {
203+
error,
204+
});
205+
throw error;
184206
}
185207
}
186208
this.#driverSerializesJson = await driverSerializesJson(this.#sql);
187209
this.#initialized = true;
210+
logger.debug("Initialized the message queue table {tableName}.", {
211+
tableName: this.#tableName,
212+
});
188213
}
189214

190215
/**

0 commit comments

Comments
 (0)