Unverified Commit d6d3d952 authored by Hong Minhee's avatar Hong Minhee
Browse files

Add more log messages

parent 7b5ab419
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -40,6 +40,12 @@ To be released.
    requires `--unstable-temporal` flag.  On other runtime, it still depends
    on *@js-temporal/polyfill*.

 -  Added more log messages using the [LogTape] library.  Currently the below
    logger categories are used:

     -  `["fedify", "federation", "collection"]`
     -  `["fedify", "runtime", "docloader"]`

[FEP-8fcf]: https://codeberg.org/fediverse/fep/src/branch/main/fep/8fcf/fep-8fcf.md


+11 −0
Original line number Diff line number Diff line
@@ -119,6 +119,8 @@ messages.

### `["fedify", "federation", "collection"]`

*This category is available since Fedify 0.8.0.*

The `["fedify", "federation", "collection"]` category is used for logging
messages related to collections (e.g., outbox, followers, following).

@@ -134,6 +136,15 @@ The `["fedify", "federation", "outbox"]` category is used for logging messages
related to outgoing activities.  When you cannot send an activity, you can
check the log messages in this category with the `"debug"` level.

### `["fedify", "runtime", "docloader"]`

*This category is available since Fedify 0.8.0.*

The `["fedify", "runtime", "docloader"]` category is used for logging messages
related to the document loader.  When you are curious about what specific
requests are made by the document loader, you can check the log messages in
this category with the `"debug"` level.

### `["fedify", "x", "fresh"]`

The `["fedify", "x", "fresh"]` category is used for logging messages related
+34 −1
Original line number Diff line number Diff line
import { getLogger } from "@logtape/logtape";
import type { KvKey, KvStore } from "../federation/kv.ts";
import { validateCryptoKey } from "../httpsig/key.ts";
import { sign } from "../httpsig/mod.ts";

const logger = getLogger(["fedify", "runtime", "docloader"]);

/**
 * A remote JSON-LD document and its context fetched by
 * a {@link DocumentLoader}.
@@ -63,17 +66,44 @@ function createRequest(url: string): Request {
  });
}

function logRequest(request: Request) {
  logger.debug(
    "Fetching document: {method} {url} {headers}",
    {
      method: request.method,
      url: request.url,
      headers: Object.fromEntries(request.headers.entries()),
    },
  );
}

async function getRemoteDocument(
  url: string,
  response: Response,
): Promise<RemoteDocument> {
  const documentUrl = response.url === "" ? url : response.url;
  if (!response.ok) {
    logger.error(
      "Failed to fetch document: {status} {url} {headers}",
      {
        status: response.status,
        url: documentUrl,
        headers: Object.fromEntries(response.headers.entries()),
      },
    );
    throw new FetchError(
      documentUrl,
      `HTTP ${response.status}: ${documentUrl}`,
    );
  }
  logger.error(
    "Fetched document: {status} {url} {headers}",
    {
      status: response.status,
      url: documentUrl,
      headers: Object.fromEntries(response.headers.entries()),
    },
  );
  return {
    contextUrl: null,
    document: await response.json(),
@@ -89,7 +119,9 @@ async function getRemoteDocument(
export async function fetchDocumentLoader(
  url: string,
): Promise<RemoteDocument> {
  const response = await fetch(createRequest(url));
  const request = createRequest(url);
  logRequest(request);
  const response = await fetch(request);
  return getRemoteDocument(url, response);
}

@@ -110,6 +142,7 @@ export function getAuthenticatedDocumentLoader(
  return async (url: string): Promise<RemoteDocument> => {
    let request = createRequest(url);
    request = await sign(request, identity.privateKey, identity.keyId);
    logRequest(request);
    const response = await fetch(request);
    return getRemoteDocument(url, response);
  };