refactor(logs): move to core package (#4586)

This commit is contained in:
Meier Lukas
2025-12-16 23:37:44 +01:00
committed by GitHub
parent d86af072bf
commit d348abfe4a
145 changed files with 971 additions and 708 deletions

View File

@@ -2,7 +2,7 @@
import "@homarr/auth/env";
import "@homarr/db/env";
import "@homarr/common/env";
import "@homarr/log/env";
import "@homarr/core/infrastructure/logs/env";
import "@homarr/docker/env";
import type { NextConfig } from "next";

View File

@@ -36,7 +36,6 @@
"@homarr/icons": "workspace:^0.1.0",
"@homarr/image-proxy": "workspace:^0.1.0",
"@homarr/integrations": "workspace:^0.1.0",
"@homarr/log": "workspace:^",
"@homarr/modals": "workspace:^0.1.0",
"@homarr/modals-collection": "workspace:^0.1.0",
"@homarr/notifications": "workspace:^0.1.0",

View File

@@ -11,8 +11,9 @@ import { IntegrationProvider } from "@homarr/auth/client";
import { auth } from "@homarr/auth/next";
import { getIntegrationsWithPermissionsAsync } from "@homarr/auth/server";
import { isNullOrWhitespace } from "@homarr/common";
import { createLogger } from "@homarr/core/infrastructure/logs";
import { ErrorWithMetadata } from "@homarr/core/infrastructure/logs/error";
import type { WidgetKind } from "@homarr/definitions";
import { logger } from "@homarr/log";
import { getI18n } from "@homarr/translation/server";
import { prefetchForKindAsync } from "@homarr/widgets/prefetch";
@@ -22,6 +23,8 @@ import type { Board, Item } from "../_types";
import { DynamicClientBoard } from "./_dynamic-client";
import { BoardContentHeaderActions } from "./_header-actions";
const logger = createLogger({ module: "createBoardContentPage" });
export type Params = Record<string, unknown>;
interface Props<TParams extends Params> {
@@ -57,7 +60,13 @@ export const createBoardContentPage = <TParams extends Record<string, unknown>>(
for (const [kind, items] of itemsMap) {
await prefetchForKindAsync(kind, queryClient, items).catch((error) => {
logger.error(new Error("Failed to prefetch widget", { cause: error }));
logger.error(
new ErrorWithMetadata(
"Failed to prefetch widget",
{ widgetKind: kind, itemCount: items.length },
{ cause: error },
),
);
});
}

View File

@@ -6,7 +6,7 @@ import { TRPCError } from "@trpc/server";
import { auth } from "@homarr/auth/next";
import { BoardProvider } from "@homarr/boards/context";
import { EditModeProvider } from "@homarr/boards/edit-mode";
import { logger } from "@homarr/log";
import { createLogger } from "@homarr/core/infrastructure/logs";
import { MainHeader } from "~/components/layout/header";
import { BoardLogoWithTitle } from "~/components/layout/logo/board-logo";
@@ -18,6 +18,8 @@ import { CustomCss } from "./(content)/_custom-css";
import { BoardReadyProvider } from "./(content)/_ready-context";
import { BoardMantineProvider } from "./(content)/_theme";
const logger = createLogger({ module: "createBoardLayout" });
interface CreateBoardLayoutProps<TParams extends Params> {
headerActions: JSX.Element;
getInitialBoardAsync: (params: TParams) => Promise<Board>;

View File

@@ -2,8 +2,8 @@
import { Select } from "@mantine/core";
import type { LogLevel } from "@homarr/log/constants";
import { logLevelConfiguration, logLevels } from "@homarr/log/constants";
import type { LogLevel } from "@homarr/core/infrastructure/logs/constants";
import { logLevelConfiguration, logLevels } from "@homarr/core/infrastructure/logs/constants";
import { useI18n } from "@homarr/translation/client";
import { useLogContext } from "./log-context";

View File

@@ -3,8 +3,8 @@
import type { PropsWithChildren } from "react";
import { createContext, useContext, useMemo, useState } from "react";
import type { LogLevel } from "@homarr/log/constants";
import { logLevels } from "@homarr/log/constants";
import type { LogLevel } from "@homarr/core/infrastructure/logs/constants";
import { logLevels } from "@homarr/core/infrastructure/logs/constants";
const LogContext = createContext<{
level: LogLevel;

View File

@@ -7,7 +7,7 @@ import "@xterm/xterm/css/xterm.css";
import { notFound } from "next/navigation";
import { auth } from "@homarr/auth/next";
import { env } from "@homarr/log/env";
import { logsEnv } from "@homarr/core/infrastructure/logs/env";
import { DynamicBreadcrumb } from "~/components/navigation/dynamic-breadcrumb";
import { fullHeightWithoutHeaderAndFooter } from "~/constants";
@@ -35,7 +35,7 @@ export default async function LogsManagementPage() {
}
return (
<LogContextProvider defaultLevel={env.LOG_LEVEL}>
<LogContextProvider defaultLevel={logsEnv.LEVEL}>
<Group justify="space-between" align="center" wrap="nowrap">
<DynamicBreadcrumb />
<LogLevelSelection />

View File

@@ -6,9 +6,12 @@ import { appRouter, createTRPCContext } from "@homarr/api";
import type { Session } from "@homarr/auth";
import { hashPasswordAsync } from "@homarr/auth";
import { createSessionAsync } from "@homarr/auth/server";
import { createLogger } from "@homarr/core/infrastructure/logs";
import { ErrorWithMetadata } from "@homarr/core/infrastructure/logs/error";
import { db, eq } from "@homarr/db";
import { apiKeys } from "@homarr/db/schema";
import { logger } from "@homarr/log";
const logger = createLogger({ module: "trpcOpenApiRoute" });
const handlerAsync = async (req: NextRequest) => {
const apiKeyHeaderValue = req.headers.get("ApiKey");
@@ -27,7 +30,7 @@ const handlerAsync = async (req: NextRequest) => {
router: appRouter,
createContext: () => createTRPCContext({ session, headers: req.headers }),
onError({ error, path, type }) {
logger.error(new Error(`tRPC Error with ${type} on '${path}'`, { cause: error.cause }));
logger.error(new ErrorWithMetadata("tRPC Error occured", { path, type }, { cause: error }));
},
});
};
@@ -48,9 +51,10 @@ const getSessionOrDefaultFromHeadersAsync = async (
const [apiKeyId, apiKey] = apiKeyHeaderValue.split(".");
if (!apiKeyId || !apiKey) {
logger.warn(
`An attempt to authenticate over API has failed due to invalid API key format ip='${ipAdress}' userAgent='${userAgent}'`,
);
logger.warn("An attempt to authenticate over API has failed due to invalid API key format", {
ipAdress,
userAgent,
});
return null;
}
@@ -74,18 +78,21 @@ const getSessionOrDefaultFromHeadersAsync = async (
});
if (!apiKeyFromDb) {
logger.warn(`An attempt to authenticate over API has failed ip='${ipAdress}' userAgent='${userAgent}'`);
logger.warn("An attempt to authenticate over API has failed", { ipAdress, userAgent });
return null;
}
const hashedApiKey = await hashPasswordAsync(apiKey, apiKeyFromDb.salt);
if (apiKeyFromDb.apiKey !== hashedApiKey) {
logger.warn(`An attempt to authenticate over API has failed ip='${ipAdress}' userAgent='${userAgent}'`);
logger.warn("An attempt to authenticate over API has failed", { ipAdress, userAgent });
return null;
}
logger.info(`Read session from API request and found user ${apiKeyFromDb.user.name} (${apiKeyFromDb.user.id})`);
logger.info("Read session from API request and found user", {
name: apiKeyFromDb.user.name,
id: apiKeyFromDb.user.id,
});
return await createSessionAsync(db, apiKeyFromDb.user);
};

View File

@@ -1,8 +1,10 @@
import { NextRequest } from "next/server";
import { createHandlersAsync } from "@homarr/auth";
import { createLogger } from "@homarr/core/infrastructure/logs";
import type { SupportedAuthProvider } from "@homarr/definitions";
import { logger } from "@homarr/log";
const logger = createLogger({ module: "nextAuthRoute" });
export const GET = async (req: NextRequest) => {
const { handlers } = await createHandlersAsync(extractProvider(req), isSecureCookieEnabled(req));

View File

@@ -1,13 +1,16 @@
import { performance } from "perf_hooks";
import { createLogger } from "@homarr/core/infrastructure/logs";
import { ErrorWithMetadata } from "@homarr/core/infrastructure/logs/error";
import { db } from "@homarr/db";
import { logger } from "@homarr/log";
import { handshakeAsync } from "@homarr/redis";
const logger = createLogger({ module: "healthLiveRoute" });
export async function GET() {
const timeBeforeHealthCheck = performance.now();
const response = await executeAndAggregateAllHealthChecksAsync();
logger.info(`Completed healthcheck after ${performance.now() - timeBeforeHealthCheck}ms`);
logger.info("Completed healthcheck", { elapsed: `${performance.now() - timeBeforeHealthCheck}ms` });
if (response.status === "healthy") {
return new Response(JSON.stringify(response), {
@@ -73,7 +76,7 @@ const executeHealthCheckSafelyAsync = async (
};
} catch (error) {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
logger.error(`Healthcheck '${name}' has failed: ${error}`);
logger.error(new ErrorWithMetadata("Healthcheck failed", { name }, { cause: error }));
return {
status: "unhealthy",
values: {

View File

@@ -3,7 +3,10 @@ import { fetchRequestHandler } from "@trpc/server/adapters/fetch";
import { appRouter, createTRPCContext } from "@homarr/api";
import { trpcPath } from "@homarr/api/shared";
import { auth } from "@homarr/auth/next";
import { logger } from "@homarr/log";
import { createLogger } from "@homarr/core/infrastructure/logs";
import { ErrorWithMetadata } from "@homarr/core/infrastructure/logs/error";
const logger = createLogger({ module: "trpcRoute" });
/**
* Configure basic CORS headers
@@ -31,7 +34,7 @@ const handler = auth(async (req) => {
req,
createContext: () => createTRPCContext({ session: req.auth, headers: req.headers }),
onError({ error, path, type }) {
logger.error(new Error(`tRPC Error with ${type} on '${path}'`, { cause: error.cause }));
logger.error(new ErrorWithMetadata("tRPC Error occured", { path, type }, { cause: error }));
},
});

View File

@@ -3,7 +3,9 @@ import "server-only";
import { notFound, redirect } from "next/navigation";
import { TRPCError } from "@trpc/server";
import { logger } from "@homarr/log";
import { createLogger } from "@homarr/core/infrastructure/logs";
const logger = createLogger({ module: "trpcCatchError" });
export const catchTrpcNotFound = (err: unknown) => {
if (err instanceof TRPCError && err.code === "NOT_FOUND") {

View File

@@ -23,6 +23,7 @@
"@homarr/analytics": "workspace:^0.1.0",
"@homarr/auth": "workspace:^0.1.0",
"@homarr/common": "workspace:^0.1.0",
"@homarr/core": "workspace:^",
"@homarr/cron-job-api": "workspace:^0.1.0",
"@homarr/cron-jobs": "workspace:^0.1.0",
"@homarr/cron-jobs-core": "workspace:^0.1.0",
@@ -30,7 +31,6 @@
"@homarr/definitions": "workspace:^0.1.0",
"@homarr/icons": "workspace:^0.1.0",
"@homarr/integrations": "workspace:^0.1.0",
"@homarr/log": "workspace:^",
"@homarr/ping": "workspace:^0.1.0",
"@homarr/redis": "workspace:^0.1.0",
"@homarr/request-handler": "workspace:^0.1.0",

View File

@@ -1,11 +1,13 @@
import { schedule, validate as validateCron } from "node-cron";
import { createLogger } from "@homarr/core/infrastructure/logs";
import type { IJobManager } from "@homarr/cron-job-api";
import type { jobGroup as cronJobGroup, JobGroupKeys } from "@homarr/cron-jobs";
import type { Database, InferInsertModel } from "@homarr/db";
import { eq } from "@homarr/db";
import { cronJobConfigurations } from "@homarr/db/schema";
import { logger } from "@homarr/log";
const logger = createLogger({ module: "jobManager" });
export class JobManager implements IJobManager {
constructor(
@@ -23,7 +25,7 @@ export class JobManager implements IJobManager {
await this.jobGroup.stopAsync(name);
}
public async updateIntervalAsync(name: JobGroupKeys, cron: string): Promise<void> {
logger.info(`Updating cron job interval name="${name}" expression="${cron}"`);
logger.info("Updating cron job interval", { name, expression: cron });
const job = this.jobGroup.getJobRegistry().get(name);
if (!job) throw new Error(`Job ${name} not found`);
if (!validateCron(cron)) {
@@ -38,22 +40,22 @@ export class JobManager implements IJobManager {
name,
}),
);
logger.info(`Cron job interval updated name="${name}" expression="${cron}"`);
logger.info("Cron job interval updated", { name, expression: cron });
}
public async disableAsync(name: JobGroupKeys): Promise<void> {
logger.info(`Disabling cron job name="${name}"`);
logger.info("Disabling cron job", { name });
const job = this.jobGroup.getJobRegistry().get(name);
if (!job) throw new Error(`Job ${name} not found`);
await this.updateConfigurationAsync(name, { isEnabled: false });
await this.jobGroup.stopAsync(name);
logger.info(`Cron job disabled name="${name}"`);
logger.info("Cron job disabled", { name });
}
public async enableAsync(name: JobGroupKeys): Promise<void> {
logger.info(`Enabling cron job name="${name}"`);
logger.info("Enabling cron job", { name });
await this.updateConfigurationAsync(name, { isEnabled: true });
await this.jobGroup.startAsync(name);
logger.info(`Cron job enabled name="${name}"`);
logger.info("Cron job enabled", { name });
}
private async updateConfigurationAsync(
@@ -64,9 +66,11 @@ export class JobManager implements IJobManager {
where: (table, { eq }) => eq(table.name, name),
});
logger.debug(
`Updating cron job configuration name="${name}" configuration="${JSON.stringify(configuration)}" exists="${Boolean(existingConfig)}"`,
);
logger.debug("Updating cron job configuration", {
name,
configuration: JSON.stringify(configuration),
exists: Boolean(existingConfig),
});
if (existingConfig) {
await this.db
@@ -74,7 +78,10 @@ export class JobManager implements IJobManager {
// prevent updating the name, as it is the primary key
.set({ ...configuration, name: undefined })
.where(eq(cronJobConfigurations.name, name));
logger.debug(`Cron job configuration updated name="${name}" configuration="${JSON.stringify(configuration)}"`);
logger.debug("Cron job configuration updated", {
name,
configuration: JSON.stringify(configuration),
});
return;
}
@@ -86,7 +93,10 @@ export class JobManager implements IJobManager {
cronExpression: configuration.cronExpression ?? job.cronExpression,
isEnabled: configuration.isEnabled ?? true,
});
logger.debug(`Cron job configuration updated name="${name}" configuration="${JSON.stringify(configuration)}"`);
logger.debug("Cron job configuration updated", {
name,
configuration: JSON.stringify(configuration),
});
}
public async getAllAsync(): Promise<

View File

@@ -5,16 +5,19 @@ import type { FastifyTRPCPluginOptions } from "@trpc/server/adapters/fastify";
import { fastifyTRPCPlugin } from "@trpc/server/adapters/fastify";
import fastify from "fastify";
import { createLogger } from "@homarr/core/infrastructure/logs";
import { ErrorWithMetadata } from "@homarr/core/infrastructure/logs/error";
import type { JobRouter } from "@homarr/cron-job-api";
import { jobRouter } from "@homarr/cron-job-api";
import { CRON_JOB_API_KEY_HEADER, CRON_JOB_API_PATH, CRON_JOB_API_PORT } from "@homarr/cron-job-api/constants";
import { jobGroup } from "@homarr/cron-jobs";
import { db } from "@homarr/db";
import { logger } from "@homarr/log";
import { JobManager } from "./job-manager";
import { onStartAsync } from "./on-start";
const logger = createLogger({ module: "tasksMain" });
const server = fastify({
maxParamLength: 5000,
});
@@ -27,7 +30,7 @@ server.register(fastifyTRPCPlugin, {
apiKey: req.headers[CRON_JOB_API_KEY_HEADER] as string | undefined,
}),
onError({ path, error }) {
logger.error(new Error(`Error in tasks tRPC handler path="${path}"`, { cause: error }));
logger.error(new ErrorWithMetadata("Error in tasks tRPC handler", { path }, { cause: error }));
},
} satisfies FastifyTRPCPluginOptions<JobRouter>["trpcOptions"],
});
@@ -39,9 +42,11 @@ void (async () => {
try {
await server.listen({ port: CRON_JOB_API_PORT });
logger.info(`Tasks web server started successfully port="${CRON_JOB_API_PORT}"`);
logger.info("Tasks web server started successfully", { port: CRON_JOB_API_PORT });
} catch (err) {
logger.error(new Error(`Failed to start tasks web server port="${CRON_JOB_API_PORT}"`, { cause: err }));
logger.error(
new ErrorWithMetadata("Failed to start tasks web server", { port: CRON_JOB_API_PORT }, { cause: err }),
);
process.exit(1);
}
})();

View File

@@ -1,7 +1,7 @@
import { logger } from "@homarr/log";
import { createLogger } from "@homarr/core/infrastructure/logs";
import { updateCheckerRequestHandler } from "@homarr/request-handler/update-checker";
const localLogger = logger.child({ module: "invalidateUpdateCheckerCache" });
const logger = createLogger({ module: "invalidateUpdateCheckerCache" });
/**
* Invalidates the update checker cache on startup to ensure fresh data.
@@ -11,8 +11,8 @@ export async function invalidateUpdateCheckerCacheAsync() {
try {
const handler = updateCheckerRequestHandler.handler({});
await handler.invalidateAsync();
localLogger.debug("Update checker cache invalidated");
logger.debug("Update checker cache invalidated");
} catch (error) {
localLogger.error(new Error("Failed to invalidate update checker cache", { cause: error }));
logger.error(new Error("Failed to invalidate update checker cache", { cause: error }));
}
}

View File

@@ -1,10 +1,10 @@
import { env } from "@homarr/auth/env";
import { createLogger } from "@homarr/core/infrastructure/logs";
import { db, eq, inArray } from "@homarr/db";
import { sessions, users } from "@homarr/db/schema";
import { supportedAuthProviders } from "@homarr/definitions";
import { logger } from "@homarr/log";
const localLogger = logger.child({ module: "sessionCleanup" });
const logger = createLogger({ module: "sessionCleanup" });
/**
* Deletes sessions for users that have inactive auth providers.
@@ -29,11 +29,13 @@ export async function cleanupSessionsAsync() {
await db.delete(sessions).where(inArray(sessions.userId, userIds));
if (sessionsWithInactiveProviders.length > 0) {
localLogger.info(`Deleted sessions for inactive providers count=${userIds.length}`);
logger.info("Deleted sessions for inactive providers", {
count: userIds.length,
});
} else {
localLogger.debug("No sessions to delete");
logger.debug("No sessions to delete");
}
} catch (error) {
localLogger.error(new Error("Failed to clean up sessions", { cause: error }));
logger.error(new Error("Failed to clean up sessions", { cause: error }));
}
}

View File

@@ -20,9 +20,9 @@
"@homarr/api": "workspace:^0.1.0",
"@homarr/auth": "workspace:^0.1.0",
"@homarr/common": "workspace:^0.1.0",
"@homarr/core": "workspace:^",
"@homarr/db": "workspace:^0.1.0",
"@homarr/definitions": "workspace:^0.1.0",
"@homarr/log": "workspace:^",
"@homarr/redis": "workspace:^0.1.0",
"@homarr/validation": "workspace:^0.1.0",
"dotenv": "^17.2.3",

View File

@@ -4,8 +4,10 @@ import { WebSocketServer } from "ws";
import { appRouter, createTRPCContext } from "@homarr/api/websocket";
import { getSessionFromToken, sessionTokenCookieName } from "@homarr/auth";
import { parseCookies } from "@homarr/common";
import { createLogger } from "@homarr/core/infrastructure/logs";
import { db } from "@homarr/db";
import { logger } from "@homarr/log";
const logger = createLogger({ module: "websocketMain" });
const wss = new WebSocketServer({
port: 3001,

View File

@@ -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"

View File

@@ -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");

View File

@@ -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",

View File

@@ -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")

View File

@@ -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")

View File

@@ -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({

View File

@@ -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 },
),
);

View File

@@ -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",

View File

@@ -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",

View File

@@ -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",

View File

@@ -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",

View File

@@ -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",

View File

@@ -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",

View File

@@ -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",

View File

@@ -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",

View File

@@ -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",

View File

@@ -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")

View File

@@ -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;

View File

@@ -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 {

View File

@@ -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 ?? "");

View File

@@ -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,
});
});
}),

View File

@@ -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,

View File

@@ -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",

View File

@@ -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,
});
}
};

View File

@@ -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",

View File

@@ -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,

View File

@@ -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 {

View File

@@ -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",

View File

@@ -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 ??= {};

View File

@@ -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,

View File

@@ -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,
});

View File

@@ -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;
}

View File

@@ -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,
});

View File

@@ -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,

View File

@@ -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,
});

View File

@@ -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: "?" });

View File

@@ -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,
});

View File

@@ -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;
}

View File

@@ -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 });
}

View File

@@ -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);

View File

@@ -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} `));
});
});

View File

@@ -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": {

View 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_"),
});

View 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;
}
}

View File

@@ -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)]));
};

View 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)}`;
}),
);

View File

@@ -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(" ");

View 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;

View 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();

View File

@@ -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],

View File

@@ -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",

View File

@@ -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"
},

View File

@@ -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),

View File

@@ -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();
}
},

View File

@@ -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),

View File

@@ -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;
}

View File

@@ -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",

View File

@@ -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 }));
}
}),
);

View File

@@ -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,
});
});

View File

@@ -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({

View File

@@ -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 }));
}
}
}

View File

@@ -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 }));
}
}
});

View File

@@ -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);
}
}

View File

@@ -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 });
}
}

View File

@@ -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",

View File

@@ -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",

View File

@@ -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: [],

View File

@@ -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"
},

View File

@@ -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 {

View File

@@ -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",

View File

@@ -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,
});

View File

@@ -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();
},
};

View File

@@ -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,

View File

@@ -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,

View File

@@ -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;
}

View File

@@ -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),

View File

@@ -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),

View File

@@ -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),
});

View File

@@ -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;
}
}

View File

@@ -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" } };

Some files were not shown because too many files have changed in this diff Show More