refactor: ♻️ Rewrite logging logic into a unified package
Some checks failed
Mirror to Codeberg / Mirror (push) Failing after 0s
Test Publish / build (client) (push) Failing after 0s
Test Publish / build (sdk) (push) Failing after 0s

This commit is contained in:
Jesse Wierzbinski 2025-06-22 18:43:03 +02:00
parent e1bd389bf1
commit aff51b651c
No known key found for this signature in database
32 changed files with 479 additions and 402 deletions

View file

@ -1,9 +1,12 @@
import { getLogger } from "@logtape/logtape";
import * as VersiaEntities from "@versia/sdk/entities";
import { config } from "@versia-server/config";
import { ApiError } from "@versia-server/kit";
import { db } from "@versia-server/kit/db";
import { Instances } from "@versia-server/kit/tables";
import {
federationMessagingLogger,
federationResolversLogger,
} from "@versia-server/logging";
import { randomUUIDv7 } from "bun";
import chalk from "chalk";
import {
@ -175,9 +178,6 @@ export class Instance extends BaseInterface<typeof Instances> {
const wellKnownUrl = new URL("/.well-known/nodeinfo", origin);
// Go to endpoint, then follow the links to the actual metadata
const logger = getLogger(["federation", "resolvers"]);
try {
const { json, ok, status } = await fetch(wellKnownUrl, {
// @ts-expect-error Bun extension
@ -185,7 +185,7 @@ export class Instance extends BaseInterface<typeof Instances> {
});
if (!ok) {
logger.error`Failed to fetch ActivityPub metadata for instance ${chalk.bold(
federationResolversLogger.error`Failed to fetch ActivityPub metadata for instance ${chalk.bold(
origin,
)} - HTTP ${status}`;
return null;
@ -196,7 +196,7 @@ export class Instance extends BaseInterface<typeof Instances> {
};
if (!wellKnown.links) {
logger.error`Failed to fetch ActivityPub metadata for instance ${chalk.bold(
federationResolversLogger.error`Failed to fetch ActivityPub metadata for instance ${chalk.bold(
origin,
)} - No links found`;
return null;
@ -209,7 +209,7 @@ export class Instance extends BaseInterface<typeof Instances> {
);
if (!metadataUrl) {
logger.error`Failed to fetch ActivityPub metadata for instance ${chalk.bold(
federationResolversLogger.error`Failed to fetch ActivityPub metadata for instance ${chalk.bold(
origin,
)} - No metadata URL found`;
return null;
@ -225,7 +225,7 @@ export class Instance extends BaseInterface<typeof Instances> {
});
if (!ok2) {
logger.error`Failed to fetch ActivityPub metadata for instance ${chalk.bold(
federationResolversLogger.error`Failed to fetch ActivityPub metadata for instance ${chalk.bold(
origin,
)} - HTTP ${status2}`;
return null;
@ -264,7 +264,7 @@ export class Instance extends BaseInterface<typeof Instances> {
},
});
} catch (error) {
logger.error`Failed to fetch ActivityPub metadata for instance ${chalk.bold(
federationResolversLogger.error`Failed to fetch ActivityPub metadata for instance ${chalk.bold(
origin,
)} - Error! ${error}`;
return null;
@ -312,14 +312,12 @@ export class Instance extends BaseInterface<typeof Instances> {
}
public async updateFromRemote(): Promise<Instance> {
const logger = getLogger(["federation", "resolvers"]);
const output = await Instance.fetchMetadata(
new URL(`https://${this.data.baseUrl}`),
);
if (!output) {
logger.error`Failed to update instance ${chalk.bold(
federationResolversLogger.error`Failed to update instance ${chalk.bold(
this.data.baseUrl,
)}`;
throw new Error("Failed to update instance");
@ -341,12 +339,10 @@ export class Instance extends BaseInterface<typeof Instances> {
}
public async sendMessage(content: string): Promise<void> {
const logger = getLogger(["federation", "messaging"]);
if (
!this.data.extensions?.["pub.versia:instance_messaging"]?.endpoint
) {
logger.info`Instance ${chalk.gray(
federationMessagingLogger.info`Instance ${chalk.gray(
this.data.baseUrl,
)} does not support Instance Messaging, skipping message`;

View file

@ -1,4 +1,3 @@
import { getLogger } from "@logtape/logtape";
import type {
Account,
Mention as MentionSchema,
@ -29,6 +28,10 @@ import {
Users,
UserToPinnedNotes,
} from "@versia-server/kit/tables";
import {
federationDeliveryLogger,
federationResolversLogger,
} from "@versia-server/logging";
import { password as bunPassword, randomUUIDv7 } from "bun";
import chalk from "chalk";
import {
@ -49,7 +52,6 @@ import { htmlToText } from "html-to-text";
import type { z } from "zod";
import { getBestContentType } from "@/content_types";
import { randomString } from "@/math";
import { sentry } from "@/sentry";
import { searchManager } from "~/classes/search/search-manager";
import type { HttpVerb, KnownEntity } from "~/types/api.ts";
import {
@ -1165,8 +1167,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
}
public static async resolve(uri: URL): Promise<User | null> {
getLogger(["federation", "resolvers"])
.debug`Resolving user ${chalk.gray(uri)}`;
federationResolversLogger.debug`Resolving user ${chalk.gray(uri)}`;
// Check if user not already in database
const foundUser = await User.fromSql(eq(Users.uri, uri.href));
@ -1187,8 +1188,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
return await User.fromId(userUuid[0]);
}
getLogger(["federation", "resolvers"])
.debug`User not found in database, fetching from remote`;
federationResolversLogger.debug`User not found in database, fetching from remote`;
return User.fromVersia(uri);
}
@ -1419,11 +1419,10 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
entity,
);
} catch (e) {
getLogger(["federation", "delivery"]).error`Federating ${chalk.gray(
federationDeliveryLogger.error`Federating ${chalk.gray(
entity.data.type,
)} to ${user.uri} ${chalk.bold.red("failed")}`;
getLogger(["federation", "delivery"]).error`${e}`;
sentry?.captureException(e);
federationDeliveryLogger.error`${e}`;
return { ok: false };
}