Skip to content

Commit

Permalink
fix: make format specs available in DebugLogger with prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
jiftechnify committed Jun 10, 2023
1 parent c974d0e commit ff69d0f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
6 changes: 3 additions & 3 deletions packages/nostr-fetch-kernel/src/debugLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ export class DebugLogger {
/**
* Emits log with the specified level.
*/
public log(lv: BasicLogLevel, ...data: unknown[]) {
public log(lv: BasicLogLevel, msg: unknown, ...optionalParams: unknown[]) {
if (scoreForlogLevel[lv] < scoreForlogLevel[this.#minLevel]) {
return;
}
this.#renderedPrefix
? logFnForlogLevel[lv](this.#renderedPrefix, ...data)
: logFnForlogLevel[lv](...data);
? logFnForlogLevel[lv](`${this.#renderedPrefix} ${msg}`, ...optionalParams)
: logFnForlogLevel[lv](msg, ...optionalParams);
}
}
18 changes: 9 additions & 9 deletions packages/nostr-fetch/src/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class NostrFetcher {
...defaultFetchOptions,
...options,
};
this.#debugLogger?.log("verbose", "finalOpts: %O", finalOpts);
this.#debugLogger?.log("verbose", "finalOpts=%O", finalOpts);

const connectedRelayUrls = await this.#fetcherBase.ensureRelays(relayUrls, finalOpts);

Expand Down Expand Up @@ -163,7 +163,7 @@ export class NostrFetcher {
limit: Math.min(finalOpts.limitPerReq, MAX_LIMIT_PER_REQ),
};
});
logger?.log("verbose", "refinedFilters: %O", refinedFilters);
logger?.log("verbose", "refinedFilters=%O", refinedFilters);

let gotNewEvent = false;
let oldestCreatedAt = Number.MAX_SAFE_INTEGER;
Expand All @@ -186,12 +186,12 @@ export class NostrFetcher {

if (!gotNewEvent) {
// termination contidion 1
this.#debugLogger?.log("info", `[${rurl}] got ${localSeenEventIds.size} events`);
logger?.log("info", `got ${localSeenEventIds.size} events`);
break;
}
if (finalOpts.abortSignal?.aborted) {
// termination contidion 2
this.#debugLogger?.log("info", `[${rurl}] aborted`);
logger?.log("info", `aborted`);
break;
}

Expand Down Expand Up @@ -279,7 +279,7 @@ export class NostrFetcher {
...defaultFetchLatestOptions,
...options,
};
this.#debugLogger?.log("verbose", "finalOpts: %O", finalOpts);
this.#debugLogger?.log("verbose", "finalOpts=%O", finalOpts);

// options for subscription
const subOpts: FetchTillEoseOptions = {
Expand Down Expand Up @@ -318,7 +318,7 @@ export class NostrFetcher {
limit: Math.min(remainingLimit, MAX_LIMIT_PER_REQ),
};
});
logger?.log("verbose", "refinedFilters: %O", refinedFilters);
logger?.log("verbose", "refinedFilters=%O", refinedFilters);

let numNewEvents = 0;
let oldestCreatedAt = Number.MAX_SAFE_INTEGER;
Expand All @@ -342,12 +342,12 @@ export class NostrFetcher {
remainingLimit -= numNewEvents;
if (numNewEvents === 0 || remainingLimit <= 0) {
// termination condition 1, 2
this.#debugLogger?.log("info", `[${rurl}] got ${localSeenEventIds.size} events`);
logger?.log("info", `got ${localSeenEventIds.size} events`);
break;
}
if (finalOpts.abortSignal?.aborted) {
// termination condition 3
this.#debugLogger?.log("info", `[${rurl}] aborted`);
logger?.log("info", `aborted`);
break;
}

Expand Down Expand Up @@ -449,7 +449,7 @@ export class NostrFetcher {
...defaultFetchLatestOptions,
...options,
};
this.#debugLogger?.log("verbose", "finalOpts: %O", finalOpts);
this.#debugLogger?.log("verbose", "finalOpts=%O", finalOpts);

// options for subscription
const subOpts: FetchTillEoseOptions = {
Expand Down
8 changes: 5 additions & 3 deletions packages/nostr-fetch/src/fetcherBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class DefaultFetcherBase implements NostrFetcherBase {

public constructor(options: RelayPoolOptions) {
this.#relayPool = initRelayPool(options);
if (options.minLogLevel === "none") {
if (options.minLogLevel !== "none") {
this.#debugLogger = new DebugLogger(options.minLogLevel);
}
}
Expand Down Expand Up @@ -60,13 +60,13 @@ export class DefaultFetcherBase implements NostrFetcherBase {
): AsyncIterable<NostrEvent> {
const logger = this.#debugLogger?.subLogger(relayUrl);

const [tx, chIter] = Channel.make<NostrEvent>();

const relay = this.#relayPool.getRelayIfConnected(relayUrl);
if (relay === undefined) {
return emptyAsyncGen();
}

const [tx, chIter] = Channel.make<NostrEvent>();

// error handlings
const onNotice = (n: unknown) => {
tx.error(Error(`NOTICE: ${JSON.stringify(n)}`));
Expand Down Expand Up @@ -97,6 +97,7 @@ export class DefaultFetcherBase implements NostrFetcherBase {
sub.close();
tx.close();
removeRelayListeners();
logger?.log("verbose", `CLOSE: subId=${options.subId ?? "<auto>"}`);
};

sub.on("eose", ({ aborted }) => {
Expand All @@ -117,6 +118,7 @@ export class DefaultFetcherBase implements NostrFetcherBase {
});

// start the subscription
logger?.log("verbose", `REQ: subId=${options.subId ?? "<auto>"}, filters=%O`, filters);
sub.req();

return chIter;
Expand Down

0 comments on commit ff69d0f

Please sign in to comment.