refactor(logs): move to core package (#4586)
This commit is contained in:
@@ -22,8 +22,8 @@
|
||||
},
|
||||
"prettier": "@homarr/prettier-config",
|
||||
"dependencies": {
|
||||
"@homarr/core": "workspace:^0.1.0",
|
||||
"@homarr/db": "workspace:^0.1.0",
|
||||
"@homarr/log": "workspace:^0.1.0",
|
||||
"@homarr/server-settings": "workspace:^0.1.0",
|
||||
"@umami/node": "^0.4.0",
|
||||
"superjson": "2.2.6"
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
import type { UmamiEventData } from "@umami/node";
|
||||
import { Umami } from "@umami/node";
|
||||
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { count, db } from "@homarr/db";
|
||||
import { getServerSettingByKeyAsync } from "@homarr/db/queries";
|
||||
import { integrations, items, users } from "@homarr/db/schema";
|
||||
import { logger } from "@homarr/log";
|
||||
import type { defaultServerSettings } from "@homarr/server-settings";
|
||||
|
||||
import { Stopwatch } from "../../common/src";
|
||||
import { UMAMI_HOST_URL, UMAMI_WEBSITE_ID } from "./constants";
|
||||
|
||||
const logger = createLogger({ module: "analytics" });
|
||||
|
||||
export const sendServerAnalyticsAsync = async () => {
|
||||
const stopWatch = new Stopwatch();
|
||||
const analyticsSettings = await getServerSettingByKeyAsync(db, "analytics");
|
||||
|
||||
@@ -33,7 +33,6 @@
|
||||
"@homarr/docker": "workspace:^0.1.0",
|
||||
"@homarr/icons": "workspace:^0.1.0",
|
||||
"@homarr/integrations": "workspace:^0.1.0",
|
||||
"@homarr/log": "workspace:^",
|
||||
"@homarr/old-import": "workspace:^0.1.0",
|
||||
"@homarr/old-schema": "workspace:^0.1.0",
|
||||
"@homarr/ping": "workspace:^0.1.0",
|
||||
|
||||
@@ -4,13 +4,15 @@ import { zfd } from "zod-form-data";
|
||||
import { z } from "zod/v4";
|
||||
|
||||
import { addCustomRootCertificateAsync, removeCustomRootCertificateAsync } from "@homarr/certificates/server";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { and, eq } from "@homarr/db";
|
||||
import { trustedCertificateHostnames } from "@homarr/db/schema";
|
||||
import { logger } from "@homarr/log";
|
||||
import { certificateValidFileNameSchema, checkCertificateFile } from "@homarr/validation/certificates";
|
||||
|
||||
import { createTRPCRouter, permissionRequiredProcedure } from "../../trpc";
|
||||
|
||||
const logger = createLogger({ module: "certificateRouter" });
|
||||
|
||||
export const certificateRouter = createTRPCRouter({
|
||||
addCertificate: permissionRequiredProcedure
|
||||
.requiresPermission("admin")
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
import { observable } from "@trpc/server/observable";
|
||||
import z from "zod/v4";
|
||||
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { cronExpressionSchema, jobGroupKeys, jobNameSchema } from "@homarr/cron-job-api";
|
||||
import { cronJobApi } from "@homarr/cron-job-api/client";
|
||||
import type { TaskStatus } from "@homarr/cron-job-status";
|
||||
import { createCronJobStatusChannel } from "@homarr/cron-job-status";
|
||||
import { logger } from "@homarr/log";
|
||||
|
||||
import { createTRPCRouter, permissionRequiredProcedure } from "../trpc";
|
||||
|
||||
const logger = createLogger({ module: "cronJobsRouter" });
|
||||
|
||||
export const cronJobsRouter = createTRPCRouter({
|
||||
triggerJob: permissionRequiredProcedure
|
||||
.requiresPermission("admin")
|
||||
|
||||
@@ -3,6 +3,7 @@ import { z } from "zod/v4";
|
||||
|
||||
import { createId, objectEntries } from "@homarr/common";
|
||||
import { decryptSecret, encryptSecret } from "@homarr/common/server";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import type { Database } from "@homarr/db";
|
||||
import { and, asc, eq, handleTransactionsAsync, inArray, like, or } from "@homarr/db";
|
||||
import {
|
||||
@@ -26,7 +27,6 @@ import {
|
||||
integrationSecretKindObject,
|
||||
} from "@homarr/definitions";
|
||||
import { createIntegrationAsync } from "@homarr/integrations";
|
||||
import { logger } from "@homarr/log";
|
||||
import { byIdSchema } from "@homarr/validation/common";
|
||||
import {
|
||||
integrationCreateSchema,
|
||||
@@ -40,6 +40,8 @@ import { throwIfActionForbiddenAsync } from "./integration-access";
|
||||
import { MissingSecretError, testConnectionAsync } from "./integration-test-connection";
|
||||
import { mapTestConnectionError } from "./map-test-connection-error";
|
||||
|
||||
const logger = createLogger({ module: "integrationRouter" });
|
||||
|
||||
export const integrationRouter = createTRPCRouter({
|
||||
all: publicProcedure.query(async ({ ctx }) => {
|
||||
const groupsOfCurrentUser = await ctx.db.query.groupMembers.findMany({
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
import { decryptSecret } from "@homarr/common/server";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { ErrorWithMetadata } from "@homarr/core/infrastructure/logs/error";
|
||||
import type { Integration } from "@homarr/db/schema";
|
||||
import type { IntegrationKind, IntegrationSecretKind } from "@homarr/definitions";
|
||||
import { getAllSecretKindOptions } from "@homarr/definitions";
|
||||
import { createIntegrationAsync } from "@homarr/integrations";
|
||||
import { logger } from "@homarr/log";
|
||||
|
||||
const logger = createLogger({ module: "integrationTestConnection" });
|
||||
|
||||
type FormIntegration = Omit<Integration, "appId"> & {
|
||||
secrets: {
|
||||
@@ -35,8 +38,13 @@ export const testConnectionAsync = async (
|
||||
};
|
||||
} catch (error) {
|
||||
logger.warn(
|
||||
new Error(
|
||||
`Failed to decrypt secret from database integration="${integration.name}" secretKind="${secret.kind}"`,
|
||||
new ErrorWithMetadata(
|
||||
"Failed to decrypt secret from database",
|
||||
{
|
||||
integrationName: integration.name,
|
||||
integrationKind: integration.kind,
|
||||
secretKind: secret.kind,
|
||||
},
|
||||
{ cause: error },
|
||||
),
|
||||
);
|
||||
|
||||
@@ -2,7 +2,6 @@ import type { V1NodeList, VersionInfo } from "@kubernetes/client-node";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import type { ClusterResourceCount, KubernetesCluster } from "@homarr/definitions";
|
||||
import { logger } from "@homarr/log";
|
||||
|
||||
import { kubernetesMiddleware } from "../../../middlewares/kubernetes";
|
||||
import { createTRPCRouter, permissionRequiredProcedure } from "../../../trpc";
|
||||
@@ -129,7 +128,6 @@ export const clusterRouter = createTRPCRouter({
|
||||
],
|
||||
};
|
||||
} catch (error) {
|
||||
logger.error("Unable to retrieve cluster", error);
|
||||
throw new TRPCError({
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
message: "An error occurred while fetching Kubernetes cluster",
|
||||
@@ -165,7 +163,6 @@ export const clusterRouter = createTRPCRouter({
|
||||
{ label: "volumes", count: volumes.items.length },
|
||||
];
|
||||
} catch (error) {
|
||||
logger.error("Unable to retrieve cluster resource counts", error);
|
||||
throw new TRPCError({
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
message: "An error occurred while fetching Kubernetes resources count",
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import type { KubernetesBaseResource } from "@homarr/definitions";
|
||||
import { logger } from "@homarr/log";
|
||||
|
||||
import { kubernetesMiddleware } from "../../../middlewares/kubernetes";
|
||||
import { createTRPCRouter, permissionRequiredProcedure } from "../../../trpc";
|
||||
@@ -25,7 +24,6 @@ export const configMapsRouter = createTRPCRouter({
|
||||
};
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error("Unable to retrieve configMaps", error);
|
||||
throw new TRPCError({
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
message: "An error occurred while fetching Kubernetes ConfigMaps",
|
||||
|
||||
@@ -2,7 +2,6 @@ import type { V1HTTPIngressPath, V1Ingress, V1IngressRule } from "@kubernetes/cl
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import type { KubernetesIngress, KubernetesIngressPath, KubernetesIngressRuleAndPath } from "@homarr/definitions";
|
||||
import { logger } from "@homarr/log";
|
||||
|
||||
import { kubernetesMiddleware } from "../../../middlewares/kubernetes";
|
||||
import { createTRPCRouter, permissionRequiredProcedure } from "../../../trpc";
|
||||
@@ -43,7 +42,6 @@ export const ingressesRouter = createTRPCRouter({
|
||||
|
||||
return ingresses.items.map(mapIngress);
|
||||
} catch (error) {
|
||||
logger.error("Unable to retrieve ingresses", error);
|
||||
throw new TRPCError({
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
message: "An error occurred while fetching Kubernetes ingresses",
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import type { KubernetesNamespace, KubernetesNamespaceState } from "@homarr/definitions";
|
||||
import { logger } from "@homarr/log";
|
||||
|
||||
import { kubernetesMiddleware } from "../../../middlewares/kubernetes";
|
||||
import { createTRPCRouter, permissionRequiredProcedure } from "../../../trpc";
|
||||
@@ -25,7 +24,6 @@ export const namespacesRouter = createTRPCRouter({
|
||||
} satisfies KubernetesNamespace;
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error("Unable to retrieve namespaces", error);
|
||||
throw new TRPCError({
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
message: "An error occurred while fetching Kubernetes namespaces",
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import type { KubernetesNode, KubernetesNodeState } from "@homarr/definitions";
|
||||
import { logger } from "@homarr/log";
|
||||
|
||||
import { kubernetesMiddleware } from "../../../middlewares/kubernetes";
|
||||
import { createTRPCRouter, permissionRequiredProcedure } from "../../../trpc";
|
||||
@@ -57,7 +56,6 @@ export const nodesRouter = createTRPCRouter({
|
||||
};
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error("Unable to retrieve nodes", error);
|
||||
throw new TRPCError({
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
message: "An error occurred while fetching Kubernetes nodes",
|
||||
|
||||
@@ -2,13 +2,15 @@ import type { KubeConfig, V1OwnerReference } from "@kubernetes/client-node";
|
||||
import { AppsV1Api } from "@kubernetes/client-node";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import type { KubernetesPod } from "@homarr/definitions";
|
||||
import { logger } from "@homarr/log";
|
||||
|
||||
import { kubernetesMiddleware } from "../../../middlewares/kubernetes";
|
||||
import { createTRPCRouter, permissionRequiredProcedure } from "../../../trpc";
|
||||
import { KubernetesClient } from "../kubernetes-client";
|
||||
|
||||
const logger = createLogger({ module: "podsRouter" });
|
||||
|
||||
export const podsRouter = createTRPCRouter({
|
||||
getPods: permissionRequiredProcedure
|
||||
.requiresPermission("admin")
|
||||
@@ -55,7 +57,6 @@ export const podsRouter = createTRPCRouter({
|
||||
|
||||
return pods;
|
||||
} catch (error) {
|
||||
logger.error("Unable to retrieve pods", error);
|
||||
throw new TRPCError({
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
message: "An error occurred while fetching Kubernetes pods",
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import type { KubernetesSecret } from "@homarr/definitions";
|
||||
import { logger } from "@homarr/log";
|
||||
|
||||
import { kubernetesMiddleware } from "../../../middlewares/kubernetes";
|
||||
import { createTRPCRouter, permissionRequiredProcedure } from "../../../trpc";
|
||||
@@ -25,7 +24,6 @@ export const secretsRouter = createTRPCRouter({
|
||||
};
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error("Unable to retrieve secrets", error);
|
||||
throw new TRPCError({
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
message: "An error occurred while fetching Kubernetes secrets",
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import type { KubernetesService } from "@homarr/definitions";
|
||||
import { logger } from "@homarr/log";
|
||||
|
||||
import { kubernetesMiddleware } from "../../../middlewares/kubernetes";
|
||||
import { createTRPCRouter, permissionRequiredProcedure } from "../../../trpc";
|
||||
@@ -29,7 +28,6 @@ export const servicesRouter = createTRPCRouter({
|
||||
};
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error("Unable to retrieve services", error);
|
||||
throw new TRPCError({
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
message: "An error occurred while fetching Kubernetes services",
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import type { KubernetesVolume } from "@homarr/definitions";
|
||||
import { logger } from "@homarr/log";
|
||||
|
||||
import { kubernetesMiddleware } from "../../../middlewares/kubernetes";
|
||||
import { createTRPCRouter, permissionRequiredProcedure } from "../../../trpc";
|
||||
@@ -31,7 +30,6 @@ export const volumesRouter = createTRPCRouter({
|
||||
};
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error("Unable to retrieve volumes", error);
|
||||
throw new TRPCError({
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
message: "An error occurred while fetching Kubernetes Volumes",
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
import { observable } from "@trpc/server/observable";
|
||||
import z from "zod/v4";
|
||||
|
||||
import { logger } from "@homarr/log";
|
||||
import { logLevels } from "@homarr/log/constants";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { logLevels } from "@homarr/core/infrastructure/logs/constants";
|
||||
import type { LoggerMessage } from "@homarr/redis";
|
||||
import { loggingChannel } from "@homarr/redis";
|
||||
import { zodEnumFromArray } from "@homarr/validation/enums";
|
||||
|
||||
import { createTRPCRouter, permissionRequiredProcedure } from "../trpc";
|
||||
|
||||
const logger = createLogger({ module: "logRouter" });
|
||||
|
||||
export const logRouter = createTRPCRouter({
|
||||
subscribe: permissionRequiredProcedure
|
||||
.requiresPermission("other-view-logs")
|
||||
|
||||
@@ -2,11 +2,11 @@ import { TRPCError } from "@trpc/server";
|
||||
import { z } from "zod/v4";
|
||||
|
||||
import { createId } from "@homarr/common";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { asc, eq, like } from "@homarr/db";
|
||||
import { getServerSettingByKeyAsync, updateServerSettingByKeyAsync } from "@homarr/db/queries";
|
||||
import { searchEngines, users } from "@homarr/db/schema";
|
||||
import { createIntegrationAsync } from "@homarr/integrations";
|
||||
import { logger } from "@homarr/log";
|
||||
import { byIdSchema, paginatedSchema, searchSchema } from "@homarr/validation/common";
|
||||
import { searchEngineEditSchema, searchEngineManageSchema } from "@homarr/validation/search-engine";
|
||||
import { mediaRequestOptionsSchema, mediaRequestRequestSchema } from "@homarr/validation/widgets/media-request";
|
||||
@@ -14,6 +14,8 @@ import { mediaRequestOptionsSchema, mediaRequestRequestSchema } from "@homarr/va
|
||||
import { createOneIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { createTRPCRouter, permissionRequiredProcedure, protectedProcedure, publicProcedure } from "../../trpc";
|
||||
|
||||
const logger = createLogger({ module: "searchEngineRouter" });
|
||||
|
||||
export const searchEngineRouter = createTRPCRouter({
|
||||
getPaginated: protectedProcedure.input(paginatedSchema).query(async ({ input, ctx }) => {
|
||||
const whereQuery = input.search ? like(searchEngines.name, `%${input.search.trim()}%`) : undefined;
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import { logger } from "@homarr/log";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { updateCheckerRequestHandler } from "@homarr/request-handler/update-checker";
|
||||
|
||||
import { createTRPCRouter, permissionRequiredProcedure } from "../trpc";
|
||||
|
||||
const logger = createLogger({ module: "updateCheckerRouter" });
|
||||
|
||||
export const updateCheckerRouter = createTRPCRouter({
|
||||
getAvailableUpdates: permissionRequiredProcedure.requiresPermission("admin").query(async () => {
|
||||
try {
|
||||
|
||||
@@ -3,6 +3,7 @@ import { z } from "zod/v4";
|
||||
|
||||
import { createSaltAsync, hashPasswordAsync } from "@homarr/auth";
|
||||
import { createId } from "@homarr/common";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import type { Database } from "@homarr/db";
|
||||
import { and, eq, like } from "@homarr/db";
|
||||
import { getMaxGroupPositionAsync } from "@homarr/db/queries";
|
||||
@@ -10,7 +11,6 @@ import { boards, groupMembers, groupPermissions, groups, invites, users } from "
|
||||
import { selectUserSchema } from "@homarr/db/validationSchemas";
|
||||
import { credentialsAdminGroup } from "@homarr/definitions";
|
||||
import type { SupportedAuthProvider } from "@homarr/definitions";
|
||||
import { logger } from "@homarr/log";
|
||||
import { byIdSchema } from "@homarr/validation/common";
|
||||
import type { userBaseCreateSchema } from "@homarr/validation/user";
|
||||
import {
|
||||
@@ -39,6 +39,8 @@ import { throwIfCredentialsDisabled } from "./invite/checks";
|
||||
import { nextOnboardingStepAsync } from "./onboard/onboard-queries";
|
||||
import { changeSearchPreferencesAsync, changeSearchPreferencesInputSchema } from "./user/change-search-preferences";
|
||||
|
||||
const logger = createLogger({ module: "userRouter" });
|
||||
|
||||
export const userRouter = createTRPCRouter({
|
||||
initUser: onboardingProcedure
|
||||
.requiresStep("user")
|
||||
@@ -364,9 +366,11 @@ export const userRouter = createTRPCRouter({
|
||||
// Admins can change the password of other users without providing the previous password
|
||||
const isPreviousPasswordRequired = ctx.session.user.id === input.userId;
|
||||
|
||||
logger.info(
|
||||
`User ${user.id} is changing password for user ${input.userId}, previous password is required: ${isPreviousPasswordRequired}`,
|
||||
);
|
||||
logger.info("Changing user password", {
|
||||
actorId: ctx.session.user.id,
|
||||
targetUserId: input.userId,
|
||||
previousPasswordRequired: isPreviousPasswordRequired,
|
||||
});
|
||||
|
||||
if (isPreviousPasswordRequired) {
|
||||
const previousPasswordHash = await hashPasswordAsync(input.previousPassword, dbUser.salt ?? "");
|
||||
|
||||
@@ -4,7 +4,6 @@ import { observable } from "@trpc/server/observable";
|
||||
import { getIntegrationKindsByCategory } from "@homarr/definitions";
|
||||
import { createIntegrationAsync } from "@homarr/integrations";
|
||||
import type { Indexer } from "@homarr/integrations/types";
|
||||
import { logger } from "@homarr/log";
|
||||
import { indexerManagerRequestHandler } from "@homarr/request-handler/indexer-manager";
|
||||
|
||||
import type { IntegrationAction } from "../../middlewares/integration";
|
||||
@@ -61,10 +60,10 @@ export const indexerManagerRouter = createTRPCRouter({
|
||||
ctx.integrations.map(async (integration) => {
|
||||
const client = await createIntegrationAsync(integration);
|
||||
await client.testAllAsync().catch((err) => {
|
||||
logger.error("indexer-manager router - ", err);
|
||||
throw new TRPCError({
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
message: `Failed to test all indexers for ${integration.name} (${integration.id})`,
|
||||
cause: err,
|
||||
});
|
||||
});
|
||||
}),
|
||||
|
||||
@@ -14,12 +14,14 @@ import { ZodError } from "zod/v4";
|
||||
import type { Session } from "@homarr/auth";
|
||||
import { FlattenError } from "@homarr/common";
|
||||
import { userAgent } from "@homarr/common/server";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { db } from "@homarr/db";
|
||||
import type { GroupPermissionKey, OnboardingStep } from "@homarr/definitions";
|
||||
import { logger } from "@homarr/log";
|
||||
|
||||
import { getOnboardingOrFallbackAsync } from "./router/onboard/onboard-queries";
|
||||
|
||||
const logger = createLogger({ module: "trpc" });
|
||||
|
||||
/**
|
||||
* 1. CONTEXT
|
||||
*
|
||||
@@ -36,7 +38,7 @@ export const createTRPCContext = (opts: { headers: Headers; session: Session | n
|
||||
const session = opts.session;
|
||||
const source = opts.headers.get("x-trpc-source") ?? "unknown";
|
||||
|
||||
logger.info(`tRPC request from ${source} by user '${session?.user.name} (${session?.user.id})'`, session?.user);
|
||||
logger.info("Received tRPC request", { source, userId: session?.user.id, userName: session?.user.name });
|
||||
|
||||
return {
|
||||
session,
|
||||
|
||||
@@ -3,9 +3,9 @@ import { cookies } from "next/headers";
|
||||
import NextAuth from "next-auth";
|
||||
import Credentials from "next-auth/providers/credentials";
|
||||
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { db } from "@homarr/db";
|
||||
import type { SupportedAuthProvider } from "@homarr/definitions";
|
||||
import { logger } from "@homarr/log";
|
||||
|
||||
import { createAdapter } from "./adapter";
|
||||
import { createSessionCallback } from "./callbacks";
|
||||
@@ -18,6 +18,8 @@ import { OidcProvider } from "./providers/oidc/oidc-provider";
|
||||
import { createRedirectUri } from "./redirect";
|
||||
import { expireDateAfter, generateSessionToken, sessionTokenCookieName } from "./session";
|
||||
|
||||
const logger = createLogger({ module: "authConfiguration" });
|
||||
|
||||
// See why it's unknown in the [...nextauth]/route.ts file
|
||||
export const createConfiguration = (
|
||||
provider: SupportedAuthProvider | "unknown",
|
||||
|
||||
@@ -2,15 +2,17 @@ import { cookies } from "next/headers";
|
||||
import dayjs from "dayjs";
|
||||
import type { NextAuthConfig } from "next-auth";
|
||||
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { and, eq, inArray } from "@homarr/db";
|
||||
import type { Database } from "@homarr/db";
|
||||
import { groupMembers, groups, users } from "@homarr/db/schema";
|
||||
import { colorSchemeCookieKey, everyoneGroup } from "@homarr/definitions";
|
||||
import { logger } from "@homarr/log";
|
||||
|
||||
import { env } from "./env";
|
||||
import { extractProfileName } from "./providers/oidc/oidc-provider";
|
||||
|
||||
const logger = createLogger({ module: "authEvents" });
|
||||
|
||||
export const createSignInEventHandler = (db: Database): Exclude<NextAuthConfig["events"], undefined>["signIn"] => {
|
||||
return async ({ user, profile }) => {
|
||||
logger.debug(`SignIn EventHandler for user: ${JSON.stringify(user)} . profile: ${JSON.stringify(profile)}`);
|
||||
@@ -43,9 +45,11 @@ export const createSignInEventHandler = (db: Database): Exclude<NextAuthConfig["
|
||||
|
||||
if (dbUser.name !== user.name) {
|
||||
await db.update(users).set({ name: user.name }).where(eq(users.id, user.id));
|
||||
logger.info(
|
||||
`Username for user of credentials provider has changed. user=${user.id} old=${dbUser.name} new=${user.name}`,
|
||||
);
|
||||
logger.info("Username for user of credentials provider has changed.", {
|
||||
userId: user.id,
|
||||
oldName: dbUser.name,
|
||||
newName: user.name,
|
||||
});
|
||||
}
|
||||
|
||||
if (profile) {
|
||||
@@ -56,9 +60,11 @@ export const createSignInEventHandler = (db: Database): Exclude<NextAuthConfig["
|
||||
|
||||
if (dbUser.name !== profileUsername) {
|
||||
await db.update(users).set({ name: profileUsername }).where(eq(users.id, user.id));
|
||||
logger.info(
|
||||
`Username for user of oidc provider has changed. user=${user.id} old='${dbUser.name}' new='${profileUsername}'`,
|
||||
);
|
||||
logger.info("Username for user of oidc provider has changed.", {
|
||||
userId: user.id,
|
||||
oldName: dbUser.name,
|
||||
newName: profileUsername,
|
||||
});
|
||||
}
|
||||
|
||||
if (
|
||||
@@ -67,11 +73,13 @@ export const createSignInEventHandler = (db: Database): Exclude<NextAuthConfig["
|
||||
!dbUser.image?.startsWith("data:")
|
||||
) {
|
||||
await db.update(users).set({ image: profile.picture }).where(eq(users.id, user.id));
|
||||
logger.info(`Profile picture for user of oidc provider has changed. user=${user.id}'`);
|
||||
logger.info("Profile picture for user of oidc provider has changed.", {
|
||||
userId: user.id,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
logger.info(`User '${dbUser.name}' logged in at ${dayjs().format()}`);
|
||||
logger.info("User logged in", { userId: user.id, userName: dbUser.name, timestamp: dayjs().format() });
|
||||
|
||||
// We use a cookie as localStorage is not shared with server (otherwise flickering would occur)
|
||||
(await cookies()).set(colorSchemeCookieKey, dbUser.colorScheme, {
|
||||
@@ -96,7 +104,7 @@ const addUserToEveryoneGroupIfNotMemberAsync = async (db: Database, userId: stri
|
||||
userId,
|
||||
groupId: dbEveryoneGroup.id,
|
||||
});
|
||||
logger.info(`Added user to everyone group. user=${userId}`);
|
||||
logger.info("Added user to everyone group.", { userId });
|
||||
}
|
||||
};
|
||||
|
||||
@@ -118,9 +126,10 @@ const synchronizeGroupsWithExternalForUserAsync = async (db: Database, userId: s
|
||||
);
|
||||
|
||||
if (missingExternalGroupsForUser.length > 0) {
|
||||
logger.debug(
|
||||
`Homarr does not have the user in certain groups. user=${userId} count=${missingExternalGroupsForUser.length}`,
|
||||
);
|
||||
logger.debug("Homarr does not have the user in certain groups.", {
|
||||
user: userId,
|
||||
count: missingExternalGroupsForUser.length,
|
||||
});
|
||||
|
||||
const groupIds = await db.query.groups.findMany({
|
||||
columns: {
|
||||
@@ -129,7 +138,10 @@ const synchronizeGroupsWithExternalForUserAsync = async (db: Database, userId: s
|
||||
where: inArray(groups.name, missingExternalGroupsForUser),
|
||||
});
|
||||
|
||||
logger.debug(`Homarr has found groups in the database user is not in. user=${userId} count=${groupIds.length}`);
|
||||
logger.debug("Homarr has found groups in the database user is not in.", {
|
||||
user: userId,
|
||||
count: groupIds.length,
|
||||
});
|
||||
|
||||
if (groupIds.length > 0) {
|
||||
await db.insert(groupMembers).values(
|
||||
@@ -139,9 +151,9 @@ const synchronizeGroupsWithExternalForUserAsync = async (db: Database, userId: s
|
||||
})),
|
||||
);
|
||||
|
||||
logger.info(`Added user to groups successfully. user=${userId} count=${groupIds.length}`);
|
||||
logger.info("Added user to groups successfully.", { user: userId, count: groupIds.length });
|
||||
} else {
|
||||
logger.debug(`User is already in all groups of Homarr. user=${userId}`);
|
||||
logger.debug("User is already in all groups of Homarr.", { user: userId });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,9 +166,10 @@ const synchronizeGroupsWithExternalForUserAsync = async (db: Database, userId: s
|
||||
);
|
||||
|
||||
if (groupsUserIsNoLongerMemberOfExternally.length > 0) {
|
||||
logger.debug(
|
||||
`Homarr has the user in certain groups that LDAP does not have. user=${userId} count=${groupsUserIsNoLongerMemberOfExternally.length}`,
|
||||
);
|
||||
logger.debug("Homarr has the user in certain groups that LDAP does not have.", {
|
||||
user: userId,
|
||||
count: groupsUserIsNoLongerMemberOfExternally.length,
|
||||
});
|
||||
|
||||
await db.delete(groupMembers).where(
|
||||
and(
|
||||
@@ -168,8 +181,9 @@ const synchronizeGroupsWithExternalForUserAsync = async (db: Database, userId: s
|
||||
),
|
||||
);
|
||||
|
||||
logger.info(
|
||||
`Removed user from groups successfully. user=${userId} count=${groupsUserIsNoLongerMemberOfExternally.length}`,
|
||||
);
|
||||
logger.info("Removed user from groups successfully.", {
|
||||
user: userId,
|
||||
count: groupsUserIsNoLongerMemberOfExternally.length,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -30,7 +30,6 @@
|
||||
"@homarr/core": "workspace:^0.1.0",
|
||||
"@homarr/db": "workspace:^0.1.0",
|
||||
"@homarr/definitions": "workspace:^0.1.0",
|
||||
"@homarr/log": "workspace:^0.1.0",
|
||||
"@homarr/validation": "workspace:^0.1.0",
|
||||
"bcrypt": "^6.0.0",
|
||||
"cookies": "^0.9.1",
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
import bcrypt from "bcrypt";
|
||||
import type { z } from "zod/v4";
|
||||
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import type { Database } from "@homarr/db";
|
||||
import { and, eq } from "@homarr/db";
|
||||
import { users } from "@homarr/db/schema";
|
||||
import { logger } from "@homarr/log";
|
||||
import type { userSignInSchema } from "@homarr/validation/user";
|
||||
|
||||
const logger = createLogger({ module: "basicAuthorization" });
|
||||
|
||||
export const authorizeWithBasicCredentialsAsync = async (
|
||||
db: Database,
|
||||
credentials: z.infer<typeof userSignInSchema>,
|
||||
@@ -16,19 +18,19 @@ export const authorizeWithBasicCredentialsAsync = async (
|
||||
});
|
||||
|
||||
if (!user?.password) {
|
||||
logger.info(`user ${credentials.name} was not found`);
|
||||
logger.info("User not found", { userName: credentials.name });
|
||||
return null;
|
||||
}
|
||||
|
||||
logger.info(`user ${user.name} is trying to log in. checking password...`);
|
||||
logger.info("User is trying to log in. Checking password...", { userName: user.name });
|
||||
const isValidPassword = await bcrypt.compare(credentials.password, user.password);
|
||||
|
||||
if (!isValidPassword) {
|
||||
logger.warn(`password for user ${user.name} was incorrect`);
|
||||
logger.warn("Password for user was incorrect", { userName: user.name });
|
||||
return null;
|
||||
}
|
||||
|
||||
logger.info(`user ${user.name} successfully authorized`);
|
||||
logger.info("User successfully authorized", { userName: user.name });
|
||||
|
||||
return {
|
||||
id: user.id,
|
||||
|
||||
@@ -1,21 +1,23 @@
|
||||
import { CredentialsSignin } from "@auth/core/errors";
|
||||
import { z } from "zod/v4";
|
||||
|
||||
import { createId, extractErrorMessage } from "@homarr/common";
|
||||
import { createId } from "@homarr/common";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import type { Database, InferInsertModel } from "@homarr/db";
|
||||
import { and, eq } from "@homarr/db";
|
||||
import { users } from "@homarr/db/schema";
|
||||
import { logger } from "@homarr/log";
|
||||
import type { ldapSignInSchema } from "@homarr/validation/user";
|
||||
|
||||
import { env } from "../../../env";
|
||||
import { LdapClient } from "../ldap-client";
|
||||
|
||||
const logger = createLogger({ module: "ldapAuthorization" });
|
||||
|
||||
export const authorizeWithLdapCredentialsAsync = async (
|
||||
db: Database,
|
||||
credentials: z.infer<typeof ldapSignInSchema>,
|
||||
) => {
|
||||
logger.info(`user ${credentials.name} is trying to log in using LDAP. Connecting to LDAP server...`);
|
||||
logger.info("User is trying to log in using LDAP. Connecting to LDAP server...", { userName: credentials.name });
|
||||
const client = new LdapClient();
|
||||
await client
|
||||
.bindAsync({
|
||||
@@ -23,8 +25,7 @@ export const authorizeWithLdapCredentialsAsync = async (
|
||||
password: env.AUTH_LDAP_BIND_PASSWORD,
|
||||
})
|
||||
.catch((error) => {
|
||||
logger.error(`Failed to connect to LDAP server ${extractErrorMessage(error)}`);
|
||||
throw new CredentialsSignin();
|
||||
throw new CredentialsSignin("Failed to connect to LDAP server", { cause: error });
|
||||
});
|
||||
|
||||
logger.info("Connected to LDAP server. Searching for user...");
|
||||
@@ -48,21 +49,21 @@ export const authorizeWithLdapCredentialsAsync = async (
|
||||
});
|
||||
|
||||
if (!ldapUser) {
|
||||
logger.warn(`User ${credentials.name} not found in LDAP`);
|
||||
throw new CredentialsSignin();
|
||||
throw new CredentialsSignin(`User not found in LDAP username="${credentials.name}"`);
|
||||
}
|
||||
|
||||
// Validate email
|
||||
const mailResult = await z.string().email().safeParseAsync(ldapUser[env.AUTH_LDAP_USER_MAIL_ATTRIBUTE]);
|
||||
|
||||
if (!mailResult.success) {
|
||||
logger.error(
|
||||
`User ${credentials.name} found but with invalid or non-existing Email. Not Supported: "${ldapUser[env.AUTH_LDAP_USER_MAIL_ATTRIBUTE]}"`,
|
||||
);
|
||||
throw new CredentialsSignin();
|
||||
logger.error("User found in LDAP but with invalid or non-existing Email", {
|
||||
userName: credentials.name,
|
||||
emailValue: ldapUser[env.AUTH_LDAP_USER_MAIL_ATTRIBUTE],
|
||||
});
|
||||
throw new CredentialsSignin("User found in LDAP but with invalid or non-existing Email");
|
||||
}
|
||||
|
||||
logger.info(`User ${credentials.name} found in LDAP. Logging in...`);
|
||||
logger.info("User found in LDAP. Logging in...", { userName: credentials.name });
|
||||
|
||||
// Bind with user credentials to check if the password is correct
|
||||
const userClient = new LdapClient();
|
||||
@@ -72,12 +73,12 @@ export const authorizeWithLdapCredentialsAsync = async (
|
||||
password: credentials.password,
|
||||
})
|
||||
.catch(() => {
|
||||
logger.warn(`Wrong credentials for user ${credentials.name}`);
|
||||
logger.warn("Wrong credentials for user", { userName: credentials.name });
|
||||
throw new CredentialsSignin();
|
||||
});
|
||||
await userClient.disconnectAsync();
|
||||
|
||||
logger.info(`User ${credentials.name} logged in successfully, retrieving user groups...`);
|
||||
logger.info("User credentials are correct. Retrieving user groups...", { userName: credentials.name });
|
||||
|
||||
const userGroups = await client
|
||||
.searchAsync({
|
||||
@@ -93,7 +94,7 @@ export const authorizeWithLdapCredentialsAsync = async (
|
||||
})
|
||||
.then((entries) => entries.map((entry) => entry.cn).filter((group): group is string => group !== undefined));
|
||||
|
||||
logger.info(`Found ${userGroups.length} groups for user ${credentials.name}.`);
|
||||
logger.info("User groups retrieved", { userName: credentials.name, groups: userGroups.length });
|
||||
|
||||
await client.disconnectAsync();
|
||||
|
||||
@@ -111,7 +112,7 @@ export const authorizeWithLdapCredentialsAsync = async (
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
logger.info(`User ${credentials.name} not found in the database. Creating...`);
|
||||
logger.info("User not found in the database. Creating...", { userName: credentials.name });
|
||||
|
||||
const insertUser = {
|
||||
id: createId(),
|
||||
@@ -126,7 +127,7 @@ export const authorizeWithLdapCredentialsAsync = async (
|
||||
|
||||
user = insertUser;
|
||||
|
||||
logger.info(`User ${credentials.name} created successfully.`);
|
||||
logger.info("User created successfully", { userName: credentials.name });
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
@@ -28,7 +28,6 @@
|
||||
"prettier": "@homarr/prettier-config",
|
||||
"dependencies": {
|
||||
"@homarr/core": "workspace:^0.1.0",
|
||||
"@homarr/log": "workspace:^0.1.0",
|
||||
"@paralleldrive/cuid2": "^3.1.0",
|
||||
"dayjs": "^1.11.19",
|
||||
"dns-caching": "^0.2.9",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { DnsCacheManager } from "dns-caching";
|
||||
|
||||
import { logger } from "@homarr/log";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
|
||||
import { env } from "../env";
|
||||
|
||||
@@ -12,6 +12,8 @@ declare global {
|
||||
};
|
||||
}
|
||||
|
||||
const logger = createLogger({ module: "dns" });
|
||||
|
||||
// Initialize global.homarr if not present
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||
global.homarr ??= {};
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import { AxiosError } from "axios";
|
||||
|
||||
import { logger } from "@homarr/log";
|
||||
|
||||
import type { AnyRequestError } from "../request-error";
|
||||
import { RequestError } from "../request-error";
|
||||
import { ResponseError } from "../response-error";
|
||||
@@ -9,11 +7,15 @@ import { matchErrorCode } from "./fetch-http-error-handler";
|
||||
import { HttpErrorHandler } from "./http-error-handler";
|
||||
|
||||
export class AxiosHttpErrorHandler extends HttpErrorHandler {
|
||||
constructor() {
|
||||
super("axios");
|
||||
}
|
||||
|
||||
handleRequestError(error: unknown): AnyRequestError | undefined {
|
||||
if (!(error instanceof AxiosError)) return undefined;
|
||||
if (error.code === undefined) return undefined;
|
||||
|
||||
logger.debug("Received Axios request error", {
|
||||
this.logRequestError({
|
||||
code: error.code,
|
||||
message: error.message,
|
||||
});
|
||||
@@ -28,8 +30,7 @@ export class AxiosHttpErrorHandler extends HttpErrorHandler {
|
||||
handleResponseError(error: unknown): ResponseError | undefined {
|
||||
if (!(error instanceof AxiosError)) return undefined;
|
||||
if (error.response === undefined) return undefined;
|
||||
|
||||
logger.debug("Received Axios response error", {
|
||||
this.logResponseError({
|
||||
status: error.response.status,
|
||||
url: error.response.config.url,
|
||||
message: error.message,
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import { logger } from "@homarr/log";
|
||||
|
||||
import { objectEntries } from "../../../object";
|
||||
import type { Modify } from "../../../types";
|
||||
import type { AnyRequestError, AnyRequestErrorInput, RequestErrorCode, RequestErrorReason } from "../request-error";
|
||||
@@ -9,13 +7,13 @@ import { HttpErrorHandler } from "./http-error-handler";
|
||||
|
||||
export class FetchHttpErrorHandler extends HttpErrorHandler {
|
||||
constructor(private type = "undici") {
|
||||
super();
|
||||
super(type);
|
||||
}
|
||||
|
||||
handleRequestError(error: unknown): AnyRequestError | undefined {
|
||||
if (!isTypeErrorWithCode(error)) return undefined;
|
||||
|
||||
logger.debug(`Received ${this.type} request error`, {
|
||||
this.logRequestError({
|
||||
code: error.cause.code,
|
||||
});
|
||||
|
||||
|
||||
@@ -1,7 +1,24 @@
|
||||
import type { Logger } from "@homarr/core/infrastructure/logs";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
|
||||
import type { AnyRequestError } from "../request-error";
|
||||
import type { ResponseError } from "../response-error";
|
||||
|
||||
export abstract class HttpErrorHandler {
|
||||
protected logger: Logger;
|
||||
|
||||
constructor(type: string) {
|
||||
this.logger = createLogger({ module: "httpErrorHandler", type });
|
||||
}
|
||||
|
||||
protected logRequestError<T extends { code: string }>(metadata: T) {
|
||||
this.logger.debug("Received request error", metadata);
|
||||
}
|
||||
|
||||
protected logResponseError<T extends { status: number; url: string | undefined }>(metadata: T) {
|
||||
this.logger.debug("Received response error", metadata);
|
||||
}
|
||||
|
||||
abstract handleRequestError(error: unknown): AnyRequestError | undefined;
|
||||
abstract handleResponseError(error: unknown): ResponseError | undefined;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import { FetchError } from "node-fetch";
|
||||
|
||||
import { logger } from "@homarr/log";
|
||||
|
||||
import { RequestError } from "../request-error";
|
||||
import type { AnyRequestError } from "../request-error";
|
||||
import type { ResponseError } from "../response-error";
|
||||
@@ -15,14 +13,14 @@ import { HttpErrorHandler } from "./http-error-handler";
|
||||
*/
|
||||
export class NodeFetchHttpErrorHandler extends HttpErrorHandler {
|
||||
constructor(private type = "node-fetch") {
|
||||
super();
|
||||
super(type);
|
||||
}
|
||||
|
||||
handleRequestError(error: unknown): AnyRequestError | undefined {
|
||||
if (!(error instanceof FetchError)) return undefined;
|
||||
if (error.code === undefined) return undefined;
|
||||
|
||||
logger.debug(`Received ${this.type} request error`, {
|
||||
this.logRequestError({
|
||||
code: error.code,
|
||||
message: error.message,
|
||||
});
|
||||
|
||||
@@ -5,6 +5,10 @@ import { ResponseError } from "../response-error";
|
||||
import { HttpErrorHandler } from "./http-error-handler";
|
||||
|
||||
export class OctokitHttpErrorHandler extends HttpErrorHandler {
|
||||
constructor() {
|
||||
super("octokit");
|
||||
}
|
||||
|
||||
/**
|
||||
* I wasn't able to get a request error triggered. Therefore we ignore them for now
|
||||
* and just forward them as unknown errors
|
||||
@@ -16,6 +20,11 @@ export class OctokitHttpErrorHandler extends HttpErrorHandler {
|
||||
handleResponseError(error: unknown): ResponseError | undefined {
|
||||
if (!(error instanceof OctokitRequestError)) return undefined;
|
||||
|
||||
this.logResponseError({
|
||||
status: error.status,
|
||||
url: error.response?.url,
|
||||
});
|
||||
|
||||
return new ResponseError({
|
||||
status: error.status,
|
||||
url: error.response?.url,
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import { FetchError } from "ofetch";
|
||||
|
||||
import { logger } from "@homarr/log";
|
||||
|
||||
import type { AnyRequestError } from "../request-error";
|
||||
import { ResponseError } from "../response-error";
|
||||
import { FetchHttpErrorHandler } from "./fetch-http-error-handler";
|
||||
@@ -14,6 +12,10 @@ import { HttpErrorHandler } from "./http-error-handler";
|
||||
* It is for example used within the ctrl packages like qbittorrent, deluge, transmission, etc.
|
||||
*/
|
||||
export class OFetchHttpErrorHandler extends HttpErrorHandler {
|
||||
constructor() {
|
||||
super("ofetch");
|
||||
}
|
||||
|
||||
handleRequestError(error: unknown): AnyRequestError | undefined {
|
||||
if (!(error instanceof FetchError)) return undefined;
|
||||
if (!(error.cause instanceof TypeError)) return undefined;
|
||||
@@ -28,7 +30,7 @@ export class OFetchHttpErrorHandler extends HttpErrorHandler {
|
||||
if (!(error instanceof FetchError)) return undefined;
|
||||
if (error.response === undefined) return undefined;
|
||||
|
||||
logger.debug("Received ofetch response error", {
|
||||
this.logResponseError({
|
||||
status: error.response.status,
|
||||
url: error.response.url,
|
||||
});
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
import { logger } from "@homarr/log";
|
||||
|
||||
import type { AnyRequestError } from "../request-error";
|
||||
import { ResponseError } from "../response-error";
|
||||
import { HttpErrorHandler } from "./http-error-handler";
|
||||
import { NodeFetchHttpErrorHandler } from "./node-fetch-http-error-handler";
|
||||
|
||||
export class TsdavHttpErrorHandler extends HttpErrorHandler {
|
||||
constructor() {
|
||||
super("tsdav");
|
||||
}
|
||||
|
||||
handleRequestError(error: unknown): AnyRequestError | undefined {
|
||||
return new NodeFetchHttpErrorHandler("tsdav").handleRequestError(error);
|
||||
}
|
||||
@@ -16,8 +18,9 @@ export class TsdavHttpErrorHandler extends HttpErrorHandler {
|
||||
// https://github.com/natelindev/tsdav/blob/bf33f04b1884694d685ee6f2b43fe9354b12d167/src/account.ts#L86
|
||||
if (error.message !== "Invalid credentials") return undefined;
|
||||
|
||||
logger.debug("Received tsdav response error", {
|
||||
this.logResponseError({
|
||||
status: 401,
|
||||
url: undefined,
|
||||
});
|
||||
|
||||
return new ResponseError({ status: 401, url: "?" });
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
import { logger } from "@homarr/log";
|
||||
|
||||
import { ParseError } from "../parse-error";
|
||||
import { ParseErrorHandler } from "./parse-error-handler";
|
||||
|
||||
export class JsonParseErrorHandler extends ParseErrorHandler {
|
||||
constructor() {
|
||||
super("json");
|
||||
}
|
||||
|
||||
handleParseError(error: unknown): ParseError | undefined {
|
||||
if (!(error instanceof SyntaxError)) return undefined;
|
||||
|
||||
logger.debug("Received JSON parse error", {
|
||||
this.logParseError({
|
||||
message: error.message,
|
||||
});
|
||||
|
||||
|
||||
@@ -1,5 +1,17 @@
|
||||
import type { Logger } from "@homarr/core/infrastructure/logs";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
|
||||
import type { ParseError } from "../parse-error";
|
||||
|
||||
export abstract class ParseErrorHandler {
|
||||
protected logger: Logger;
|
||||
constructor(type: string) {
|
||||
this.logger = createLogger({ module: "parseErrorHandler", type });
|
||||
}
|
||||
|
||||
protected logParseError(metadata?: Record<string, unknown>) {
|
||||
this.logger.debug("Received parse error", metadata);
|
||||
}
|
||||
|
||||
abstract handleParseError(error: unknown): ParseError | undefined;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
import { fromError } from "zod-validation-error";
|
||||
import { ZodError } from "zod/v4";
|
||||
|
||||
import { logger } from "@homarr/log";
|
||||
|
||||
import { ParseError } from "../parse-error";
|
||||
import { ParseErrorHandler } from "./parse-error-handler";
|
||||
|
||||
export class ZodParseErrorHandler extends ParseErrorHandler {
|
||||
constructor() {
|
||||
super("zod");
|
||||
}
|
||||
|
||||
handleParseError(error: unknown): ParseError | undefined {
|
||||
if (!(error instanceof ZodError)) return undefined;
|
||||
|
||||
@@ -17,7 +19,7 @@ export class ZodParseErrorHandler extends ParseErrorHandler {
|
||||
prefix: null,
|
||||
}).toString();
|
||||
|
||||
logger.debug("Received Zod parse error");
|
||||
this.logParseError();
|
||||
|
||||
return new ParseError(message, { cause: error });
|
||||
}
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
import type { Dispatcher } from "undici";
|
||||
import { Agent } from "undici";
|
||||
|
||||
import { logger } from "@homarr/log";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
|
||||
// The below import statement initializes dns-caching
|
||||
import "./dns";
|
||||
|
||||
const logger = createLogger({ module: "fetchAgent" });
|
||||
|
||||
export class LoggingAgent extends Agent {
|
||||
constructor(...props: ConstructorParameters<typeof Agent>) {
|
||||
super(...props);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { Dispatcher } from "undici";
|
||||
import { describe, expect, test, vi } from "vitest";
|
||||
|
||||
import { logger } from "@homarr/log";
|
||||
import * as logs from "@homarr/core/infrastructure/logs";
|
||||
|
||||
import { LoggingAgent } from "../fetch-agent";
|
||||
|
||||
@@ -16,24 +16,36 @@ vi.mock("undici", () => {
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("@homarr/core/infrastructure/logs", async () => {
|
||||
const actual: typeof logs = await vi.importActual("@homarr/core/infrastructure/logs");
|
||||
return {
|
||||
...actual,
|
||||
createLogger: vi.fn().mockReturnValue({
|
||||
debug: vi.fn(),
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
const REDACTED = "REDACTED";
|
||||
|
||||
const loggerMock = logs.createLogger({ module: "test" });
|
||||
|
||||
describe("LoggingAgent should log all requests", () => {
|
||||
test("should log all requests", () => {
|
||||
// Arrange
|
||||
const infoLogSpy = vi.spyOn(logger, "debug");
|
||||
const debugSpy = vi.spyOn(loggerMock, "debug");
|
||||
const agent = new LoggingAgent();
|
||||
|
||||
// Act
|
||||
agent.dispatch({ origin: "https://homarr.dev", path: "/", method: "GET" }, {});
|
||||
|
||||
// Assert
|
||||
expect(infoLogSpy).toHaveBeenCalledWith("Dispatching request https://homarr.dev/ (0 headers)");
|
||||
expect(debugSpy).toHaveBeenCalledWith("Dispatching request https://homarr.dev/ (0 headers)");
|
||||
});
|
||||
|
||||
test("should show amount of headers", () => {
|
||||
// Arrange
|
||||
const infoLogSpy = vi.spyOn(logger, "debug");
|
||||
const debugSpy = vi.spyOn(loggerMock, "debug");
|
||||
const agent = new LoggingAgent();
|
||||
|
||||
// Act
|
||||
@@ -51,7 +63,7 @@ describe("LoggingAgent should log all requests", () => {
|
||||
);
|
||||
|
||||
// Assert
|
||||
expect(infoLogSpy).toHaveBeenCalledWith(expect.stringContaining("(2 headers)"));
|
||||
expect(debugSpy).toHaveBeenCalledWith(expect.stringContaining("(2 headers)"));
|
||||
});
|
||||
|
||||
test.each([
|
||||
@@ -69,14 +81,14 @@ describe("LoggingAgent should log all requests", () => {
|
||||
[`/${"a".repeat(32)}/?param=123`, `/${REDACTED}/?param=123`],
|
||||
])("should redact sensitive data in url https://homarr.dev%s", (path, expected) => {
|
||||
// Arrange
|
||||
const infoLogSpy = vi.spyOn(logger, "debug");
|
||||
const debugSpy = vi.spyOn(loggerMock, "debug");
|
||||
const agent = new LoggingAgent();
|
||||
|
||||
// Act
|
||||
agent.dispatch({ origin: "https://homarr.dev", path, method: "GET" }, {});
|
||||
|
||||
// Assert
|
||||
expect(infoLogSpy).toHaveBeenCalledWith(expect.stringContaining(` https://homarr.dev${expected} `));
|
||||
expect(debugSpy).toHaveBeenCalledWith(expect.stringContaining(` https://homarr.dev${expected} `));
|
||||
});
|
||||
test.each([
|
||||
["empty", "/?empty"],
|
||||
@@ -88,13 +100,13 @@ describe("LoggingAgent should log all requests", () => {
|
||||
["date times", "/?datetime=2022-01-01T00:00:00.000Z"],
|
||||
])("should not redact values that are %s", (_reason, path) => {
|
||||
// Arrange
|
||||
const infoLogSpy = vi.spyOn(logger, "debug");
|
||||
const debugSpy = vi.spyOn(loggerMock, "debug");
|
||||
const agent = new LoggingAgent();
|
||||
|
||||
// Act
|
||||
agent.dispatch({ origin: "https://homarr.dev", path, method: "GET" }, {});
|
||||
|
||||
// Assert
|
||||
expect(infoLogSpy).toHaveBeenCalledWith(expect.stringContaining(` https://homarr.dev${path} `));
|
||||
expect(debugSpy).toHaveBeenCalledWith(expect.stringContaining(` https://homarr.dev${path} `));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -7,7 +7,10 @@
|
||||
"exports": {
|
||||
"./infrastructure/redis": "./src/infrastructure/redis/client.ts",
|
||||
"./infrastructure/env": "./src/infrastructure/env/index.ts",
|
||||
".": "./src/index.ts"
|
||||
"./infrastructure/logs": "./src/infrastructure/logs/index.ts",
|
||||
"./infrastructure/logs/constants": "./src/infrastructure/logs/constants.ts",
|
||||
"./infrastructure/logs/env": "./src/infrastructure/logs/env.ts",
|
||||
"./infrastructure/logs/error": "./src/infrastructure/logs/error.ts"
|
||||
},
|
||||
"typesVersions": {
|
||||
"*": {
|
||||
@@ -26,6 +29,8 @@
|
||||
"dependencies": {
|
||||
"@t3-oss/env-nextjs": "^0.13.8",
|
||||
"ioredis": "5.8.2",
|
||||
"superjson": "2.2.6",
|
||||
"winston": "3.19.0",
|
||||
"zod": "^4.1.13"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
11
packages/core/src/infrastructure/logs/env.ts
Normal file
11
packages/core/src/infrastructure/logs/env.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { z } from "zod/v4";
|
||||
|
||||
import { createEnv, runtimeEnvWithPrefix } from "../env";
|
||||
import { logLevels } from "./constants";
|
||||
|
||||
export const logsEnv = createEnv({
|
||||
server: {
|
||||
LEVEL: z.enum(logLevels).default("info"),
|
||||
},
|
||||
runtimeEnv: runtimeEnvWithPrefix("LOG_"),
|
||||
});
|
||||
9
packages/core/src/infrastructure/logs/error.ts
Normal file
9
packages/core/src/infrastructure/logs/error.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
export class ErrorWithMetadata extends Error {
|
||||
public metadata: Record<string, unknown>;
|
||||
|
||||
constructor(message: string, metadata: Record<string, unknown> = {}, options?: ErrorOptions) {
|
||||
super(message, options);
|
||||
this.name = "Error";
|
||||
this.metadata = metadata;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,10 @@
|
||||
import { logsEnv } from "../env";
|
||||
import { formatMetadata } from "./metadata";
|
||||
|
||||
const ERROR_OBJECT_PRUNE_DEPTH = logsEnv.LEVEL === "debug" ? 10 : 3;
|
||||
const ERROR_STACK_LINE_LIMIT = logsEnv.LEVEL === "debug" ? undefined : 5;
|
||||
const ERROR_CAUSE_DEPTH = logsEnv.LEVEL === "debug" ? 10 : 5;
|
||||
|
||||
/**
|
||||
* Formats the cause of an error in the format
|
||||
* @example caused by Error: {message}
|
||||
@@ -10,7 +15,7 @@ import { formatMetadata } from "./metadata";
|
||||
*/
|
||||
export const formatErrorCause = (cause: unknown, iteration = 0): string => {
|
||||
// Prevent infinite recursion
|
||||
if (iteration > 5) {
|
||||
if (iteration > ERROR_CAUSE_DEPTH) {
|
||||
return "";
|
||||
}
|
||||
|
||||
@@ -22,8 +27,12 @@ export const formatErrorCause = (cause: unknown, iteration = 0): string => {
|
||||
return `\ncaused by ${formatErrorTitle(cause)}\n${formatErrorStack(cause.stack)}${formatErrorCause(cause.cause, iteration + 1)}`;
|
||||
}
|
||||
|
||||
if (cause instanceof Object) {
|
||||
return `\ncaused by ${JSON.stringify(cause)}`;
|
||||
if (typeof cause === "object" && cause !== null) {
|
||||
if ("cause" in cause) {
|
||||
const { cause: innerCause, ...rest } = cause;
|
||||
return `\ncaused by ${JSON.stringify(prune(rest, ERROR_OBJECT_PRUNE_DEPTH))}${formatErrorCause(innerCause, iteration + 1)}`;
|
||||
}
|
||||
return `\ncaused by ${JSON.stringify(prune(cause, ERROR_OBJECT_PRUNE_DEPTH))}`;
|
||||
}
|
||||
|
||||
return `\ncaused by ${cause as string}`;
|
||||
@@ -50,5 +59,28 @@ export const formatErrorTitle = (error: Error) => {
|
||||
* @param stack stack trace
|
||||
* @returns formatted stack trace
|
||||
*/
|
||||
export const formatErrorStack = (stack: string | undefined) => (stack ? removeFirstLine(stack) : "");
|
||||
const removeFirstLine = (stack: string) => stack.split("\n").slice(1).join("\n");
|
||||
export const formatErrorStack = (stack: string | undefined) =>
|
||||
stack
|
||||
?.split("\n")
|
||||
.slice(1, ERROR_STACK_LINE_LIMIT ? ERROR_STACK_LINE_LIMIT + 1 : undefined)
|
||||
.join("\n") ?? "";
|
||||
|
||||
/**
|
||||
* Removes nested properties from an object beyond a certain depth
|
||||
*/
|
||||
const prune = (value: unknown, depth: number): unknown => {
|
||||
if (typeof value !== "object" || value === null) {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
if (depth === 0) return [];
|
||||
return value.map((item) => prune(item, depth - 1));
|
||||
}
|
||||
|
||||
if (depth === 0) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return Object.fromEntries(Object.entries(value).map(([key, val]) => [key, prune(val, depth - 1)]));
|
||||
};
|
||||
25
packages/core/src/infrastructure/logs/format/index.ts
Normal file
25
packages/core/src/infrastructure/logs/format/index.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { format } from "winston";
|
||||
|
||||
import { formatErrorCause, formatErrorStack } from "./error";
|
||||
import { formatMetadata } from "./metadata";
|
||||
|
||||
export const logFormat = format.combine(
|
||||
format.colorize(),
|
||||
format.timestamp(),
|
||||
format.errors({ stack: true, cause: true }),
|
||||
format.printf(({ level, message, timestamp, cause, stack, ...metadata }) => {
|
||||
const firstLine = `${timestamp as string} ${level}: ${message as string} ${formatMetadata(metadata)}`;
|
||||
|
||||
if (!cause && !stack) {
|
||||
return firstLine;
|
||||
}
|
||||
|
||||
const formatedStack = formatErrorStack(stack as string | undefined);
|
||||
|
||||
if (!cause) {
|
||||
return `${firstLine}\n${formatedStack}`;
|
||||
}
|
||||
|
||||
return `${firstLine}\n${formatedStack}${formatErrorCause(cause)}`;
|
||||
}),
|
||||
);
|
||||
@@ -1,7 +1,11 @@
|
||||
import { ErrorWithMetadata } from "@homarr/core/infrastructure/logs/error";
|
||||
|
||||
export const formatMetadata = (metadata: Record<string, unknown> | Error, ignoreKeys?: string[]) => {
|
||||
const filteredMetadata = Object.keys(metadata)
|
||||
const metadataObject = metadata instanceof ErrorWithMetadata ? metadata.metadata : metadata;
|
||||
|
||||
const filteredMetadata = Object.keys(metadataObject)
|
||||
.filter((key) => !ignoreKeys?.includes(key))
|
||||
.map((key) => ({ key, value: metadata[key as keyof typeof metadata] }))
|
||||
.map((key) => ({ key, value: metadataObject[key as keyof typeof metadataObject] }))
|
||||
.filter(({ value }) => typeof value !== "object" && typeof value !== "function");
|
||||
|
||||
return filteredMetadata.map(({ key, value }) => `${key}="${value as string}"`).join(" ");
|
||||
18
packages/core/src/infrastructure/logs/index.ts
Normal file
18
packages/core/src/infrastructure/logs/index.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import winston from "winston";
|
||||
|
||||
import { logsEnv } from "./env";
|
||||
import { logFormat } from "./format";
|
||||
import { logTransports } from "./transports";
|
||||
|
||||
const logger = winston.createLogger({
|
||||
format: logFormat,
|
||||
transports: logTransports,
|
||||
level: logsEnv.LEVEL,
|
||||
});
|
||||
|
||||
interface DefaultMetadata {
|
||||
module: string;
|
||||
}
|
||||
|
||||
export const createLogger = (metadata: DefaultMetadata & Record<string, unknown>) => logger.child(metadata);
|
||||
export type Logger = winston.Logger;
|
||||
21
packages/core/src/infrastructure/logs/transports/index.ts
Normal file
21
packages/core/src/infrastructure/logs/transports/index.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { transports } from "winston";
|
||||
import type { transport } from "winston";
|
||||
|
||||
import { RedisTransport } from "./redis-transport";
|
||||
|
||||
const getTransports = () => {
|
||||
const defaultTransports: transport[] = [new transports.Console()];
|
||||
|
||||
// Only add the Redis transport if we are not in CI
|
||||
if (!(Boolean(process.env.CI) || Boolean(process.env.DISABLE_REDIS_LOGS))) {
|
||||
return defaultTransports.concat(
|
||||
new RedisTransport({
|
||||
level: "debug",
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
return defaultTransports;
|
||||
};
|
||||
|
||||
export const logTransports = getTransports();
|
||||
@@ -1,8 +1,8 @@
|
||||
import superjson from "superjson";
|
||||
import Transport from "winston-transport";
|
||||
|
||||
import type { RedisClient } from "@homarr/core/infrastructure/redis";
|
||||
import { createRedisClient } from "@homarr/core/infrastructure/redis";
|
||||
import type { RedisClient } from "../../redis/client";
|
||||
import { createRedisClient } from "../../redis/client";
|
||||
|
||||
const messageSymbol = Symbol.for("message");
|
||||
const levelSymbol = Symbol.for("level");
|
||||
@@ -13,6 +13,7 @@ const levelSymbol = Symbol.for("level");
|
||||
//
|
||||
export class RedisTransport extends Transport {
|
||||
private redis: RedisClient | null = null;
|
||||
public static readonly publishChannel = "pubSub:logging";
|
||||
|
||||
/**
|
||||
* Log the info to the Redis channel
|
||||
@@ -27,7 +28,7 @@ export class RedisTransport extends Transport {
|
||||
|
||||
this.redis
|
||||
.publish(
|
||||
"pubSub:logging",
|
||||
RedisTransport.publishChannel,
|
||||
superjson.stringify({
|
||||
message: info[messageSymbol],
|
||||
level: info[levelSymbol],
|
||||
@@ -28,7 +28,6 @@
|
||||
"@homarr/common": "workspace:^0.1.0",
|
||||
"@homarr/core": "workspace:^0.1.0",
|
||||
"@homarr/cron-jobs": "workspace:^0.1.0",
|
||||
"@homarr/log": "workspace:^0.1.0",
|
||||
"@tanstack/react-query": "^5.90.12",
|
||||
"@trpc/client": "^11.7.2",
|
||||
"@trpc/server": "^11.7.2",
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
"prettier": "@homarr/prettier-config",
|
||||
"dependencies": {
|
||||
"@homarr/common": "workspace:^0.1.0",
|
||||
"@homarr/core": "workspace:^",
|
||||
"@homarr/db": "workspace:^0.1.0",
|
||||
"node-cron": "^4.2.1"
|
||||
},
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { AxiosError } from "axios";
|
||||
import { createTask, validate } from "node-cron";
|
||||
|
||||
import { Stopwatch } from "@homarr/common";
|
||||
import type { MaybePromise } from "@homarr/common/types";
|
||||
import { ErrorWithMetadata } from "@homarr/core/infrastructure/logs/error";
|
||||
import { db } from "@homarr/db";
|
||||
|
||||
import type { Logger } from "./logger";
|
||||
@@ -33,33 +33,39 @@ const createCallback = <TAllowedNames extends string, TName extends TAllowedName
|
||||
return (callback: () => MaybePromise<void>) => {
|
||||
const catchingCallbackAsync = async () => {
|
||||
try {
|
||||
creatorOptions.logger.logDebug(`The callback of '${name}' cron job started`);
|
||||
creatorOptions.logger.logDebug("The callback of cron job started", {
|
||||
name,
|
||||
});
|
||||
const stopwatch = new Stopwatch();
|
||||
await creatorOptions.beforeCallback?.(name);
|
||||
const beforeCallbackTook = stopwatch.getElapsedInHumanWords();
|
||||
await callback();
|
||||
const callbackTook = stopwatch.getElapsedInHumanWords();
|
||||
creatorOptions.logger.logDebug(
|
||||
`The callback of '${name}' cron job succeeded (before callback took ${beforeCallbackTook}, callback took ${callbackTook})`,
|
||||
);
|
||||
creatorOptions.logger.logDebug("The callback of cron job succeeded", {
|
||||
name,
|
||||
beforeCallbackTook,
|
||||
callbackTook,
|
||||
});
|
||||
|
||||
const durationInMillis = stopwatch.getElapsedInMilliseconds();
|
||||
if (durationInMillis > expectedMaximumDurationInMillis) {
|
||||
creatorOptions.logger.logWarning(
|
||||
`The callback of '${name}' succeeded but took ${(durationInMillis - expectedMaximumDurationInMillis).toFixed(2)}ms longer than expected (${expectedMaximumDurationInMillis}ms). This may indicate that your network performance, host performance or something else is too slow. If this happens too often, it should be looked into.`,
|
||||
);
|
||||
creatorOptions.logger.logWarning("The callback of cron job took longer than expected", {
|
||||
name,
|
||||
durationInMillis,
|
||||
expectedMaximumDurationInMillis,
|
||||
});
|
||||
}
|
||||
await creatorOptions.onCallbackSuccess?.(name);
|
||||
} catch (error) {
|
||||
// Log AxiosError in a less detailed way to prevent very long output
|
||||
if (error instanceof AxiosError) {
|
||||
creatorOptions.logger.logError(
|
||||
`Failed to run job '${name}': [AxiosError] ${error.message} ${error.response?.status} ${error.response?.config.url}\n${error.stack}`,
|
||||
);
|
||||
} else {
|
||||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
||||
creatorOptions.logger.logError(`Failed to run job '${name}': ${error}`);
|
||||
}
|
||||
creatorOptions.logger.logError(
|
||||
new ErrorWithMetadata(
|
||||
"The callback of cron job failed",
|
||||
{
|
||||
name,
|
||||
},
|
||||
{ cause: error },
|
||||
),
|
||||
);
|
||||
await creatorOptions.onCallbackError?.(name, error);
|
||||
}
|
||||
};
|
||||
@@ -80,21 +86,28 @@ const createCallback = <TAllowedNames extends string, TName extends TAllowedName
|
||||
timezone: creatorOptions.timezone,
|
||||
},
|
||||
);
|
||||
creatorOptions.logger.logDebug(
|
||||
`The cron job '${name}' was created with expression ${defaultCronExpression} in timezone ${creatorOptions.timezone} and runOnStart ${options.runOnStart}`,
|
||||
);
|
||||
creatorOptions.logger.logDebug("The scheduled task for cron job was created", {
|
||||
name,
|
||||
cronExpression: defaultCronExpression,
|
||||
timezone: creatorOptions.timezone,
|
||||
runOnStart: options.runOnStart,
|
||||
});
|
||||
|
||||
return scheduledTask;
|
||||
},
|
||||
async onStartAsync() {
|
||||
if (options.beforeStart) {
|
||||
creatorOptions.logger.logDebug(`Running beforeStart for job: ${name}`);
|
||||
creatorOptions.logger.logDebug("Running beforeStart for job", {
|
||||
name,
|
||||
});
|
||||
await options.beforeStart();
|
||||
}
|
||||
|
||||
if (!options.runOnStart) return;
|
||||
|
||||
creatorOptions.logger.logDebug(`The cron job '${name}' is running because runOnStart is set to true`);
|
||||
creatorOptions.logger.logDebug("The cron job is configured to run on start, executing callback", {
|
||||
name,
|
||||
});
|
||||
await catchingCallbackAsync();
|
||||
},
|
||||
async executeAsync() {
|
||||
@@ -117,11 +130,17 @@ export const createCronJobCreator = <TAllowedNames extends string = string>(
|
||||
defaultCronExpression: TExpression,
|
||||
options: CreateCronJobOptions = { runOnStart: false },
|
||||
) => {
|
||||
creatorOptions.logger.logDebug(`Validating cron expression '${defaultCronExpression}' for job: ${name}`);
|
||||
creatorOptions.logger.logDebug("Validating cron expression for cron job", {
|
||||
name,
|
||||
cronExpression: defaultCronExpression,
|
||||
});
|
||||
if (!validate(defaultCronExpression)) {
|
||||
throw new Error(`Invalid cron expression '${defaultCronExpression}' for job '${name}'`);
|
||||
}
|
||||
creatorOptions.logger.logDebug(`Cron job expression '${defaultCronExpression}' for job ${name} is valid`);
|
||||
creatorOptions.logger.logDebug("Cron job expression for cron job is valid", {
|
||||
name,
|
||||
cronExpression: defaultCronExpression,
|
||||
});
|
||||
|
||||
const returnValue = {
|
||||
withCallback: createCallback<TAllowedNames, TName>(name, defaultCronExpression, options, creatorOptions),
|
||||
|
||||
@@ -19,11 +19,15 @@ export const createJobGroupCreator = <TAllowedNames extends string = string>(
|
||||
options: CreateCronJobGroupCreatorOptions,
|
||||
) => {
|
||||
return <TJobs extends Jobs<TAllowedNames>>(jobs: TJobs) => {
|
||||
options.logger.logDebug(`Creating job group with ${Object.keys(jobs).length} jobs.`);
|
||||
options.logger.logDebug("Creating job group.", {
|
||||
jobCount: Object.keys(jobs).length,
|
||||
});
|
||||
for (const [key, job] of objectEntries(jobs)) {
|
||||
if (typeof key !== "string" || typeof job.name !== "string") continue;
|
||||
|
||||
options.logger.logDebug(`Added job ${job.name} to the job registry.`);
|
||||
options.logger.logDebug("Registering job in the job registry.", {
|
||||
name: job.name,
|
||||
});
|
||||
jobRegistry.set(key, {
|
||||
...job,
|
||||
name: job.name,
|
||||
@@ -54,7 +58,9 @@ export const createJobGroupCreator = <TAllowedNames extends string = string>(
|
||||
if (!job) return;
|
||||
if (!tasks.has(job.name)) return;
|
||||
|
||||
options.logger.logInfo(`Starting schedule cron job ${job.name}.`);
|
||||
options.logger.logInfo("Starting schedule of cron job.", {
|
||||
name: job.name,
|
||||
});
|
||||
await job.onStartAsync();
|
||||
await tasks.get(name as string)?.start();
|
||||
},
|
||||
@@ -64,7 +70,9 @@ export const createJobGroupCreator = <TAllowedNames extends string = string>(
|
||||
continue;
|
||||
}
|
||||
|
||||
options.logger.logInfo(`Starting schedule of cron job ${job.name}.`);
|
||||
options.logger.logInfo("Starting schedule of cron job.", {
|
||||
name: job.name,
|
||||
});
|
||||
await job.onStartAsync();
|
||||
await tasks.get(job.name)?.start();
|
||||
}
|
||||
@@ -76,19 +84,25 @@ export const createJobGroupCreator = <TAllowedNames extends string = string>(
|
||||
throw new Error(`The job "${job.name}" can not be executed manually.`);
|
||||
}
|
||||
|
||||
options.logger.logInfo(`Running schedule cron job ${job.name} manually.`);
|
||||
options.logger.logInfo("Running schedule cron job manually.", {
|
||||
name: job.name,
|
||||
});
|
||||
await tasks.get(name as string)?.execute();
|
||||
},
|
||||
stopAsync: async (name: keyof TJobs) => {
|
||||
const job = jobRegistry.get(name as string);
|
||||
if (!job) return;
|
||||
|
||||
options.logger.logInfo(`Stopping schedule cron job ${job.name}.`);
|
||||
options.logger.logInfo("Stopping schedule of cron job.", {
|
||||
name: job.name,
|
||||
});
|
||||
await tasks.get(name as string)?.stop();
|
||||
},
|
||||
stopAllAsync: async () => {
|
||||
for (const job of jobRegistry.values()) {
|
||||
options.logger.logInfo(`Stopping schedule cron job ${job.name}.`);
|
||||
options.logger.logInfo("Stopping schedule of cron job.", {
|
||||
name: job.name,
|
||||
});
|
||||
await tasks.get(job.name)?.stop();
|
||||
}
|
||||
},
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import type { CreateCronJobCreatorOptions } from "./creator";
|
||||
import { createCronJobCreator } from "./creator";
|
||||
import { createJobGroupCreator } from "./group";
|
||||
import { ConsoleLogger } from "./logger";
|
||||
|
||||
export const createCronJobFunctions = <TAllowedNames extends string>(
|
||||
options: CreateCronJobCreatorOptions<TAllowedNames> = { logger: new ConsoleLogger() },
|
||||
options: CreateCronJobCreatorOptions<TAllowedNames>,
|
||||
) => {
|
||||
return {
|
||||
createCronJob: createCronJobCreator<TAllowedNames>(options),
|
||||
|
||||
@@ -1,24 +1,7 @@
|
||||
export interface Logger {
|
||||
logDebug(message: string): void;
|
||||
logInfo(message: string): void;
|
||||
logDebug(message: string, metadata?: Record<string, unknown>): void;
|
||||
logInfo(message: string, metadata?: Record<string, unknown>): void;
|
||||
logError(message: string, metadata?: Record<string, unknown>): void;
|
||||
logError(error: unknown): void;
|
||||
logWarning(message: string): void;
|
||||
}
|
||||
|
||||
export class ConsoleLogger implements Logger {
|
||||
public logDebug(message: string) {
|
||||
console.log(message);
|
||||
}
|
||||
|
||||
public logInfo(message: string) {
|
||||
console.log(message);
|
||||
}
|
||||
|
||||
public logError(error: unknown) {
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
public logWarning(message: string) {
|
||||
console.warn(message);
|
||||
}
|
||||
logWarning(message: string, metadata?: Record<string, unknown>): void;
|
||||
}
|
||||
|
||||
@@ -25,13 +25,13 @@
|
||||
"@homarr/analytics": "workspace:^0.1.0",
|
||||
"@homarr/auth": "workspace:^0.1.0",
|
||||
"@homarr/common": "workspace:^0.1.0",
|
||||
"@homarr/core": "workspace:^0.1.0",
|
||||
"@homarr/cron-job-status": "workspace:^0.1.0",
|
||||
"@homarr/cron-jobs-core": "workspace:^0.1.0",
|
||||
"@homarr/db": "workspace:^0.1.0",
|
||||
"@homarr/definitions": "workspace:^0.1.0",
|
||||
"@homarr/icons": "workspace:^0.1.0",
|
||||
"@homarr/integrations": "workspace:^0.1.0",
|
||||
"@homarr/log": "workspace:^0.1.0",
|
||||
"@homarr/ping": "workspace:^0.1.0",
|
||||
"@homarr/redis": "workspace:^0.1.0",
|
||||
"@homarr/request-handler": "workspace:^0.1.0",
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
import SuperJSON from "superjson";
|
||||
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { ErrorWithMetadata } from "@homarr/core/infrastructure/logs/error";
|
||||
import { EVERY_MINUTE } from "@homarr/cron-jobs-core/expressions";
|
||||
import { db, eq } from "@homarr/db";
|
||||
import { items } from "@homarr/db/schema";
|
||||
import { logger } from "@homarr/log";
|
||||
import { dockerContainersRequestHandler } from "@homarr/request-handler/docker";
|
||||
|
||||
import type { WidgetComponentProps } from "../../../widgets";
|
||||
import { createCronJob } from "../lib";
|
||||
|
||||
const logger = createLogger({ module: "dockerJobs" });
|
||||
|
||||
export const dockerContainersJob = createCronJob("dockerContainers", EVERY_MINUTE).withCallback(async () => {
|
||||
const dockerItems = await db.query.items.findMany({
|
||||
where: eq(items.kind, "dockerContainers"),
|
||||
@@ -21,7 +24,7 @@ export const dockerContainersJob = createCronJob("dockerContainers", EVERY_MINUT
|
||||
const innerHandler = dockerContainersRequestHandler.handler(options);
|
||||
await innerHandler.getCachedOrUpdatedDataAsync({ forceUpdate: true });
|
||||
} catch (error) {
|
||||
logger.error("Failed to update Docker container status", { item, error });
|
||||
logger.error(new ErrorWithMetadata("Failed to update Docker container status", { item }, { cause: error }));
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
import { createId, splitToNChunks, Stopwatch } from "@homarr/common";
|
||||
import { env } from "@homarr/common/env";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { EVERY_WEEK } from "@homarr/cron-jobs-core/expressions";
|
||||
import type { InferInsertModel } from "@homarr/db";
|
||||
import { db, handleTransactionsAsync, inArray, sql } from "@homarr/db";
|
||||
import { iconRepositories, icons } from "@homarr/db/schema";
|
||||
import { fetchIconsAsync } from "@homarr/icons";
|
||||
import { logger } from "@homarr/log";
|
||||
|
||||
import { createCronJob } from "../lib";
|
||||
|
||||
const logger = createLogger({ module: "iconsUpdaterJobs" });
|
||||
|
||||
export const iconsUpdaterJob = createCronJob("iconsUpdater", EVERY_WEEK, {
|
||||
runOnStart: true,
|
||||
expectedMaximumDurationInMillis: 10 * 1000,
|
||||
@@ -21,9 +23,11 @@ export const iconsUpdaterJob = createCronJob("iconsUpdater", EVERY_WEEK, {
|
||||
const countIcons = repositoryIconGroups
|
||||
.map((group) => group.icons.length)
|
||||
.reduce((partialSum, arrayLength) => partialSum + arrayLength, 0);
|
||||
logger.info(
|
||||
`Successfully fetched ${countIcons} icons from ${repositoryIconGroups.length} repositories within ${stopWatch.getElapsedInHumanWords()}`,
|
||||
);
|
||||
logger.info("Fetched icons from repositories", {
|
||||
repositoryCount: repositoryIconGroups.length,
|
||||
iconCount: countIcons,
|
||||
duration: stopWatch.getElapsedInHumanWords(),
|
||||
});
|
||||
|
||||
const databaseIconRepositories = await db.query.iconRepositories.findMany({
|
||||
with: {
|
||||
@@ -162,5 +166,9 @@ export const iconsUpdaterJob = createCronJob("iconsUpdater", EVERY_WEEK, {
|
||||
},
|
||||
});
|
||||
|
||||
logger.info(`Updated database within ${stopWatch.getElapsedInHumanWords()} (-${countDeleted}, +${countInserted})`);
|
||||
logger.info("Updated icons in database", {
|
||||
duration: stopWatch.getElapsedInHumanWords(),
|
||||
added: countInserted,
|
||||
deleted: countDeleted,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { ErrorWithMetadata } from "@homarr/core/infrastructure/logs/error";
|
||||
import { EVERY_MINUTE } from "@homarr/cron-jobs-core/expressions";
|
||||
import { db } from "@homarr/db";
|
||||
import { getServerSettingByKeyAsync } from "@homarr/db/queries";
|
||||
import { logger } from "@homarr/log";
|
||||
import { sendPingRequestAsync } from "@homarr/ping";
|
||||
import { pingChannel, pingUrlChannel } from "@homarr/redis";
|
||||
|
||||
import { createCronJob } from "../lib";
|
||||
|
||||
const logger = createLogger({ module: "pingJobs" });
|
||||
|
||||
const resetPreviousUrlsAsync = async () => {
|
||||
await pingUrlChannel.clearAsync();
|
||||
logger.info("Cleared previous ping urls");
|
||||
@@ -31,9 +34,9 @@ const pingAsync = async (url: string) => {
|
||||
const pingResult = await sendPingRequestAsync(url);
|
||||
|
||||
if ("statusCode" in pingResult) {
|
||||
logger.debug(`executed ping for url ${url} with status code ${pingResult.statusCode}`);
|
||||
logger.debug("Executed ping successfully", { url, statusCode: pingResult.statusCode });
|
||||
} else {
|
||||
logger.error(`Executing ping for url ${url} failed with error: ${pingResult.error}`);
|
||||
logger.error(new ErrorWithMetadata("Executing ping failed", { url }, { cause: pingResult.error }));
|
||||
}
|
||||
|
||||
await pingChannel.publishAsync({
|
||||
|
||||
@@ -1,15 +1,18 @@
|
||||
import SuperJSON from "superjson";
|
||||
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { ErrorWithMetadata } from "@homarr/core/infrastructure/logs/error";
|
||||
import { EVERY_10_MINUTES } from "@homarr/cron-jobs-core/expressions";
|
||||
import { db, eq } from "@homarr/db";
|
||||
import { items } from "@homarr/db/schema";
|
||||
import { logger } from "@homarr/log";
|
||||
// This import is done that way to avoid circular dependencies.
|
||||
import { rssFeedsRequestHandler } from "@homarr/request-handler/rss-feeds";
|
||||
|
||||
import type { WidgetComponentProps } from "../../../widgets";
|
||||
import { createCronJob } from "../lib";
|
||||
|
||||
const logger = createLogger({ module: "rssFeedsJobs" });
|
||||
|
||||
export const rssFeedsJob = createCronJob("rssFeeds", EVERY_10_MINUTES).withCallback(async () => {
|
||||
const rssItems = await db.query.items.findMany({
|
||||
where: eq(items.kind, "rssFeed"),
|
||||
@@ -29,7 +32,7 @@ export const rssFeedsJob = createCronJob("rssFeeds", EVERY_10_MINUTES).withCallb
|
||||
forceUpdate: true,
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error("Failed to update RSS feed", { url, error });
|
||||
logger.error(new ErrorWithMetadata("Failed to update RSS feed", { url }, { cause: error }));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
import SuperJSON from "superjson";
|
||||
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { ErrorWithMetadata } from "@homarr/core/infrastructure/logs/error";
|
||||
import { EVERY_10_MINUTES } from "@homarr/cron-jobs-core/expressions";
|
||||
import { db, eq } from "@homarr/db";
|
||||
import { items } from "@homarr/db/schema";
|
||||
import { logger } from "@homarr/log";
|
||||
import { weatherRequestHandler } from "@homarr/request-handler/weather";
|
||||
|
||||
import type { WidgetComponentProps } from "../../../widgets";
|
||||
import { createCronJob } from "../lib";
|
||||
|
||||
const logger = createLogger({ module: "weatherJobs" });
|
||||
|
||||
export const weatherJob = createCronJob("weather", EVERY_10_MINUTES).withCallback(async () => {
|
||||
const weatherItems = await db.query.items.findMany({
|
||||
where: eq(items.kind, "weather"),
|
||||
@@ -27,7 +30,7 @@ export const weatherJob = createCronJob("weather", EVERY_10_MINUTES).withCallbac
|
||||
});
|
||||
await innerHandler.getCachedOrUpdatedDataAsync({ forceUpdate: true });
|
||||
} catch (error) {
|
||||
logger.error("Failed to update weather", { id: item.id, error });
|
||||
logger.error(new ErrorWithMetadata("Failed to update weather", { id: item.id }, { cause: error }));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,24 +1,29 @@
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { beforeCallbackAsync, onCallbackErrorAsync, onCallbackSuccessAsync } from "@homarr/cron-job-status/publisher";
|
||||
import { createCronJobFunctions } from "@homarr/cron-jobs-core";
|
||||
import type { Logger } from "@homarr/cron-jobs-core/logger";
|
||||
import { logger } from "@homarr/log";
|
||||
import type { TranslationObject } from "@homarr/translation";
|
||||
|
||||
const logger = createLogger({ module: "cronJobs" });
|
||||
|
||||
class WinstonCronJobLogger implements Logger {
|
||||
logDebug(message: string) {
|
||||
logger.debug(message);
|
||||
logDebug(message: string, metadata?: Record<string, unknown>): void {
|
||||
logger.debug(message, metadata);
|
||||
}
|
||||
|
||||
logInfo(message: string) {
|
||||
logger.info(message);
|
||||
logInfo(message: string, metadata?: Record<string, unknown>): void {
|
||||
logger.info(message, metadata);
|
||||
}
|
||||
|
||||
logError(error: unknown) {
|
||||
logger.error(error);
|
||||
logError(message: string, metadata?: Record<string, unknown>): void;
|
||||
logError(error: unknown): void;
|
||||
logError(messageOrError: unknown, metadata?: Record<string, unknown>): void {
|
||||
if (typeof messageOrError === "string") {
|
||||
logger.error(messageOrError, metadata);
|
||||
return;
|
||||
}
|
||||
logger.error(messageOrError);
|
||||
}
|
||||
|
||||
logWarning(message: string) {
|
||||
logger.warn(message);
|
||||
logWarning(message: string, metadata?: Record<string, unknown>): void {
|
||||
logger.warn(message, metadata);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,13 +11,15 @@ import type { Pool as MysqlConnectionPool } from "mysql2";
|
||||
import mysql from "mysql2";
|
||||
import { Pool as PostgresPool } from "pg";
|
||||
|
||||
import { logger } from "@homarr/log";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
|
||||
import { env } from "./env";
|
||||
import * as mysqlSchema from "./schema/mysql";
|
||||
import * as pgSchema from "./schema/postgresql";
|
||||
import * as sqliteSchema from "./schema/sqlite";
|
||||
|
||||
const logger = createLogger({ module: "db" });
|
||||
|
||||
export type HomarrDatabase = BetterSQLite3Database<typeof sqliteSchema>;
|
||||
export type HomarrDatabaseMysql = MySql2Database<typeof mysqlSchema>;
|
||||
export type HomarrDatabasePostgresql = NodePgDatabase<typeof pgSchema>;
|
||||
@@ -44,7 +46,7 @@ export let database: HomarrDatabase;
|
||||
|
||||
class WinstonDrizzleLogger implements Logger {
|
||||
logQuery(query: string, _: unknown[]): void {
|
||||
logger.debug(`Executed SQL query: ${query}`);
|
||||
logger.debug("Executed SQL query", { query });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -47,7 +47,6 @@
|
||||
"@homarr/common": "workspace:^0.1.0",
|
||||
"@homarr/core": "workspace:^0.1.0",
|
||||
"@homarr/definitions": "workspace:^0.1.0",
|
||||
"@homarr/log": "workspace:^0.1.0",
|
||||
"@homarr/server-settings": "workspace:^0.1.0",
|
||||
"@mantine/core": "^8.3.10",
|
||||
"@paralleldrive/cuid2": "^3.1.0",
|
||||
|
||||
@@ -24,8 +24,8 @@
|
||||
"prettier": "@homarr/prettier-config",
|
||||
"dependencies": {
|
||||
"@homarr/common": "workspace:^0.1.0",
|
||||
"@homarr/db": "workspace:^0.1.0",
|
||||
"@homarr/log": "workspace:^0.1.0"
|
||||
"@homarr/core": "workspace:^0.1.0",
|
||||
"@homarr/db": "workspace:^0.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@homarr/eslint-config": "workspace:^0.2.0",
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import { logger } from "@homarr/log";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { ErrorWithMetadata } from "@homarr/core/infrastructure/logs/error";
|
||||
|
||||
import type { IconRepositoryLicense } from "../types/icon-repository-license";
|
||||
import type { RepositoryIconGroup } from "../types/repository-icon-group";
|
||||
|
||||
const logger = createLogger({ module: "iconRepository" });
|
||||
|
||||
export abstract class IconRepository {
|
||||
protected readonly allowedImageFileTypes = [".png", ".svg", ".jpeg"];
|
||||
|
||||
@@ -19,7 +22,9 @@ export abstract class IconRepository {
|
||||
try {
|
||||
return await this.getAllIconsInternalAsync();
|
||||
} catch (err) {
|
||||
logger.error(`Unable to request icons from repository "${this.slug}": ${JSON.stringify(err)}`);
|
||||
logger.error(
|
||||
new ErrorWithMetadata("Unable to request icons from repository", { slug: this.slug }, { cause: err }),
|
||||
);
|
||||
return {
|
||||
success: false,
|
||||
icons: [],
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
"dependencies": {
|
||||
"@homarr/certificates": "workspace:^0.1.0",
|
||||
"@homarr/common": "workspace:^0.1.0",
|
||||
"@homarr/log": "workspace:^0.1.0",
|
||||
"@homarr/core": "workspace:^0.1.0",
|
||||
"@homarr/redis": "workspace:^0.1.0",
|
||||
"bcrypt": "^6.0.0"
|
||||
},
|
||||
|
||||
@@ -3,9 +3,12 @@ import bcrypt from "bcrypt";
|
||||
import { fetchWithTrustedCertificatesAsync } from "@homarr/certificates/server";
|
||||
import { createId } from "@homarr/common";
|
||||
import { decryptSecret, encryptSecret } from "@homarr/common/server";
|
||||
import { logger } from "@homarr/log";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { ErrorWithMetadata } from "@homarr/core/infrastructure/logs/error";
|
||||
import { createGetSetChannel } from "@homarr/redis";
|
||||
|
||||
const logger = createLogger({ module: "imageProxy" });
|
||||
|
||||
const createHashChannel = (hash: `${string}.${string}`) => createGetSetChannel<string>(`image-proxy:hash:${hash}`);
|
||||
const createUrlByIdChannel = (id: string) =>
|
||||
createGetSetChannel<{
|
||||
@@ -25,7 +28,7 @@ export class ImageProxy {
|
||||
}
|
||||
|
||||
const salt = await bcrypt.genSalt(10);
|
||||
logger.debug(`Generated new salt for image proxy salt="${salt}"`);
|
||||
logger.debug("Generated new salt for image proxy", { salt });
|
||||
ImageProxy.salt = salt;
|
||||
await saltChannel.setAsync(salt);
|
||||
return salt;
|
||||
@@ -34,9 +37,11 @@ export class ImageProxy {
|
||||
public async createImageAsync(url: string, headers?: Record<string, string>): Promise<string> {
|
||||
const existingId = await this.getExistingIdAsync(url, headers);
|
||||
if (existingId) {
|
||||
logger.debug(
|
||||
`Image already exists in the proxy id="${existingId}" url="${this.redactUrl(url)}" headers="${this.redactHeaders(headers ?? null)}"`,
|
||||
);
|
||||
logger.debug("Image already exists in the proxy", {
|
||||
id: existingId,
|
||||
url: this.redactUrl(url),
|
||||
headers: this.redactHeaders(headers ?? null),
|
||||
});
|
||||
return this.createImageUrl(existingId);
|
||||
}
|
||||
|
||||
@@ -59,15 +64,25 @@ export class ImageProxy {
|
||||
const proxyUrl = this.createImageUrl(id);
|
||||
if (!response.ok) {
|
||||
logger.error(
|
||||
`Failed to fetch image id="${id}" url="${this.redactUrl(urlAndHeaders.url)}" headers="${this.redactHeaders(urlAndHeaders.headers)}" proxyUrl="${proxyUrl}" statusCode="${response.status}"`,
|
||||
new ErrorWithMetadata("Failed to fetch image", {
|
||||
id,
|
||||
url: this.redactUrl(urlAndHeaders.url),
|
||||
headers: this.redactHeaders(urlAndHeaders.headers),
|
||||
proxyUrl,
|
||||
statusCode: response.status,
|
||||
}),
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
const blob = (await response.blob()) as Blob;
|
||||
logger.debug(
|
||||
`Forwarding image succeeded id="${id}" url="${this.redactUrl(urlAndHeaders.url)}" headers="${this.redactHeaders(urlAndHeaders.headers)}" proxyUrl="${proxyUrl} size="${(blob.size / 1024).toFixed(1)}KB"`,
|
||||
);
|
||||
logger.debug("Forwarding image succeeded", {
|
||||
id,
|
||||
url: this.redactUrl(urlAndHeaders.url),
|
||||
headers: this.redactHeaders(urlAndHeaders.headers),
|
||||
proxyUrl,
|
||||
size: `${(blob.size / 1024).toFixed(1)}KB`,
|
||||
});
|
||||
|
||||
return blob;
|
||||
}
|
||||
@@ -80,7 +95,7 @@ export class ImageProxy {
|
||||
const urlHeaderChannel = createUrlByIdChannel(id);
|
||||
const urlHeader = await urlHeaderChannel.getAsync();
|
||||
if (!urlHeader) {
|
||||
logger.warn(`Image not found in the proxy id="${id}"`);
|
||||
logger.warn("Image not found in the proxy", { id });
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -112,9 +127,11 @@ export class ImageProxy {
|
||||
});
|
||||
await hashChannel.setAsync(id);
|
||||
|
||||
logger.debug(
|
||||
`Stored image in the proxy id="${id}" url="${this.redactUrl(url)}" headers="${this.redactHeaders(headers ?? null)}"`,
|
||||
);
|
||||
logger.debug("Stored image in the proxy", {
|
||||
id,
|
||||
url: this.redactUrl(url),
|
||||
headers: this.redactHeaders(headers ?? null),
|
||||
});
|
||||
}
|
||||
|
||||
private redactUrl(url: string): string {
|
||||
|
||||
@@ -31,10 +31,10 @@
|
||||
"@gitbeaker/rest": "^43.8.0",
|
||||
"@homarr/certificates": "workspace:^0.1.0",
|
||||
"@homarr/common": "workspace:^0.1.0",
|
||||
"@homarr/core": "workspace:^0.1.0",
|
||||
"@homarr/db": "workspace:^0.1.0",
|
||||
"@homarr/definitions": "workspace:^0.1.0",
|
||||
"@homarr/image-proxy": "workspace:^0.1.0",
|
||||
"@homarr/log": "workspace:^0.1.0",
|
||||
"@homarr/node-unifi": "^2.6.0",
|
||||
"@homarr/redis": "workspace:^0.1.0",
|
||||
"@homarr/translation": "workspace:^0.1.0",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { isFunction } from "@homarr/common";
|
||||
import { logger } from "@homarr/log";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
|
||||
import type { Integration } from "../integration";
|
||||
import type { IIntegrationErrorHandler } from "./handler";
|
||||
@@ -8,9 +8,7 @@ import { IntegrationError } from "./integration-error";
|
||||
import { IntegrationUnknownError } from "./integration-unknown-error";
|
||||
import { integrationJsonParseErrorHandler, integrationZodParseErrorHandler } from "./parse";
|
||||
|
||||
const localLogger = logger.child({
|
||||
module: "HandleIntegrationErrors",
|
||||
});
|
||||
const logger = createLogger({ module: "handleIntegrationErrors" });
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-object-type, @typescript-eslint/no-explicit-any
|
||||
type AbstractConstructor<T = {}> = abstract new (...args: any[]) => T;
|
||||
@@ -59,7 +57,7 @@ export const HandleIntegrationErrors = (errorHandlers: IIntegrationErrorHandler[
|
||||
}
|
||||
|
||||
// If the error was handled and should be thrown again, throw it
|
||||
localLogger.debug("Unhandled error in integration", {
|
||||
logger.debug("Unhandled error in integration", {
|
||||
error: error instanceof Error ? `${error.name}: ${error.message}` : undefined,
|
||||
integrationName: this.publicIntegration.name,
|
||||
});
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import superjson from "superjson";
|
||||
|
||||
import { decryptSecret, encryptSecret } from "@homarr/common/server";
|
||||
import { logger } from "@homarr/log";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { ErrorWithMetadata } from "@homarr/core/infrastructure/logs/error";
|
||||
import { createGetSetChannel } from "@homarr/redis";
|
||||
|
||||
const localLogger = logger.child({ module: "SessionStore" });
|
||||
const logger = createLogger({ module: "sessionStore" });
|
||||
|
||||
export const createSessionStore = <TValue>(integration: { id: string }) => {
|
||||
const channelName = `session-store:${integration.id}`;
|
||||
@@ -12,26 +13,26 @@ export const createSessionStore = <TValue>(integration: { id: string }) => {
|
||||
|
||||
return {
|
||||
async getAsync() {
|
||||
localLogger.debug("Getting session from store", { store: channelName });
|
||||
logger.debug("Getting session from store", { store: channelName });
|
||||
const value = await channel.getAsync();
|
||||
if (!value) return null;
|
||||
try {
|
||||
return superjson.parse<TValue>(decryptSecret(value));
|
||||
} catch (error) {
|
||||
localLogger.warn("Failed to load session", { store: channelName, error });
|
||||
logger.warn("Failed to load session", { store: channelName, error });
|
||||
return null;
|
||||
}
|
||||
},
|
||||
async setAsync(value: TValue) {
|
||||
localLogger.debug("Updating session in store", { store: channelName });
|
||||
logger.debug("Updating session in store", { store: channelName });
|
||||
try {
|
||||
await channel.setAsync(encryptSecret(superjson.stringify(value)));
|
||||
} catch (error) {
|
||||
localLogger.error("Failed to save session", { store: channelName, error });
|
||||
logger.error(new ErrorWithMetadata("Failed to save session", { store: channelName }, { cause: error }));
|
||||
}
|
||||
},
|
||||
async clearAsync() {
|
||||
localLogger.debug("Cleared session in store", { store: channelName });
|
||||
logger.debug("Cleared session in store", { store: channelName });
|
||||
await channel.removeAsync();
|
||||
},
|
||||
};
|
||||
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
getTrustedCertificateHostnamesAsync,
|
||||
} from "@homarr/certificates/server";
|
||||
import { getPortFromUrl } from "@homarr/common";
|
||||
import { logger } from "@homarr/log";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
|
||||
import type { IntegrationRequestErrorOfType } from "../errors/http/integration-request-error";
|
||||
import { IntegrationRequestError } from "../errors/http/integration-request-error";
|
||||
@@ -15,8 +15,8 @@ import { IntegrationError } from "../errors/integration-error";
|
||||
import type { AnyTestConnectionError } from "./test-connection-error";
|
||||
import { TestConnectionError } from "./test-connection-error";
|
||||
|
||||
const localLogger = logger.child({
|
||||
module: "TestConnectionService",
|
||||
const logger = createLogger({
|
||||
module: "testConnectionService",
|
||||
});
|
||||
|
||||
export type TestingResult =
|
||||
@@ -36,7 +36,7 @@ export class TestConnectionService {
|
||||
constructor(private url: URL) {}
|
||||
|
||||
public async handleAsync(testingCallbackAsync: AsyncTestingCallback) {
|
||||
localLogger.debug("Testing connection", {
|
||||
logger.debug("Testing connection", {
|
||||
url: this.url.toString(),
|
||||
});
|
||||
|
||||
@@ -72,14 +72,14 @@ export class TestConnectionService {
|
||||
});
|
||||
|
||||
if (testingResult.success) {
|
||||
localLogger.debug("Testing connection succeeded", {
|
||||
logger.debug("Testing connection succeeded", {
|
||||
url: this.url.toString(),
|
||||
});
|
||||
|
||||
return testingResult;
|
||||
}
|
||||
|
||||
localLogger.debug("Testing connection failed", {
|
||||
logger.debug("Testing connection failed", {
|
||||
url: this.url.toString(),
|
||||
error: `${testingResult.error.name}: ${testingResult.error.message}`,
|
||||
});
|
||||
@@ -124,7 +124,7 @@ export class TestConnectionService {
|
||||
const x509 = socket.getPeerX509Certificate();
|
||||
socket.destroy();
|
||||
|
||||
localLogger.debug("Fetched certificate", {
|
||||
logger.debug("Fetched certificate", {
|
||||
url: this.url.toString(),
|
||||
subject: x509?.subject,
|
||||
issuer: x509?.issuer,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { RequestInit, Response } from "undici";
|
||||
|
||||
import { fetchWithTrustedCertificatesAsync } from "@homarr/certificates/server";
|
||||
import { logger } from "@homarr/log";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
|
||||
import type { IntegrationTestingInput } from "../base/integration";
|
||||
import { Integration } from "../base/integration";
|
||||
@@ -15,7 +15,7 @@ import type {
|
||||
} from "../interfaces/releases-providers/releases-providers-types";
|
||||
import { detailsResponseSchema, releasesResponseSchema } from "./codeberg-schemas";
|
||||
|
||||
const localLogger = logger.child({ module: "CodebergIntegration" });
|
||||
const logger = createLogger({ module: "codebergIntegration" });
|
||||
|
||||
export class CodebergIntegration extends Integration implements ReleasesProviderIntegration {
|
||||
private async withHeadersAsync(callback: (headers: RequestInit["headers"]) => Promise<Response>): Promise<Response> {
|
||||
@@ -45,10 +45,9 @@ export class CodebergIntegration extends Integration implements ReleasesProvider
|
||||
private parseIdentifier(identifier: string) {
|
||||
const [owner, name] = identifier.split("/");
|
||||
if (!owner || !name) {
|
||||
localLogger.warn(
|
||||
`Invalid identifier format. Expected 'owner/name', for ${identifier} with Codeberg integration`,
|
||||
{ identifier },
|
||||
);
|
||||
logger.warn("Invalid identifier format. Expected 'owner/name', for identifier", {
|
||||
identifier,
|
||||
});
|
||||
return null;
|
||||
}
|
||||
return { owner, name };
|
||||
@@ -109,7 +108,7 @@ export class CodebergIntegration extends Integration implements ReleasesProvider
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
localLogger.warn(`Failed to get details response for ${owner}/${name} with Codeberg integration`, {
|
||||
logger.warn("Failed to get details", {
|
||||
owner,
|
||||
name,
|
||||
error: response.statusText,
|
||||
@@ -122,7 +121,7 @@ export class CodebergIntegration extends Integration implements ReleasesProvider
|
||||
const { data, success, error } = detailsResponseSchema.safeParse(responseJson);
|
||||
|
||||
if (!success) {
|
||||
localLogger.warn(`Failed to parse details response for ${owner}/${name} with Codeberg integration`, {
|
||||
logger.warn("Failed to parse details", {
|
||||
owner,
|
||||
name,
|
||||
error,
|
||||
|
||||
@@ -2,7 +2,7 @@ import type { fetch, RequestInit, Response } from "undici";
|
||||
|
||||
import { fetchWithTrustedCertificatesAsync } from "@homarr/certificates/server";
|
||||
import { ResponseError } from "@homarr/common/server";
|
||||
import { logger } from "@homarr/log";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
|
||||
import type { IntegrationInput, IntegrationTestingInput } from "../base/integration";
|
||||
import { Integration } from "../base/integration";
|
||||
@@ -18,7 +18,7 @@ import type {
|
||||
} from "../interfaces/releases-providers/releases-providers-types";
|
||||
import { accessTokenResponseSchema, detailsResponseSchema, releasesResponseSchema } from "./docker-hub-schemas";
|
||||
|
||||
const localLogger = logger.child({ module: "DockerHubIntegration" });
|
||||
const logger = createLogger({ module: "dockerHubIntegration" });
|
||||
|
||||
export class DockerHubIntegration extends Integration implements ReleasesProviderIntegration {
|
||||
private readonly sessionStore: SessionStore<string>;
|
||||
@@ -35,7 +35,7 @@ export class DockerHubIntegration extends Integration implements ReleasesProvide
|
||||
const storedSession = await this.sessionStore.getAsync();
|
||||
|
||||
if (storedSession) {
|
||||
localLogger.debug("Using stored session for request", { integrationId: this.integration.id });
|
||||
logger.debug("Using stored session for request", { integrationId: this.integration.id });
|
||||
const response = await callback({
|
||||
Authorization: `Bearer ${storedSession}`,
|
||||
});
|
||||
@@ -43,7 +43,7 @@ export class DockerHubIntegration extends Integration implements ReleasesProvide
|
||||
return response;
|
||||
}
|
||||
|
||||
localLogger.debug("Session expired, getting new session", { integrationId: this.integration.id });
|
||||
logger.debug("Session expired, getting new session", { integrationId: this.integration.id });
|
||||
}
|
||||
|
||||
const accessToken = await this.getSessionAsync();
|
||||
@@ -57,10 +57,10 @@ export class DockerHubIntegration extends Integration implements ReleasesProvide
|
||||
const hasAuth = this.hasSecretValue("username") && this.hasSecretValue("personalAccessToken");
|
||||
|
||||
if (hasAuth) {
|
||||
localLogger.debug("Testing DockerHub connection with authentication", { integrationId: this.integration.id });
|
||||
logger.debug("Testing DockerHub connection with authentication", { integrationId: this.integration.id });
|
||||
await this.getSessionAsync(input.fetchAsync);
|
||||
} else {
|
||||
localLogger.debug("Testing DockerHub connection without authentication", { integrationId: this.integration.id });
|
||||
logger.debug("Testing DockerHub connection without authentication", { integrationId: this.integration.id });
|
||||
const response = await input.fetchAsync(this.url("/v2/repositories/library"));
|
||||
if (!response.ok) {
|
||||
return TestConnectionError.StatusResult(response);
|
||||
@@ -76,7 +76,7 @@ export class DockerHubIntegration extends Integration implements ReleasesProvide
|
||||
if (!identifier.includes("/")) return { owner: "", name: identifier };
|
||||
const [owner, name] = identifier.split("/");
|
||||
if (!owner || !name) {
|
||||
localLogger.warn(`Invalid identifier format. Expected 'owner/name' or 'name', for ${identifier} on DockerHub`, {
|
||||
logger.warn("Invalid identifier format. Expected 'owner/name' or 'name', for identifier", {
|
||||
identifier,
|
||||
});
|
||||
return null;
|
||||
@@ -137,7 +137,7 @@ export class DockerHubIntegration extends Integration implements ReleasesProvide
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
localLogger.warn(`Failed to get details response for ${relativeUrl} with DockerHub integration`, {
|
||||
logger.warn("Failed to get details response", {
|
||||
relativeUrl,
|
||||
error: response.statusText,
|
||||
});
|
||||
@@ -149,7 +149,7 @@ export class DockerHubIntegration extends Integration implements ReleasesProvide
|
||||
const { data, success, error } = detailsResponseSchema.safeParse(responseJson);
|
||||
|
||||
if (!success) {
|
||||
localLogger.warn(`Failed to parse details response for ${relativeUrl} with DockerHub integration`, {
|
||||
logger.warn("Failed to parse details response", {
|
||||
relativeUrl,
|
||||
error,
|
||||
});
|
||||
@@ -183,7 +183,7 @@ export class DockerHubIntegration extends Integration implements ReleasesProvide
|
||||
throw new ResponseError({ status: 401, url: response.url });
|
||||
}
|
||||
|
||||
localLogger.info("Received session successfully", { integrationId: this.integration.id });
|
||||
logger.info("Received session successfully", { integrationId: this.integration.id });
|
||||
|
||||
return result.access_token;
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ import { Octokit, RequestError } from "octokit";
|
||||
import type { fetch } from "undici";
|
||||
|
||||
import { fetchWithTrustedCertificatesAsync } from "@homarr/certificates/server";
|
||||
import { logger } from "@homarr/log";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
|
||||
import { HandleIntegrationErrors } from "../base/errors/decorator";
|
||||
import { integrationOctokitHttpErrorHandler } from "../base/errors/http";
|
||||
@@ -18,7 +18,7 @@ import type {
|
||||
ReleaseResponse,
|
||||
} from "../interfaces/releases-providers/releases-providers-types";
|
||||
|
||||
const localLogger = logger.child({ module: "GitHubContainerRegistryIntegration" });
|
||||
const logger = createLogger({ module: "githubContainerRegistryIntegration" });
|
||||
|
||||
@HandleIntegrationErrors([integrationOctokitHttpErrorHandler])
|
||||
export class GitHubContainerRegistryIntegration extends Integration implements ReleasesProviderIntegration {
|
||||
@@ -45,10 +45,7 @@ export class GitHubContainerRegistryIntegration extends Integration implements R
|
||||
private parseIdentifier(identifier: string) {
|
||||
const [owner, name] = identifier.split("/");
|
||||
if (!owner || !name) {
|
||||
localLogger.warn(
|
||||
`Invalid identifier format. Expected 'owner/name', for ${identifier} with GitHub Container Registry integration`,
|
||||
{ identifier },
|
||||
);
|
||||
logger.warn("Invalid identifier format. Expected 'owner/name', for identifier", { identifier });
|
||||
return null;
|
||||
}
|
||||
return { owner, name };
|
||||
@@ -91,7 +88,7 @@ export class GitHubContainerRegistryIntegration extends Integration implements R
|
||||
return { success: true, data: { ...details, ...latestRelease } };
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof RequestError ? error.message : String(error);
|
||||
localLogger.warn(`Failed to get releases for ${owner}\\${name} with GitHub Container Registry integration`, {
|
||||
logger.warn("Failed to get releases", {
|
||||
owner,
|
||||
name,
|
||||
error: errorMessage,
|
||||
@@ -123,7 +120,7 @@ export class GitHubContainerRegistryIntegration extends Integration implements R
|
||||
forksCount: response.data.repository?.forks_count,
|
||||
};
|
||||
} catch (error) {
|
||||
localLogger.warn(`Failed to get details for ${owner}\\${name} with GitHub Container Registry integration`, {
|
||||
logger.warn("Failed to get details", {
|
||||
owner,
|
||||
name,
|
||||
error: error instanceof RequestError ? error.message : String(error),
|
||||
|
||||
@@ -3,7 +3,7 @@ import { Octokit, RequestError as OctokitRequestError } from "octokit";
|
||||
import type { fetch } from "undici";
|
||||
|
||||
import { fetchWithTrustedCertificatesAsync } from "@homarr/certificates/server";
|
||||
import { logger } from "@homarr/log";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
|
||||
import { HandleIntegrationErrors } from "../base/errors/decorator";
|
||||
import { integrationOctokitHttpErrorHandler } from "../base/errors/http";
|
||||
@@ -18,7 +18,7 @@ import type {
|
||||
ReleaseResponse,
|
||||
} from "../interfaces/releases-providers/releases-providers-types";
|
||||
|
||||
const localLogger = logger.child({ module: "GithubIntegration" });
|
||||
const logger = createLogger({ module: "githubIntegration" });
|
||||
|
||||
@HandleIntegrationErrors([integrationOctokitHttpErrorHandler])
|
||||
export class GithubIntegration extends Integration implements ReleasesProviderIntegration {
|
||||
@@ -45,7 +45,7 @@ export class GithubIntegration extends Integration implements ReleasesProviderIn
|
||||
private parseIdentifier(identifier: string) {
|
||||
const [owner, name] = identifier.split("/");
|
||||
if (!owner || !name) {
|
||||
localLogger.warn(`Invalid identifier format. Expected 'owner/name', for ${identifier} with Github integration`, {
|
||||
logger.warn("Invalid identifier format. Expected 'owner/name' for identifier", {
|
||||
identifier,
|
||||
});
|
||||
return null;
|
||||
@@ -64,7 +64,7 @@ export class GithubIntegration extends Integration implements ReleasesProviderIn
|
||||
const releasesResponse = await api.rest.repos.listReleases({ owner, repo: name });
|
||||
|
||||
if (releasesResponse.data.length === 0) {
|
||||
localLogger.warn(`No releases found, for ${owner}/${name} with Github integration`, {
|
||||
logger.warn("No releases found", {
|
||||
identifier: `${owner}/${name}`,
|
||||
});
|
||||
return { success: false, error: { code: "noMatchingVersion" } };
|
||||
@@ -91,7 +91,7 @@ export class GithubIntegration extends Integration implements ReleasesProviderIn
|
||||
return { success: true, data: { ...details, ...latestRelease } };
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof OctokitRequestError ? error.message : String(error);
|
||||
localLogger.warn(`Failed to get releases for ${owner}\\${name} with Github integration`, {
|
||||
logger.warn("Failed to get releases", {
|
||||
owner,
|
||||
name,
|
||||
error: errorMessage,
|
||||
@@ -122,7 +122,7 @@ export class GithubIntegration extends Integration implements ReleasesProviderIn
|
||||
forksCount: response.data.forks_count,
|
||||
};
|
||||
} catch (error) {
|
||||
localLogger.warn(`Failed to get details for ${owner}\\${name} with Github integration`, {
|
||||
logger.warn("Failed to get details", {
|
||||
owner,
|
||||
name,
|
||||
error: error instanceof OctokitRequestError ? error.message : String(error),
|
||||
|
||||
@@ -4,7 +4,7 @@ import type { FormattedResponse, RequestOptions, ResourceOptions } from "@gitbea
|
||||
import { Gitlab } from "@gitbeaker/rest";
|
||||
|
||||
import { fetchWithTrustedCertificatesAsync } from "@homarr/certificates/server";
|
||||
import { logger } from "@homarr/log";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
|
||||
import type { IntegrationTestingInput } from "../base/integration";
|
||||
import { Integration } from "../base/integration";
|
||||
@@ -18,7 +18,7 @@ import type {
|
||||
ReleaseResponse,
|
||||
} from "../interfaces/releases-providers/releases-providers-types";
|
||||
|
||||
const localLogger = logger.child({ module: "GitlabIntegration" });
|
||||
const logger = createLogger({ module: "gitlabIntegration" });
|
||||
|
||||
export class GitlabIntegration extends Integration implements ReleasesProviderIntegration {
|
||||
protected async testingAsync(input: IntegrationTestingInput): Promise<TestingResult> {
|
||||
@@ -48,7 +48,7 @@ export class GitlabIntegration extends Integration implements ReleasesProviderIn
|
||||
});
|
||||
|
||||
if (releasesResponse instanceof Error) {
|
||||
localLogger.warn(`Failed to get releases for ${identifier} with Gitlab integration`, {
|
||||
logger.warn("No releases found", {
|
||||
identifier,
|
||||
error: releasesResponse.message,
|
||||
});
|
||||
@@ -78,7 +78,7 @@ export class GitlabIntegration extends Integration implements ReleasesProviderIn
|
||||
return { success: true, data: { ...details, ...latestRelease } };
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
localLogger.warn(`Failed to get releases for ${identifier} with Gitlab integration`, {
|
||||
logger.warn("Failed to get releases", {
|
||||
identifier,
|
||||
error: errorMessage,
|
||||
});
|
||||
@@ -91,7 +91,7 @@ export class GitlabIntegration extends Integration implements ReleasesProviderIn
|
||||
const response = await api.Projects.show(identifier);
|
||||
|
||||
if (response instanceof Error) {
|
||||
localLogger.warn(`Failed to get details for ${identifier} with Gitlab integration`, {
|
||||
logger.warn("Failed to get details", {
|
||||
identifier,
|
||||
error: response.message,
|
||||
});
|
||||
@@ -100,7 +100,7 @@ export class GitlabIntegration extends Integration implements ReleasesProviderIn
|
||||
}
|
||||
|
||||
if (!response.web_url) {
|
||||
localLogger.warn(`No web URL found for ${identifier} with Gitlab integration`, {
|
||||
logger.warn("No web URL found", {
|
||||
identifier,
|
||||
});
|
||||
return undefined;
|
||||
@@ -117,7 +117,7 @@ export class GitlabIntegration extends Integration implements ReleasesProviderIn
|
||||
forksCount: response.forks_count,
|
||||
};
|
||||
} catch (error) {
|
||||
localLogger.warn(`Failed to get details for ${identifier} with Gitlab integration`, {
|
||||
logger.warn("Failed to get details", {
|
||||
identifier,
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
});
|
||||
|
||||
@@ -2,7 +2,8 @@ import z from "zod";
|
||||
|
||||
import { fetchWithTrustedCertificatesAsync } from "@homarr/certificates/server";
|
||||
import { ResponseError } from "@homarr/common/server";
|
||||
import { logger } from "@homarr/log";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { ErrorWithMetadata } from "@homarr/core/infrastructure/logs/error";
|
||||
|
||||
import type { IntegrationTestingInput } from "../base/integration";
|
||||
import { Integration } from "../base/integration";
|
||||
@@ -13,13 +14,15 @@ import type { ISmartHomeIntegration } from "../interfaces/smart-home/smart-home-
|
||||
import type { CalendarEvent } from "../types";
|
||||
import { calendarEventSchema, calendarsSchema, entityStateSchema } from "./homeassistant-types";
|
||||
|
||||
const logger = createLogger({ module: "homeAssistantIntegration" });
|
||||
|
||||
export class HomeAssistantIntegration extends Integration implements ISmartHomeIntegration, ICalendarIntegration {
|
||||
public async getEntityStateAsync(entityId: string) {
|
||||
try {
|
||||
const response = await this.getAsync(`/api/states/${entityId}`);
|
||||
const body = await response.json();
|
||||
if (!response.ok) {
|
||||
logger.warn(`Response did not indicate success`);
|
||||
logger.warn("Response did not indicate success");
|
||||
return {
|
||||
success: false as const,
|
||||
error: "Response did not indicate success",
|
||||
@@ -27,7 +30,7 @@ export class HomeAssistantIntegration extends Integration implements ISmartHomeI
|
||||
}
|
||||
return entityStateSchema.safeParseAsync(body);
|
||||
} catch (err) {
|
||||
logger.error(`Failed to fetch from ${this.url("/")}: ${err as string}`);
|
||||
logger.error(new ErrorWithMetadata("Failed to fetch entity state", { entityId }, { cause: err }));
|
||||
return {
|
||||
success: false as const,
|
||||
error: err,
|
||||
@@ -43,7 +46,7 @@ export class HomeAssistantIntegration extends Integration implements ISmartHomeI
|
||||
|
||||
return response.ok;
|
||||
} catch (err) {
|
||||
logger.error(`Failed to fetch from '${this.url("/")}': ${err as string}`);
|
||||
logger.error(new ErrorWithMetadata("Failed to trigger automation", { entityId }, { cause: err }));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -62,7 +65,7 @@ export class HomeAssistantIntegration extends Integration implements ISmartHomeI
|
||||
|
||||
return response.ok;
|
||||
} catch (err) {
|
||||
logger.error(`Failed to fetch from '${this.url("/")}': ${err as string}`);
|
||||
logger.error(new ErrorWithMetadata("Failed to toggle entity", { entityId }, { cause: err }));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { fetchWithTrustedCertificatesAsync } from "@homarr/certificates/server";
|
||||
import { logger } from "@homarr/log";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
|
||||
import type { IntegrationTestingInput } from "../base/integration";
|
||||
import { Integration } from "../base/integration";
|
||||
@@ -9,7 +9,7 @@ import type { ReleasesProviderIntegration } from "../interfaces/releases-provide
|
||||
import type { ReleaseResponse } from "../interfaces/releases-providers/releases-providers-types";
|
||||
import { releasesResponseSchema } from "./linuxserverio-schemas";
|
||||
|
||||
const localLogger = logger.child({ module: "LinuxServerIOsIntegration" });
|
||||
const logger = createLogger({ module: "linuxServerIOIntegration" });
|
||||
|
||||
export class LinuxServerIOIntegration extends Integration implements ReleasesProviderIntegration {
|
||||
protected async testingAsync(input: IntegrationTestingInput): Promise<TestingResult> {
|
||||
@@ -27,10 +27,7 @@ export class LinuxServerIOIntegration extends Integration implements ReleasesPro
|
||||
private parseIdentifier(identifier: string) {
|
||||
const [owner, name] = identifier.split("/");
|
||||
if (!owner || !name) {
|
||||
localLogger.warn(
|
||||
`Invalid identifier format. Expected 'owner/name', for ${identifier} with LinuxServerIO integration`,
|
||||
{ identifier },
|
||||
);
|
||||
logger.warn("Invalid identifier format. Expected 'owner/name' for identifier", { identifier });
|
||||
return null;
|
||||
}
|
||||
return { owner, name };
|
||||
@@ -53,7 +50,7 @@ export class LinuxServerIOIntegration extends Integration implements ReleasesPro
|
||||
|
||||
const release = data.data.repositories.linuxserver.find((repo) => repo.name === name);
|
||||
if (!release) {
|
||||
localLogger.warn(`Repository ${name} not found on provider, with LinuxServerIO integration`, {
|
||||
logger.warn("Repository not found on provider", {
|
||||
name,
|
||||
});
|
||||
return { success: false, error: { code: "noMatchingVersion" } };
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { z } from "zod/v4";
|
||||
|
||||
import { fetchWithTrustedCertificatesAsync } from "@homarr/certificates/server";
|
||||
import { logger } from "@homarr/log";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
|
||||
import { Integration } from "../../base/integration";
|
||||
import type { IntegrationTestingInput } from "../../base/integration";
|
||||
@@ -11,6 +11,8 @@ import type { ICalendarIntegration } from "../../interfaces/calendar/calendar-in
|
||||
import type { CalendarEvent, CalendarLink } from "../../interfaces/calendar/calendar-types";
|
||||
import { mediaOrganizerPriorities } from "../media-organizer";
|
||||
|
||||
const logger = createLogger({ module: "lidarrIntegration" });
|
||||
|
||||
export class LidarrIntegration extends Integration implements ICalendarIntegration {
|
||||
protected async testingAsync(input: IntegrationTestingInput): Promise<TestingResult> {
|
||||
const response = await input.fetchAsync(this.url("/api"), {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { z } from "zod/v4";
|
||||
|
||||
import { fetchWithTrustedCertificatesAsync } from "@homarr/certificates/server";
|
||||
import { logger } from "@homarr/log";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
|
||||
import type { IntegrationTestingInput } from "../../base/integration";
|
||||
import { Integration } from "../../base/integration";
|
||||
@@ -12,6 +12,8 @@ import type { CalendarEvent, CalendarLink } from "../../interfaces/calendar/cale
|
||||
import { radarrReleaseTypes } from "../../interfaces/calendar/calendar-types";
|
||||
import { mediaOrganizerPriorities } from "../media-organizer";
|
||||
|
||||
const logger = createLogger({ module: "radarrIntegration" });
|
||||
|
||||
export class RadarrIntegration extends Integration implements ICalendarIntegration {
|
||||
/**
|
||||
* Gets the events in the Radarr calendar between two dates.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { z } from "zod/v4";
|
||||
|
||||
import { fetchWithTrustedCertificatesAsync } from "@homarr/certificates/server";
|
||||
import { logger } from "@homarr/log";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
|
||||
import { Integration } from "../../base/integration";
|
||||
import type { IntegrationTestingInput } from "../../base/integration";
|
||||
@@ -11,6 +11,8 @@ import type { ICalendarIntegration } from "../../interfaces/calendar/calendar-in
|
||||
import type { CalendarEvent, CalendarLink } from "../../interfaces/calendar/calendar-types";
|
||||
import { mediaOrganizerPriorities } from "../media-organizer";
|
||||
|
||||
const logger = createLogger({ module: "readarrIntegration" });
|
||||
|
||||
export class ReadarrIntegration extends Integration implements ICalendarIntegration {
|
||||
protected async testingAsync(input: IntegrationTestingInput): Promise<TestingResult> {
|
||||
const response = await input.fetchAsync(this.url("/api"), {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { z } from "zod/v4";
|
||||
|
||||
import { fetchWithTrustedCertificatesAsync } from "@homarr/certificates/server";
|
||||
import { logger } from "@homarr/log";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
|
||||
import { Integration } from "../../base/integration";
|
||||
import type { IntegrationTestingInput } from "../../base/integration";
|
||||
@@ -11,6 +11,8 @@ import type { ICalendarIntegration } from "../../interfaces/calendar/calendar-in
|
||||
import type { CalendarEvent, CalendarLink } from "../../interfaces/calendar/calendar-types";
|
||||
import { mediaOrganizerPriorities } from "../media-organizer";
|
||||
|
||||
const logger = createLogger({ module: "sonarrIntegration" });
|
||||
|
||||
export class SonarrIntegration extends Integration implements ICalendarIntegration {
|
||||
/**
|
||||
* Gets the events in the Sonarr calendar between two dates.
|
||||
|
||||
@@ -7,7 +7,7 @@ import * as ical from "node-ical";
|
||||
import { DAVClient } from "tsdav";
|
||||
|
||||
import { createHttpsAgentAsync } from "@homarr/certificates/server";
|
||||
import { logger } from "@homarr/log";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
|
||||
import { HandleIntegrationErrors } from "../base/errors/decorator";
|
||||
import { integrationTsdavHttpErrorHandler } from "../base/errors/http";
|
||||
@@ -17,6 +17,8 @@ import type { TestingResult } from "../base/test-connection/test-connection-serv
|
||||
import type { ICalendarIntegration } from "../interfaces/calendar/calendar-integration";
|
||||
import type { CalendarEvent } from "../interfaces/calendar/calendar-types";
|
||||
|
||||
const logger = createLogger({ module: "nextcloudIntegration" });
|
||||
|
||||
dayjs.extend(utc);
|
||||
dayjs.extend(timezone);
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ import type { Headers, HeadersInit, fetch as undiciFetch, Response as UndiciResp
|
||||
|
||||
import { fetchWithTrustedCertificatesAsync } from "@homarr/certificates/server";
|
||||
import { ResponseError } from "@homarr/common/server";
|
||||
import { logger } from "@homarr/log";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
|
||||
import type { IntegrationInput, IntegrationTestingInput } from "../base/integration";
|
||||
import { Integration } from "../base/integration";
|
||||
@@ -13,7 +13,7 @@ import type { ISystemHealthMonitoringIntegration } from "../interfaces/health-mo
|
||||
import type { SystemHealthMonitoring } from "../types";
|
||||
import { cpuTempSchema, fileSystemSchema, smartSchema, systemInformationSchema } from "./openmediavault-types";
|
||||
|
||||
const localLogger = logger.child({ module: "OpenMediaVaultIntegration" });
|
||||
const logger = createLogger({ module: "openMediaVaultIntegration" });
|
||||
|
||||
type SessionStoreValue =
|
||||
| { type: "header"; sessionId: string }
|
||||
@@ -151,13 +151,13 @@ export class OpenMediaVaultIntegration extends Integration implements ISystemHea
|
||||
const storedSession = await this.sessionStore.getAsync();
|
||||
|
||||
if (storedSession) {
|
||||
localLogger.debug("Using stored session for request", { integrationId: this.integration.id });
|
||||
logger.debug("Using stored session for request", { integrationId: this.integration.id });
|
||||
const response = await callback(storedSession);
|
||||
if (response.status !== 401) {
|
||||
return response;
|
||||
}
|
||||
|
||||
localLogger.debug("Session expired, getting new session", { integrationId: this.integration.id });
|
||||
logger.debug("Session expired, getting new session", { integrationId: this.integration.id });
|
||||
}
|
||||
|
||||
const session = await this.getSessionAsync();
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { z } from "zod/v4";
|
||||
|
||||
import { fetchWithTrustedCertificatesAsync } from "@homarr/certificates/server";
|
||||
import { logger } from "@homarr/log";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { ErrorWithMetadata } from "@homarr/core/infrastructure/logs/error";
|
||||
|
||||
import type { IntegrationTestingInput } from "../base/integration";
|
||||
import { Integration } from "../base/integration";
|
||||
@@ -21,6 +22,8 @@ import {
|
||||
UpstreamMediaRequestStatus,
|
||||
} from "../interfaces/media-requests/media-request-types";
|
||||
|
||||
const logger = createLogger({ module: "overseerrIntegration" });
|
||||
|
||||
interface OverseerrSearchResult {
|
||||
id: number;
|
||||
name: string;
|
||||
@@ -236,7 +239,7 @@ export class OverseerrIntegration
|
||||
}
|
||||
|
||||
public async approveRequestAsync(requestId: number): Promise<void> {
|
||||
logger.info(`Approving media request id='${requestId}' integration='${this.integration.name}'`);
|
||||
logger.info("Approving media request", { requestId, integration: this.integration.name });
|
||||
await fetchWithTrustedCertificatesAsync(this.url(`/api/v1/request/${requestId}/approve`), {
|
||||
method: "POST",
|
||||
headers: {
|
||||
@@ -245,16 +248,22 @@ export class OverseerrIntegration
|
||||
}).then((response) => {
|
||||
if (!response.ok) {
|
||||
logger.error(
|
||||
`Failed to approve media request id='${requestId}' integration='${this.integration.name}' reason='${response.status} ${response.statusText}' url='${response.url}'`,
|
||||
new ErrorWithMetadata("Failed to approve media request", {
|
||||
requestId,
|
||||
integration: this.integration.name,
|
||||
reason: `${response.status} ${response.statusText}`,
|
||||
url: response.url,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
logger.info(`Successfully approved media request id='${requestId}' integration='${this.integration.name}'`);
|
||||
logger.info("Successfully approved media request", { requestId, integration: this.integration.name });
|
||||
});
|
||||
}
|
||||
|
||||
public async declineRequestAsync(requestId: number): Promise<void> {
|
||||
logger.info(`Declining media request id='${requestId}' integration='${this.integration.name}'`);
|
||||
logger.info("Declining media request", { requestId, integration: this.integration.name });
|
||||
|
||||
await fetchWithTrustedCertificatesAsync(this.url(`/api/v1/request/${requestId}/decline`), {
|
||||
method: "POST",
|
||||
headers: {
|
||||
@@ -263,11 +272,16 @@ export class OverseerrIntegration
|
||||
}).then((response) => {
|
||||
if (!response.ok) {
|
||||
logger.error(
|
||||
`Failed to decline media request id='${requestId}' integration='${this.integration.name}' reason='${response.status} ${response.statusText}' url='${response.url}'`,
|
||||
new ErrorWithMetadata("Failed to decline media request", {
|
||||
requestId,
|
||||
integration: this.integration.name,
|
||||
reason: `${response.status} ${response.statusText}`,
|
||||
url: response.url,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
logger.info(`Successfully declined media request id='${requestId}' integration='${this.integration.name}'`);
|
||||
logger.info("Successfully declined media request", { requestId, integration: this.integration.name });
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import type { z } from "zod/v4";
|
||||
|
||||
import { fetchWithTrustedCertificatesAsync } from "@homarr/certificates/server";
|
||||
import { ResponseError } from "@homarr/common/server";
|
||||
import { logger } from "@homarr/log";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
|
||||
import type { IntegrationInput, IntegrationTestingInput } from "../../base/integration";
|
||||
import { Integration } from "../../base/integration";
|
||||
@@ -14,7 +14,7 @@ import type { DnsHoleSummaryIntegration } from "../../interfaces/dns-hole-summar
|
||||
import type { DnsHoleSummary } from "../../types";
|
||||
import { dnsBlockingGetSchema, sessionResponseSchema, statsSummaryGetSchema } from "./pi-hole-schemas-v6";
|
||||
|
||||
const localLogger = logger.child({ module: "PiHoleIntegrationV6" });
|
||||
const logger = createLogger({ module: "piHoleIntegration", version: "v6" });
|
||||
|
||||
export class PiHoleIntegrationV6 extends Integration implements DnsHoleSummaryIntegration {
|
||||
private readonly sessionStore: SessionStore<{ sid: string | null }>;
|
||||
@@ -126,13 +126,13 @@ export class PiHoleIntegrationV6 extends Integration implements DnsHoleSummaryIn
|
||||
const storedSession = await this.sessionStore.getAsync();
|
||||
|
||||
if (storedSession) {
|
||||
localLogger.debug("Using stored session for request", { integrationId: this.integration.id });
|
||||
logger.debug("Using stored session for request", { integrationId: this.integration.id });
|
||||
const response = await callback(storedSession.sid);
|
||||
if (response.status !== 401) {
|
||||
return response;
|
||||
}
|
||||
|
||||
localLogger.debug("Session expired, getting new session", { integrationId: this.integration.id });
|
||||
logger.debug("Session expired, getting new session", { integrationId: this.integration.id });
|
||||
}
|
||||
|
||||
const sessionId = await this.getSessionAsync();
|
||||
@@ -171,7 +171,7 @@ export class PiHoleIntegrationV6 extends Integration implements DnsHoleSummaryIn
|
||||
);
|
||||
}
|
||||
|
||||
localLogger.info("Received session id successfully", { integrationId: this.integration.id });
|
||||
logger.info("Received session id successfully", { integrationId: this.integration.id });
|
||||
|
||||
return result.session.sid;
|
||||
}
|
||||
@@ -185,7 +185,7 @@ export class PiHoleIntegrationV6 extends Integration implements DnsHoleSummaryIn
|
||||
fetchAsync: typeof undiciFetch = fetchWithTrustedCertificatesAsync,
|
||||
) {
|
||||
if (!sessionId) {
|
||||
localLogger.debug("No session id to clear");
|
||||
logger.debug("No session id to clear");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -197,7 +197,7 @@ export class PiHoleIntegrationV6 extends Integration implements DnsHoleSummaryIn
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
localLogger.warn("Failed to clear session", { statusCode: response.status, content: await response.text() });
|
||||
logger.warn("Failed to clear session", { statusCode: response.status, content: await response.text() });
|
||||
}
|
||||
|
||||
logger.debug("Cleared session successfully");
|
||||
|
||||
@@ -3,8 +3,8 @@ import { z } from "zod/v4";
|
||||
|
||||
import { fetchWithTrustedCertificatesAsync } from "@homarr/certificates/server";
|
||||
import { ParseError } from "@homarr/common/server";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { ImageProxy } from "@homarr/image-proxy";
|
||||
import { logger } from "@homarr/log";
|
||||
|
||||
import type { IntegrationTestingInput } from "../base/integration";
|
||||
import { Integration } from "../base/integration";
|
||||
@@ -15,6 +15,8 @@ import type { CurrentSessionsInput, StreamSession } from "../interfaces/media-se
|
||||
import type { IMediaReleasesIntegration, MediaRelease } from "../types";
|
||||
import type { PlexResponse } from "./interface";
|
||||
|
||||
const logger = createLogger({ module: "plexIntegration" });
|
||||
|
||||
export class PlexIntegration extends Integration implements IMediaServerIntegration, IMediaReleasesIntegration {
|
||||
public async getCurrentSessionsAsync(_options: CurrentSessionsInput): Promise<StreamSession[]> {
|
||||
const token = super.getSecretValue("apiKey");
|
||||
|
||||
@@ -2,7 +2,7 @@ import type { Proxmox } from "proxmox-api";
|
||||
import proxmoxApi from "proxmox-api";
|
||||
|
||||
import { fetchWithTrustedCertificatesAsync } from "@homarr/certificates/server";
|
||||
import { logger } from "@homarr/log";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
|
||||
import { HandleIntegrationErrors } from "../base/errors/decorator";
|
||||
import type { IntegrationTestingInput } from "../base/integration";
|
||||
@@ -19,6 +19,8 @@ import type {
|
||||
StorageResource,
|
||||
} from "./proxmox-types";
|
||||
|
||||
const logger = createLogger({ module: "proxmoxIntegration" });
|
||||
|
||||
@HandleIntegrationErrors([new ProxmoxApiErrorHandler()])
|
||||
export class ProxmoxIntegration extends Integration implements IClusterHealthMonitoringIntegration {
|
||||
protected async testingAsync(input: IntegrationTestingInput): Promise<TestingResult> {
|
||||
@@ -31,9 +33,13 @@ export class ProxmoxIntegration extends Integration implements IClusterHealthMon
|
||||
const proxmox = this.getPromoxApi();
|
||||
const resources = await proxmox.cluster.resources.$get();
|
||||
|
||||
logger.info(
|
||||
`Found ${resources.length} resources in Proxmox cluster node=${resources.filter((resource) => resource.type === "node").length} lxc=${resources.filter((resource) => resource.type === "lxc").length} qemu=${resources.filter((resource) => resource.type === "qemu").length} storage=${resources.filter((resource) => resource.type === "storage").length}`,
|
||||
);
|
||||
logger.info("Found resources in Proxmox cluster", {
|
||||
total: resources.length,
|
||||
node: resources.filter((resource) => resource.type === "node").length,
|
||||
lxc: resources.filter((resource) => resource.type === "lxc").length,
|
||||
qemu: resources.filter((resource) => resource.type === "qemu").length,
|
||||
storage: resources.filter((resource) => resource.type === "storage").length,
|
||||
});
|
||||
|
||||
const mappedResources = resources.map(mapResource).filter((resource) => resource !== null);
|
||||
return {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { RequestInit, Response } from "undici";
|
||||
|
||||
import { fetchWithTrustedCertificatesAsync } from "@homarr/certificates/server";
|
||||
import { logger } from "@homarr/log";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
|
||||
import type { IntegrationTestingInput } from "../base/integration";
|
||||
import { Integration } from "../base/integration";
|
||||
@@ -15,7 +15,7 @@ import type {
|
||||
} from "../interfaces/releases-providers/releases-providers-types";
|
||||
import { releasesResponseSchema } from "./quay-schemas";
|
||||
|
||||
const localLogger = logger.child({ module: "QuayIntegration" });
|
||||
const logger = createLogger({ module: "quayIntegration" });
|
||||
|
||||
export class QuayIntegration extends Integration implements ReleasesProviderIntegration {
|
||||
private async withHeadersAsync(callback: (headers: RequestInit["headers"]) => Promise<Response>): Promise<Response> {
|
||||
@@ -45,7 +45,7 @@ export class QuayIntegration extends Integration implements ReleasesProviderInte
|
||||
private parseIdentifier(identifier: string) {
|
||||
const [owner, name] = identifier.split("/");
|
||||
if (!owner || !name) {
|
||||
localLogger.warn(`Invalid identifier format. Expected 'owner/name', for ${identifier} with Quay integration`, {
|
||||
logger.warn("Invalid identifier format. Expected 'owner/name' for identifier", {
|
||||
identifier,
|
||||
});
|
||||
return null;
|
||||
|
||||
@@ -3,7 +3,7 @@ import z from "zod";
|
||||
|
||||
import { createId } from "@homarr/common";
|
||||
import { RequestError, ResponseError } from "@homarr/common/server";
|
||||
import { logger } from "@homarr/log";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
|
||||
import type { IntegrationTestingInput } from "../base/integration";
|
||||
import { Integration } from "../base/integration";
|
||||
@@ -11,7 +11,7 @@ import type { TestingResult } from "../base/test-connection/test-connection-serv
|
||||
import type { ISystemHealthMonitoringIntegration } from "../interfaces/health-monitoring/health-monitoring-integration";
|
||||
import type { SystemHealthMonitoring } from "../interfaces/health-monitoring/health-monitoring-types";
|
||||
|
||||
const localLogger = logger.child({ module: "TrueNasIntegration" });
|
||||
const logger = createLogger({ module: "trueNasIntegration" });
|
||||
|
||||
const NETWORK_MULTIPLIER = 100;
|
||||
|
||||
@@ -45,14 +45,14 @@ export class TrueNasIntegration extends Integration implements ISystemHealthMoni
|
||||
* @see https://www.truenas.com/docs/api/scale_websocket_api.html
|
||||
*/
|
||||
private async connectWebSocketAsync(): Promise<WebSocket> {
|
||||
localLogger.debug("Connecting to websocket server", {
|
||||
logger.debug("Connecting to websocket server", {
|
||||
url: this.wsUrl(),
|
||||
});
|
||||
const webSocket = new WebSocket(this.wsUrl());
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
webSocket.onopen = () => {
|
||||
localLogger.debug("Connected to websocket server", {
|
||||
logger.debug("Connected to websocket server", {
|
||||
url: this.wsUrl(),
|
||||
});
|
||||
resolve(webSocket);
|
||||
@@ -97,7 +97,7 @@ export class TrueNasIntegration extends Integration implements ISystemHealthMoni
|
||||
* @see https://www.truenas.com/docs/api/scale_websocket_api.html#websocket_protocol
|
||||
*/
|
||||
private async authenticateWebSocketAsync(webSocket?: WebSocket): Promise<void> {
|
||||
localLogger.debug("Authenticating with username and password", {
|
||||
logger.debug("Authenticating with username and password", {
|
||||
url: this.wsUrl(),
|
||||
});
|
||||
const response = await this.requestAsync(
|
||||
@@ -107,7 +107,7 @@ export class TrueNasIntegration extends Integration implements ISystemHealthMoni
|
||||
);
|
||||
const result = await z.boolean().parseAsync(response);
|
||||
if (!result) throw new ResponseError({ status: 401 });
|
||||
localLogger.debug("Authenticated successfully with username and password", {
|
||||
logger.debug("Authenticated successfully with username and password", {
|
||||
url: this.wsUrl(),
|
||||
});
|
||||
}
|
||||
@@ -117,7 +117,7 @@ export class TrueNasIntegration extends Integration implements ISystemHealthMoni
|
||||
* @see https://www.truenas.com/docs/api/scale_websocket_api.html#reporting
|
||||
*/
|
||||
private async getReportingAsync(): Promise<ReportingItem[]> {
|
||||
localLogger.debug("Retrieving reporting data", {
|
||||
logger.debug("Retrieving reporting data", {
|
||||
url: this.wsUrl(),
|
||||
});
|
||||
|
||||
@@ -141,7 +141,7 @@ export class TrueNasIntegration extends Integration implements ISystemHealthMoni
|
||||
]);
|
||||
const result = await z.array(reportingItemSchema).parseAsync(response);
|
||||
|
||||
localLogger.debug("Retrieved reporting data", {
|
||||
logger.debug("Retrieved reporting data", {
|
||||
url: this.wsUrl(),
|
||||
count: result.length,
|
||||
});
|
||||
@@ -153,7 +153,7 @@ export class TrueNasIntegration extends Integration implements ISystemHealthMoni
|
||||
* @see https://www.truenas.com/docs/core/13.0/api/core_websocket_api.html#interface
|
||||
*/
|
||||
private async getNetworkInterfacesAsync(): Promise<z.infer<typeof networkInterfaceSchema>> {
|
||||
localLogger.debug("Retrieving available network-interfaces", {
|
||||
logger.debug("Retrieving available network-interfaces", {
|
||||
url: this.wsUrl(),
|
||||
});
|
||||
|
||||
@@ -163,7 +163,7 @@ export class TrueNasIntegration extends Integration implements ISystemHealthMoni
|
||||
]);
|
||||
const result = await networkInterfaceSchema.parseAsync(response);
|
||||
|
||||
localLogger.debug("Retrieved available network-interfaces", {
|
||||
logger.debug("Retrieved available network-interfaces", {
|
||||
url: this.wsUrl(),
|
||||
count: result.length,
|
||||
});
|
||||
@@ -177,7 +177,7 @@ export class TrueNasIntegration extends Integration implements ISystemHealthMoni
|
||||
private async getReportingNetdataAsync(): Promise<z.infer<typeof reportingNetDataSchema>> {
|
||||
const networkInterfaces = await this.getNetworkInterfacesAsync();
|
||||
|
||||
localLogger.debug("Retrieving reporting network data", {
|
||||
logger.debug("Retrieving reporting network data", {
|
||||
url: this.wsUrl(),
|
||||
});
|
||||
|
||||
@@ -193,7 +193,7 @@ export class TrueNasIntegration extends Integration implements ISystemHealthMoni
|
||||
]);
|
||||
const result = await reportingNetDataSchema.parseAsync(response);
|
||||
|
||||
localLogger.debug("Retrieved reporting-network-data", {
|
||||
logger.debug("Retrieved reporting-network-data", {
|
||||
url: this.wsUrl(),
|
||||
count: result.length,
|
||||
});
|
||||
@@ -205,14 +205,14 @@ export class TrueNasIntegration extends Integration implements ISystemHealthMoni
|
||||
* @see https://www.truenas.com/docs/api/scale_websocket_api.html#system
|
||||
*/
|
||||
private async getSystemInformationAsync(): Promise<z.infer<typeof systemInfoSchema>> {
|
||||
localLogger.debug("Retrieving system-information", {
|
||||
logger.debug("Retrieving system-information", {
|
||||
url: this.wsUrl(),
|
||||
});
|
||||
|
||||
const response = await this.requestAsync("system.info");
|
||||
const result = await systemInfoSchema.parseAsync(response);
|
||||
|
||||
localLogger.debug("Retrieved system-information", {
|
||||
logger.debug("Retrieved system-information", {
|
||||
url: this.wsUrl(),
|
||||
});
|
||||
return result;
|
||||
@@ -262,7 +262,7 @@ export class TrueNasIntegration extends Integration implements ISystemHealthMoni
|
||||
private async requestAsync(method: string, params: unknown[] = [], webSocketOverride?: WebSocket) {
|
||||
let webSocket = webSocketOverride ?? this.webSocket;
|
||||
if (!webSocket || webSocket.readyState !== WebSocket.OPEN) {
|
||||
localLogger.debug("Connecting to websocket", {
|
||||
logger.debug("Connecting to websocket", {
|
||||
url: this.wsUrl(),
|
||||
});
|
||||
// We can only land here with static webSocket
|
||||
@@ -282,7 +282,7 @@ export class TrueNasIntegration extends Integration implements ISystemHealthMoni
|
||||
|
||||
clearTimeout(timeoutId);
|
||||
webSocket.removeEventListener("message", handler);
|
||||
localLogger.debug("Received method response", {
|
||||
logger.debug("Received method response", {
|
||||
id,
|
||||
method,
|
||||
url: this.wsUrl(),
|
||||
@@ -305,7 +305,7 @@ export class TrueNasIntegration extends Integration implements ISystemHealthMoni
|
||||
|
||||
webSocket.addEventListener("message", handler);
|
||||
|
||||
localLogger.debug("Sending method request", {
|
||||
logger.debug("Sending method request", {
|
||||
id,
|
||||
method,
|
||||
url: this.wsUrl(),
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
import baseConfig from "@homarr/eslint-config/base";
|
||||
|
||||
/** @type {import('typescript-eslint').Config} */
|
||||
export default [...baseConfig];
|
||||
@@ -1,39 +0,0 @@
|
||||
{
|
||||
"name": "@homarr/log",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"license": "Apache-2.0",
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": "./src/index.ts",
|
||||
"./constants": "./src/constants.ts",
|
||||
"./env": "./src/env.ts"
|
||||
},
|
||||
"typesVersions": {
|
||||
"*": {
|
||||
"*": [
|
||||
"src/*"
|
||||
]
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"clean": "rm -rf .turbo node_modules",
|
||||
"format": "prettier --check . --ignore-path ../../.gitignore",
|
||||
"lint": "eslint",
|
||||
"typecheck": "tsc --noEmit"
|
||||
},
|
||||
"prettier": "@homarr/prettier-config",
|
||||
"dependencies": {
|
||||
"@homarr/core": "workspace:^0.1.0",
|
||||
"superjson": "2.2.6",
|
||||
"winston": "3.19.0",
|
||||
"zod": "^4.1.13"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@homarr/eslint-config": "workspace:^0.2.0",
|
||||
"@homarr/prettier-config": "workspace:^0.1.0",
|
||||
"@homarr/tsconfig": "workspace:^0.1.0",
|
||||
"eslint": "^9.39.1",
|
||||
"typescript": "^5.9.3"
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
import { z } from "zod/v4";
|
||||
|
||||
import { createEnv } from "@homarr/core/infrastructure/env";
|
||||
|
||||
import { logLevels } from "./constants";
|
||||
|
||||
export const env = createEnv({
|
||||
server: {
|
||||
LOG_LEVEL: z.enum(logLevels).default("info"),
|
||||
},
|
||||
experimental__runtimeEnv: process.env,
|
||||
});
|
||||
@@ -1,45 +0,0 @@
|
||||
import type { transport as Transport } from "winston";
|
||||
import winston, { format, transports } from "winston";
|
||||
|
||||
import { env } from "./env";
|
||||
import { formatErrorCause, formatErrorStack } from "./error";
|
||||
import { formatMetadata } from "./metadata";
|
||||
import { RedisTransport } from "./redis-transport";
|
||||
|
||||
const logMessageFormat = format.printf(({ level, message, timestamp, cause, stack, ...metadata }) => {
|
||||
if (!cause && !stack) {
|
||||
return `${timestamp as string} ${level}: ${message as string}`;
|
||||
}
|
||||
|
||||
const formatedStack = formatErrorStack(stack as string | undefined);
|
||||
|
||||
if (!cause) {
|
||||
return `${timestamp as string} ${level}: ${message as string} ${formatMetadata(metadata)}\n${formatedStack}`;
|
||||
}
|
||||
|
||||
return `${timestamp as string} ${level}: ${message as string} ${formatMetadata(metadata)}\n${formatedStack}${formatErrorCause(cause)}`;
|
||||
});
|
||||
|
||||
const logTransports: Transport[] = [new transports.Console()];
|
||||
|
||||
// Only add the Redis transport if we are not in CI
|
||||
if (!(Boolean(process.env.CI) || Boolean(process.env.DISABLE_REDIS_LOGS))) {
|
||||
logTransports.push(
|
||||
new RedisTransport({
|
||||
level: "debug",
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
const logger = winston.createLogger({
|
||||
format: format.combine(
|
||||
format.colorize(),
|
||||
format.timestamp(),
|
||||
format.errors({ stack: true, cause: true }),
|
||||
logMessageFormat,
|
||||
),
|
||||
transports: logTransports,
|
||||
level: env.LOG_LEVEL,
|
||||
});
|
||||
|
||||
export { logger };
|
||||
@@ -1,8 +0,0 @@
|
||||
{
|
||||
"extends": "@homarr/tsconfig/base.json",
|
||||
"compilerOptions": {
|
||||
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json"
|
||||
},
|
||||
"include": ["*.ts", "src"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
@@ -27,10 +27,10 @@
|
||||
"prettier": "@homarr/prettier-config",
|
||||
"dependencies": {
|
||||
"@homarr/common": "workspace:^0.1.0",
|
||||
"@homarr/core": "workspace:^0.1.0",
|
||||
"@homarr/db": "workspace:^0.1.0",
|
||||
"@homarr/definitions": "workspace:^0.1.0",
|
||||
"@homarr/form": "workspace:^0.1.0",
|
||||
"@homarr/log": "workspace:^0.1.0",
|
||||
"@homarr/modals": "workspace:^0.1.0",
|
||||
"@homarr/notifications": "workspace:^0.1.0",
|
||||
"@homarr/old-schema": "workspace:^0.1.0",
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
import AdmZip from "adm-zip";
|
||||
import { z } from "zod/v4";
|
||||
|
||||
import { logger } from "@homarr/log";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { ErrorWithMetadata } from "@homarr/core/infrastructure/logs/error";
|
||||
import { oldmarrConfigSchema } from "@homarr/old-schema";
|
||||
|
||||
import { oldmarrImportUserSchema } from "../user-schema";
|
||||
import type { analyseOldmarrImportInputSchema } from "./input";
|
||||
|
||||
const logger = createLogger({ module: "analyseOldmarrImport" });
|
||||
|
||||
export const analyseOldmarrImportForRouterAsync = async (input: z.infer<typeof analyseOldmarrImportInputSchema>) => {
|
||||
const { configs, checksum, users } = await analyseOldmarrImportAsync(input.file);
|
||||
|
||||
@@ -25,7 +28,13 @@ export const analyseOldmarrImportAsync = async (file: File) => {
|
||||
const configs = configEntries.map((entry) => {
|
||||
const result = oldmarrConfigSchema.safeParse(JSON.parse(entry.getData().toString()));
|
||||
if (!result.success) {
|
||||
logger.error(`Failed to parse config ${entry.entryName} with error: ${JSON.stringify(result.error)}`);
|
||||
logger.error(
|
||||
new ErrorWithMetadata(
|
||||
"Failed to parse oldmarr config",
|
||||
{ entryName: entry.entryName },
|
||||
{ cause: result.error },
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -57,7 +66,7 @@ const parseUsers = (entry: AdmZip.IZipEntry | undefined) => {
|
||||
|
||||
const result = z.array(oldmarrImportUserSchema).safeParse(JSON.parse(entry.getData().toString()));
|
||||
if (!result.success) {
|
||||
logger.error(`Failed to parse users with error: ${JSON.stringify(result.error)}`);
|
||||
logger.error(new Error("Failed to parse users", { cause: result.error }));
|
||||
}
|
||||
|
||||
return result.data ?? [];
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user