Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Refactor logging implementation for error handling and data redaction

Bill Yangb7dce3014805
diff --git a/server/src/api/analytics/generateCustomQuery.ts b/server/src/api/analytics/generateCustomQuery.ts
index 9cf953fd5..6efd9710b 100644
--- a/server/src/api/analytics/generateCustomQuery.ts
+++ b/server/src/api/analytics/generateCustomQuery.ts
@@ -7,7 +7,6 @@ import { EVENT_SCHEMA } from "./utils/eventSchema.js";
const OPENROUTER_TEMPERATURE = 0.1;
const OPENROUTER_MAX_TOKENS = 5000;
-const LOG_PREVIEW_LENGTH = 500;
const generationMessageSchema = z.object({
role: z.enum(["user", "assistant"]),
@@ -21,7 +20,6 @@ const requestBodySchema = z.object({
history: z.array(generationMessageSchema).max(12).optional().default([]),
});
-
function extractSql(content: string) {
const trimmed = content.trim();
const fencedSql = trimmed.match(/```(?:sql)?\s*([\s\S]*?)```/i);
@@ -29,11 +27,6 @@ function extractSql(content: string) {
return normalizeCustomQuery(sql.replace(/^sql\s*:/i, ""));
}
-function truncateForLog(value: string | undefined, maxLength = LOG_PREVIEW_LENGTH) {
- if (!value) return undefined;
- return value.length > maxLength ? `${value.slice(0, maxLength)}...` : value;
-}
-
function isAbortError(error: unknown) {
if (!(error instanceof Error)) return false;
return error.name === "AbortError" || error.message === "This operation was aborted";
@@ -139,9 +132,7 @@ ${body.data.prompt}
currentSiteId: body.data.currentSiteId,
accessibleSiteCount: siteIds.length,
promptLength: body.data.prompt.length,
- promptPreview: truncateForLog(body.data.prompt),
currentQueryLength: currentQuery?.length ?? 0,
- currentQueryPreview: truncateForLog(currentQuery),
historyCount: body.data.history.length,
historyRoles: body.data.history.map(message => message.role),
historyContentLengths: body.data.history.map(message => message.content.length),
@@ -167,7 +158,6 @@ ${body.data.prompt}
...generationContext,
openRouter,
generatedLength: generated.length,
- generatedPreview: truncateForLog(generated),
},
"OpenRouter returned custom analytics query candidate"
);
@@ -181,7 +171,6 @@ ${body.data.prompt}
openRouter,
validationError,
queryLength: query.length,
- queryPreview: truncateForLog(query),
},
"Generated custom query failed validation"
);
@@ -193,7 +182,6 @@ ${body.data.prompt}
...generationContext,
openRouter,
queryLength: query.length,
- queryPreview: truncateForLog(query),
},
"Generated custom analytics query passed validation"
);
diff --git a/server/src/api/as/webhook.test.ts b/server/src/api/as/webhook.test.ts
index af318236a..fbae02c5e 100644
--- a/server/src/api/as/webhook.test.ts
+++ b/server/src/api/as/webhook.test.ts
@@ -123,7 +123,7 @@ describe("handleAppSumoWebhook β€” payload validation", () => {
expect(reply.payload).toEqual({
event: "refund",
success: false,
- error: "Invalid event type: refund",
+ error: "Invalid AppSumo event type",
});
expect(mocks.execute).not.toHaveBeenCalled();
});
@@ -153,6 +153,29 @@ describe("handleAppSumoWebhook β€” audit trail", () => {
error: "connection refused",
});
});
+
+ it("does not send license credentials or the raw payload to the request logger", async () => {
+ const secrets = ["lic_current_secret", "lic_previous_secret", "lic_parent_secret"];
+
+ await invoke({
+ event: "deactivate",
+ license_key: secrets[0],
+ prev_license_key: secrets[1],
+ parent_license_key: secrets[2],
+ extra: { reason: "Customer request" },
+ });
+
+ const serializedLogCalls = JSON.stringify([
+ ...mocks.loggerInfo.mock.calls,
+ ...mocks.loggerWarn.mock.calls,
+ ...mocks.loggerError.mock.calls,
+ ...requestLogger.debug.mock.calls,
+ ]);
+ for (const secret of secrets) {
+ expect(serializedLogCalls).not.toContain(secret);
+ }
+ expect(serializedLogCalls).not.toContain('"payload"');
+ });
});
describe("handleAppSumoWebhook β€” purchase", () => {
diff --git a/server/src/api/as/webhook.ts b/server/src/api/as/webhook.ts
index ea448059b..9391ec48d 100644
--- a/server/src/api/as/webhook.ts
+++ b/server/src/api/as/webhook.ts
@@ -22,6 +22,18 @@ interface AppSumoWebhookPayload {
unit_quantity?: number;
}
+const APPSUMO_EVENT_TYPES = ["purchase", "activate", "upgrade", "downgrade", "deactivate", "migrate", "test"] as const;
+
+function eventTypeForLog(event: unknown): string {
+ return typeof event === "string" && APPSUMO_EVENT_TYPES.includes(event as (typeof APPSUMO_EVENT_TYPES)[number])
+ ? event
+ : "invalid";
+}
+
+function tierForLog(tier: unknown): string | undefined {
+ return typeof tier === "string" || typeof tier === "number" ? String(tier) : undefined;
+}
+
/**
* Check if AppSumo integration is enabled
*/
@@ -41,9 +53,8 @@ function validateWebhookPayload(payload: AppSumoWebhookPayload): boolean {
throw new Error("Missing event in webhook payload");
}
- const validEvents = ["purchase", "activate", "upgrade", "downgrade", "deactivate", "migrate", "test"];
- if (!validEvents.includes(payload.event)) {
- throw new Error(`Invalid event type: ${payload.event}`);
+ if (!APPSUMO_EVENT_TYPES.includes(payload.event as (typeof APPSUMO_EVENT_TYPES)[number])) {
+ throw new Error("Invalid AppSumo event type");
}
return true;
@@ -58,18 +69,27 @@ export async function handleAppSumoWebhook(
const requestLogger = request.log.child({ integration: "appsumo" });
if (!isAppSumoEnabled()) {
- requestLogger.info("[AppSumo] Integration not enabled");
+ requestLogger.info("Integration not enabled");
return reply.status(503).send({
error: "AppSumo integration is not available",
});
}
const payload = request.body;
- requestLogger.info({ payload }, "[AppSumo] Received webhook");
+ requestLogger.info(
+ {
+ eventType: eventTypeForLog(payload.event),
+ tier: tierForLog(payload.tier),
+ isTest: payload.test === true || payload.event === "test",
+ hasPreviousLicense: Boolean(payload.prev_license_key),
+ hasParentLicense: Boolean(payload.parent_license_key),
+ },
+ "Received AppSumo webhook"
+ );
// Handle test webhook for AppSumo validation
if (payload.test === true || payload.event === "test") {
- requestLogger.info("[AppSumo] Test webhook received");
+ requestLogger.info("Test webhook received");
return reply.status(200).send({
event: "test",
success: true,
@@ -83,7 +103,7 @@ export async function handleAppSumoWebhook(
const { license_key, event, tier, parent_license_key, prev_license_key, license_status, event_timestamp, extra } =
payload;
- requestLogger.info(`[AppSumo] Processing ${event} event for license ${license_key}`);
+ requestLogger.info({ eventType: event, tier: tierForLog(tier) }, "Processing webhook event");
// Log webhook event for audit trail
await db.execute(sql`
@@ -107,14 +127,14 @@ export async function handleAppSumoWebhook(
case "purchase":
// License purchased - create placeholder record
// Note: license_status will be "inactive" until user activates
- requestLogger.info(`[AppSumo] Handling purchase event for license ${license_key}, tier ${tier}`);
+ requestLogger.info({ eventType: event, tier: tierForLog(tier) }, "Handling webhook event");
await handlePurchaseEvent(requestLogger, license_key, tier, parent_license_key);
break;
case "activate":
// License activated by user
// Note: license_status is "inactive" in webhook, becomes active after our 200 response
- requestLogger.info(`[AppSumo] Handling activate event for license ${license_key}, tier ${tier}`);
+ requestLogger.info({ eventType: event, tier: tierForLog(tier) }, "Handling webhook event");
await handleActivateEvent(requestLogger, license_key, tier);
break;
@@ -122,7 +142,10 @@ export async function handleAppSumoWebhook(
// License upgraded to higher tier
// Note: Creates NEW license_key with prev_license_key pointing to old one
// AppSumo sends simultaneous deactivate event for old license (we skip it)
- requestLogger.info(`[AppSumo] Handling upgrade event: ${prev_license_key} -> ${license_key}, tier ${tier}`);
+ requestLogger.info(
+ { eventType: event, tier: tierForLog(tier), hasPreviousLicense: Boolean(prev_license_key) },
+ "Handling webhook event"
+ );
await handleUpgradeEvent(requestLogger, license_key, tier, prev_license_key);
break;
@@ -130,7 +153,10 @@ export async function handleAppSumoWebhook(
// License downgraded to lower tier
// Note: Creates NEW license_key with prev_license_key pointing to old one
// AppSumo sends simultaneous deactivate event for old license (we skip it)
- requestLogger.info(`[AppSumo] Handling downgrade event: ${prev_license_key} -> ${license_key}, tier ${tier}`);
+ requestLogger.info(
+ { eventType: event, tier: tierForLog(tier), hasPreviousLicense: Boolean(prev_license_key) },
+ "Handling webhook event"
+ );
await handleDowngradeEvent(requestLogger, license_key, tier, prev_license_key);
break;
@@ -141,7 +167,12 @@ export async function handleAppSumoWebhook(
const isUpgradeOrDowngradeDeactivation =
extra?.reason === "Upgraded by customer" || extra?.reason === "Downgraded by customer";
requestLogger.info(
- `[AppSumo] Deactivate event for ${license_key}, reason: ${extra?.reason}, skipping: ${isUpgradeOrDowngradeDeactivation}`
+ {
+ eventType: event,
+ deactivationCategory: isUpgradeOrDowngradeDeactivation ? "plan_change" : "other",
+ skipped: isUpgradeOrDowngradeDeactivation,
+ },
+ "Handling webhook event"
);
if (!isUpgradeOrDowngradeDeactivation) {
await handleDeactivateEvent(requestLogger, license_key);
@@ -151,22 +182,25 @@ export async function handleAppSumoWebhook(
case "migrate":
// Add-on migration when parent license is upgraded/downgraded
// Note: parent_license_key is updated to point to new parent license
- requestLogger.info(`[AppSumo] Handling migrate event for ${license_key}, parent: ${parent_license_key}`);
+ requestLogger.info(
+ { eventType: event, tier: tierForLog(tier), hasParentLicense: Boolean(parent_license_key) },
+ "Handling webhook event"
+ );
await handleMigrateEvent(requestLogger, license_key, tier, parent_license_key);
break;
default:
- requestLogger.warn({ event }, "[AppSumo] Unknown AppSumo webhook event");
+ requestLogger.warn({ eventType: event }, "Unknown AppSumo webhook event");
}
// Return success response as required by AppSumo
- requestLogger.info(`[AppSumo] Successfully processed ${event} event for ${license_key}`);
+ requestLogger.info({ eventType: event }, "Successfully processed webhook event");
return reply.status(200).send({
event: event,
success: true,
});
} catch (error) {
- requestLogger.error({ err: error }, "[AppSumo] Error processing AppSumo webhook");
+ requestLogger.error({ err: error, eventType: eventTypeForLog(payload.event) }, "Error processing AppSumo webhook");
// Still return 200 to acknowledge receipt, but log the error
return reply.status(200).send({
@@ -187,16 +221,14 @@ async function handlePurchaseEvent(
parentLicenseKey?: string
) {
const tierValue = tier?.toString() || "1";
- requestLogger.info(
- `[AppSumo] handlePurchaseEvent - license: ${licenseKey}, tier: ${tierValue}, parent: ${parentLicenseKey}`
- );
+ requestLogger.info({ tier: tierValue, hasParentLicense: Boolean(parentLicenseKey) }, "Handling purchase event");
// Check if license already exists
const existing = await db.execute(sql`SELECT id FROM appsumo.licenses WHERE license_key = ${licenseKey} LIMIT 1`);
if (Array.isArray(existing) && existing.length === 0) {
// Create placeholder - will be linked to org when user activates
- requestLogger.info(`[AppSumo] Creating new pending license ${licenseKey}`);
+ requestLogger.info({ tier: tierValue }, "Creating pending license");
await db.execute(sql`
INSERT INTO appsumo.licenses (
organization_id,
@@ -217,9 +249,9 @@ async function handlePurchaseEvent(
)
ON CONFLICT (license_key) DO NOTHING
`);
- requestLogger.info(`[AppSumo] Successfully created pending license ${licenseKey}`);
+ requestLogger.info({ tier: tierValue }, "Successfully created pending license");
} else {
- requestLogger.info(`[AppSumo] License ${licenseKey} already exists, skipping`);
+ requestLogger.info("License already exists; skipping pending license creation");
}
}
@@ -228,7 +260,7 @@ async function handlePurchaseEvent(
*/
async function handleActivateEvent(requestLogger: FastifyBaseLogger, licenseKey: string, tier: any) {
const tierValue = tier?.toString() || "1";
- requestLogger.info(`[AppSumo] handleActivateEvent - license: ${licenseKey}, tier: ${tierValue}`);
+ requestLogger.info({ tier: tierValue }, "Handling activate event");
await db.execute(sql`
UPDATE appsumo.licenses
@@ -239,7 +271,7 @@ async function handleActivateEvent(requestLogger: FastifyBaseLogger, licenseKey:
updated_at = NOW()
WHERE license_key = ${licenseKey}
`);
- requestLogger.info(`[AppSumo] Successfully activated license ${licenseKey}`);
+ requestLogger.info({ tier: tierValue }, "Successfully activated license");
}
/**
@@ -252,47 +284,46 @@ async function handleUpgradeEvent(
prevLicenseKey?: string
) {
const tierValue = tier?.toString() || "1";
- requestLogger.info(
- `[AppSumo] handleUpgradeEvent - new license: ${licenseKey}, prev license: ${prevLicenseKey}, tier: ${tierValue}`
- );
+ requestLogger.info({ tier: tierValue, hasPreviousLicense: Boolean(prevLicenseKey) }, "Handling upgrade event");
if (!prevLicenseKey) {
- requestLogger.warn("[AppSumo] No prev_license_key provided for upgrade event");
+ requestLogger.warn("No previous license provided for upgrade event");
return;
}
// Get the old license to find the organization
- requestLogger.info(`[AppSumo] Querying old license: ${prevLicenseKey}`);
+ requestLogger.debug("Looking up previous license for upgrade");
let oldLicenseResult = await db.execute(
sql`SELECT organization_id FROM appsumo.licenses WHERE license_key = ${prevLicenseKey} LIMIT 1`
);
- requestLogger.info({ oldLicenseResult }, "[AppSumo] Old license query result");
+ requestLogger.debug(
+ { previousLicenseMatchCount: Array.isArray(oldLicenseResult) ? oldLicenseResult.length : 0 },
+ "Previous license lookup completed"
+ );
// If previous license not found, try to find ANY license with an organization (fallback for missed webhooks)
if (!Array.isArray(oldLicenseResult) || oldLicenseResult.length === 0) {
- requestLogger.warn(
- `[AppSumo] Old license not found: ${prevLicenseKey}, searching for any license with organization as fallback`
- );
+ requestLogger.warn("Previous license not found; searching for an organization fallback");
oldLicenseResult = await db.execute(
sql`SELECT organization_id FROM appsumo.licenses WHERE organization_id IS NOT NULL ORDER BY updated_at DESC LIMIT 1`
);
if (!Array.isArray(oldLicenseResult) || oldLicenseResult.length === 0) {
- requestLogger.error("[AppSumo] No licenses with organization found, cannot process upgrade");
+ requestLogger.error("No licenses with an organization found; cannot process upgrade");
return;
}
- requestLogger.info({ oldLicenseResult }, "[AppSumo] Found fallback license");
+ requestLogger.info("Found fallback license for upgrade");
}
const oldLicense = oldLicenseResult[0] as any;
const organizationId = oldLicense.organization_id;
- requestLogger.info(`[AppSumo] Found old license with organization_id: ${organizationId}`);
+ requestLogger.info({ organizationId }, "Found organization for previous license");
// Create new license with the organization transferred
if (organizationId) {
// Organization exists - create active license
- requestLogger.info(`[AppSumo] Organization exists - creating active license for ${licenseKey}`);
+ requestLogger.info({ organizationId, tier: tierValue }, "Creating active license for organization");
try {
await db.execute(sql`
INSERT INTO appsumo.licenses (
@@ -319,14 +350,14 @@ async function handleUpgradeEvent(
activated_at = NOW(),
updated_at = NOW()
`);
- requestLogger.info(`[AppSumo] Successfully created/updated active license ${licenseKey}`);
+ requestLogger.info({ organizationId, tier: tierValue }, "Successfully created or updated active license");
} catch (error) {
- requestLogger.error({ err: error }, "[AppSumo] Error creating active license");
+ requestLogger.error({ err: error, organizationId }, "Error creating active license");
throw error;
}
} else {
// No organization yet - create pending license
- requestLogger.info(`[AppSumo] No organization - creating pending license for ${licenseKey}`);
+ requestLogger.info({ tier: tierValue }, "Creating pending license without an organization");
try {
await db.execute(sql`
INSERT INTO appsumo.licenses (
@@ -349,15 +380,15 @@ async function handleUpgradeEvent(
status = 'pending',
updated_at = NOW()
`);
- requestLogger.info(`[AppSumo] Successfully created/updated pending license ${licenseKey}`);
+ requestLogger.info({ tier: tierValue }, "Successfully created or updated pending license");
} catch (error) {
- requestLogger.error({ err: error }, "[AppSumo] Error creating pending license");
+ requestLogger.error({ err: error }, "Error creating pending license");
throw error;
}
}
// Deactivate the old license
- requestLogger.info(`[AppSumo] Deactivating old license ${prevLicenseKey}`);
+ requestLogger.info("Deactivating previous license");
try {
await db.execute(sql`
UPDATE appsumo.licenses
@@ -367,9 +398,9 @@ async function handleUpgradeEvent(
updated_at = NOW()
WHERE license_key = ${prevLicenseKey}
`);
- requestLogger.info(`[AppSumo] Successfully deactivated old license ${prevLicenseKey}`);
+ requestLogger.info("Successfully deactivated previous license");
} catch (error) {
- requestLogger.error({ err: error }, "[AppSumo] Error deactivating old license");
+ requestLogger.error({ err: error }, "Error deactivating previous license");
throw error;
}
}
@@ -384,47 +415,46 @@ async function handleDowngradeEvent(
prevLicenseKey?: string
) {
const tierValue = tier?.toString() || "1";
- requestLogger.info(
- `[AppSumo] handleDowngradeEvent - new license: ${licenseKey}, prev license: ${prevLicenseKey}, tier: ${tierValue}`
- );
+ requestLogger.info({ tier: tierValue, hasPreviousLicense: Boolean(prevLicenseKey) }, "Handling downgrade event");
if (!prevLicenseKey) {
- requestLogger.warn("[AppSumo] No prev_license_key provided for downgrade event");
+ requestLogger.warn("No previous license provided for downgrade event");
return;
}
// Get the old license to find the organization
- requestLogger.info(`[AppSumo] Querying old license: ${prevLicenseKey}`);
+ requestLogger.debug("Looking up previous license for downgrade");
let oldLicenseResult = await db.execute(
sql`SELECT organization_id FROM appsumo.licenses WHERE license_key = ${prevLicenseKey} LIMIT 1`
);
- requestLogger.info({ oldLicenseResult }, "[AppSumo] Old license query result");
+ requestLogger.debug(
+ { previousLicenseMatchCount: Array.isArray(oldLicenseResult) ? oldLicenseResult.length : 0 },
+ "Previous license lookup completed"
+ );
// If previous license not found, try to find ANY license with an organization (fallback for missed webhooks)
if (!Array.isArray(oldLicenseResult) || oldLicenseResult.length === 0) {
- requestLogger.warn(
- `[AppSumo] Old license not found: ${prevLicenseKey}, searching for any license with organization as fallback`
- );
+ requestLogger.warn("Previous license not found; searching for an organization fallback");
oldLicenseResult = await db.execute(
sql`SELECT organization_id FROM appsumo.licenses WHERE organization_id IS NOT NULL ORDER BY updated_at DESC LIMIT 1`
);
if (!Array.isArray(oldLicenseResult) || oldLicenseResult.length === 0) {
- requestLogger.error("[AppSumo] No licenses with organization found, cannot process downgrade");
+ requestLogger.error("No licenses with an organization found; cannot process downgrade");
return;
}
- requestLogger.info({ oldLicenseResult }, "[AppSumo] Found fallback license");
+ requestLogger.info("Found fallback license for downgrade");
}
const oldLicense = oldLicenseResult[0] as any;
const organizationId = oldLicense.organization_id;
- requestLogger.info(`[AppSumo] Found old license with organization_id: ${organizationId}`);
+ requestLogger.info({ organizationId }, "Found organization for previous license");
// Create new license with the organization transferred
if (organizationId) {
// Organization exists - create active license
- requestLogger.info(`[AppSumo] Organization exists - creating active license for ${licenseKey}`);
+ requestLogger.info({ organizationId, tier: tierValue }, "Creating active license for organization");
try {
await db.execute(sql`
INSERT INTO appsumo.licenses (
@@ -451,14 +481,14 @@ async function handleDowngradeEvent(
activated_at = NOW(),
updated_at = NOW()
`);
- requestLogger.info(`[AppSumo] Successfully created/updated active license ${licenseKey}`);
+ requestLogger.info({ organizationId, tier: tierValue }, "Successfully created or updated active license");
} catch (error) {
- requestLogger.error({ err: error }, "[AppSumo] Error creating active license");
+ requestLogger.error({ err: error, organizationId }, "Error creating active license");
throw error;
}
} else {
// No organization yet - create pending license
- requestLogger.info(`[AppSumo] No organization - creating pending license for ${licenseKey}`);
+ requestLogger.info({ tier: tierValue }, "Creating pending license without an organization");
try {
await db.execute(sql`
INSERT INTO appsumo.licenses (
@@ -481,15 +511,15 @@ async function handleDowngradeEvent(
status = 'pending',
updated_at = NOW()
`);
- requestLogger.info(`[AppSumo] Successfully created/updated pending license ${licenseKey}`);
+ requestLogger.info({ tier: tierValue }, "Successfully created or updated pending license");
} catch (error) {
- requestLogger.error({ err: error }, "[AppSumo] Error creating pending license");
+ requestLogger.error({ err: error }, "Error creating pending license");
throw error;
}
}
// Deactivate the old license
- requestLogger.info(`[AppSumo] Deactivating old license ${prevLicenseKey}`);
+ requestLogger.info("Deactivating previous license");
try {
await db.execute(sql`
UPDATE appsumo.licenses
@@ -499,9 +529,9 @@ async function handleDowngradeEvent(
updated_at = NOW()
WHERE license_key = ${prevLicenseKey}
`);
- requestLogger.info(`[AppSumo] Successfully deactivated old license ${prevLicenseKey}`);
+ requestLogger.info("Successfully deactivated previous license");
} catch (error) {
- requestLogger.error({ err: error }, "[AppSumo] Error deactivating old license");
+ requestLogger.error({ err: error }, "Error deactivating previous license");
throw error;
}
}
@@ -510,7 +540,7 @@ async function handleDowngradeEvent(
* Handle deactivate event - mark license as inactive
*/
async function handleDeactivateEvent(requestLogger: FastifyBaseLogger, licenseKey: string) {
- requestLogger.info(`[AppSumo] handleDeactivateEvent - license: ${licenseKey}`);
+ requestLogger.info("Handling deactivate event");
await db.execute(sql`
UPDATE appsumo.licenses
SET
@@ -519,7 +549,7 @@ async function handleDeactivateEvent(requestLogger: FastifyBaseLogger, licenseKe
updated_at = NOW()
WHERE license_key = ${licenseKey}
`);
- requestLogger.info(`[AppSumo] Successfully deactivated license ${licenseKey}`);
+ requestLogger.info("Successfully deactivated license");
}
/**
@@ -532,9 +562,7 @@ async function handleMigrateEvent(
parentLicenseKey?: string
) {
const tierValue = tier?.toString() || "1";
- requestLogger.info(
- `[AppSumo] handleMigrateEvent - license: ${licenseKey}, tier: ${tierValue}, parent: ${parentLicenseKey}`
- );
+ requestLogger.info({ tier: tierValue, hasParentLicense: Boolean(parentLicenseKey) }, "Handling migrate event");
await db.execute(sql`
UPDATE appsumo.licenses
@@ -544,5 +572,5 @@ async function handleMigrateEvent(
updated_at = NOW()
WHERE license_key = ${licenseKey}
`);
- requestLogger.info(`[AppSumo] Successfully migrated license ${licenseKey}`);
+ requestLogger.info({ tier: tierValue }, "Successfully migrated license");
}
diff --git a/server/src/api/gsc/getData.ts b/server/src/api/gsc/getData.ts
index d505c2376..14d8af394 100644
--- a/server/src/api/gsc/getData.ts
+++ b/server/src/api/gsc/getData.ts
@@ -61,7 +61,7 @@ export async function getGSCData(req: FastifyRequest<GetGSCDataRequest>, res: Fa
if (!gscResponse.ok) {
const errorText = await gscResponse.text();
- req.log.error({ responseBody: errorText, statusCode: gscResponse.status }, "GSC request failed");
+ req.log.error({ responseBodyLength: errorText.length, statusCode: gscResponse.status }, "GSC request failed");
return res.status(gscResponse.status).send({ error: "Failed to fetch GSC data", details: errorText });
}
diff --git a/server/src/lib/auth.ts b/server/src/lib/auth.ts
index f447033f0..de4229da7 100644
--- a/server/src/lib/auth.ts
+++ b/server/src/lib/auth.ts
@@ -176,7 +176,7 @@ const pluginList = [
.delete(invitation)
.where(and(eq(invitation.email, removedUser.email), eq(invitation.organizationId, org.id)));
} catch (error) {
- console.error("Error deleting invitations for removed member:", error);
+ authLogger.error({ err: error, organizationId: org.id }, "Error deleting invitations for removed member");
}
invalidateSitesAccessCache(removedMember.userId);
},
@@ -320,7 +320,7 @@ export const auth = betterAuth({
try {
await db.delete(schema.apiKey).where(eq(schema.apiKey.referenceId, deletedUser.id));
} catch (error) {
- console.error("Error deleting API keys for removed user:", error);
+ authLogger.error({ err: error, userId: deletedUser.id }, "Error deleting API keys for removed user");
}
},
},
@@ -353,7 +353,7 @@ export const auth = betterAuth({
user: {
create: {
after: async u => {
- console.log(u);
+ authLogger.info({ userId: u.id }, "User created");
const users = await db.select().from(schema.user).orderBy(asc(user.createdAt));
// If this is the first user, make them an admin
@@ -373,7 +373,7 @@ export const auth = betterAuth({
await db.update(user).set({ scheduledTipEmailIds: emailIds }).where(eq(user.id, u.id));
}
} catch (error) {
- console.error("Error setting up onboarding emails:", error);
+ authLogger.error({ err: error, userId: u.id }, "Error setting up onboarding emails");
}
},
},
@@ -548,7 +548,7 @@ export const auth = betterAuth({
invalidateSitesAccessCache(userRecord[0].id);
} catch (error) {
- console.error("Error applying invitation site restrictions:", error);
+ authLogger.error({ err: error }, "Error applying invitation Site restrictions");
}
}
@@ -572,7 +572,7 @@ export const auth = betterAuth({
invalidateSitesAccessCache(userId);
}
} catch (error) {
- console.error("Error cleaning up after organization leave:", error);
+ authLogger.error({ err: error }, "Error cleaning up after organization leave");
}
}
}),
diff --git a/server/src/lib/logger/LOGGING.md b/server/src/lib/logger/LOGGING.md
index 880f69bbf..71c104db2 100644
--- a/server/src/lib/logger/LOGGING.md
+++ b/server/src/lib/logger/LOGGING.md
@@ -23,7 +23,7 @@ export async function getUsers(request: FastifyRequest, reply: FastifyReply) {
request.log.debug({ userCount: users.length }, "Users fetched");
return users;
} catch (error) {
- request.log.error(error, "Failed to fetch users");
+ request.log.error({ err: error }, "Failed to fetch users");
throw error;
}
}
@@ -68,18 +68,25 @@ Prefer stable structured fields over interpolation:
logger.info({ monitorId, region }, "Running monitor check");
```
-For exceptions, pass the error directly or use Pino's `err` key:
+For exceptions, use Pino's canonical `err` key:
```typescript
-logger.error(error, "Monitor check failed");
logger.error({ err: error, monitorId }, "Monitor check failed");
```
-Do not use `{ error }`: Pino does not apply its Error serializer to that key, so native errors become empty objects.
+The runtime also normalizes legacy direct `Error` arguments and `{ error }` objects to `err`, so stack and message data survive. If code throws a non-`Error` value, the runtime emits a safe `NonErrorThrown` error with only the thrown value's type; it does not serialize the unknown value.
+
+Keep changing call sites on the canonical form above. This gives events stable fields and makes the intended exception explicit during review.
## Redaction
-The shared runtime redacts common credentials, authentication headers, cookies, and structured AppSumo license fields. Redaction only applies to structured fields at configured paths; it cannot remove sensitive values interpolated into message strings.
+The shared runtime recursively censors sensitive structured fields before Pino serializes an event. The policy covers credentials, authentication headers, cookies, license keys, webhook URLs, email addresses, phone numbers, request/response bodies, prompts, and query/SQL content. Pino path redaction remains enabled as a second layer for child bindings and serializer output.
+
+Redaction cannot reliably identify a sensitive value interpolated into a message string. Keep messages constant and put operational metadata in structured fields:
+
+```typescript
+logger.info({ organizationId, eventType }, "Processed integration event");
+```
Never log passwords, tokens, cookies, authentication headers, raw credentials, or full user records. Keep email addresses, phone numbers, prompts, queries, and request payloads out of logs unless the operational need and retention policy are explicit.
diff --git a/server/src/lib/logger/logger.test.ts b/server/src/lib/logger/logger.test.ts
index d72c7a03c..6191f5815 100644
--- a/server/src/lib/logger/logger.test.ts
+++ b/server/src/lib/logger/logger.test.ts
@@ -1,4 +1,4 @@
-import type { LoggerOptions } from "pino";
+import type { LogFn, LoggerOptions } from "pino";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const { childLogger, childMock, pinoMock, rootLogger } = vi.hoisted(() => {
@@ -29,6 +29,18 @@ async function importLogger(nodeEnv: string, logLevel = "") {
return { loggerModule, options };
}
+function applyLogMethodHook(options: LoggerOptions, inputArgs: unknown[]): unknown[] {
+ let normalizedArgs: unknown[] | undefined;
+ const method = function (...args: unknown[]) {
+ normalizedArgs = args;
+ } as LogFn;
+
+ options.hooks?.logMethod?.call(rootLogger as never, inputArgs as Parameters<LogFn>, method, 50);
+
+ if (!normalizedArgs) throw new Error("Logging runtime did not invoke the Pino log method");
+ return normalizedArgs;
+}
+
beforeEach(() => {
process.env.LOG_LEVEL = "";
});
@@ -103,4 +115,103 @@ describe("logging runtime", () => {
});
expect(options.serializers?.res({ statusCode: 204 })).toEqual({ statusCode: 204 });
});
+
+ it("normalizes direct and structured errors to the canonical err field", async () => {
+ const { options } = await importLogger("production");
+ const failure = Object.assign(new Error("database unavailable"), {
+ code: "ECONNREFUSED",
+ details: { requestId: "provider-request", responseBodyPreview: "private provider response" },
+ });
+
+ const [directContext] = applyLogMethodHook(options, [failure, "Direct failure"]);
+ const [structuredContext] = applyLogMethodHook(options, [{ error: failure, siteId: 42 }, "Structured failure"]);
+
+ expect(directContext).toMatchObject({
+ err: expect.objectContaining({
+ message: "database unavailable",
+ code: "ECONNREFUSED",
+ details: { requestId: "provider-request", responseBodyPreview: "[REDACTED]" },
+ }),
+ });
+ expect(structuredContext).toMatchObject({
+ err: expect.objectContaining({ message: "database unavailable", code: "ECONNREFUSED" }),
+ siteId: 42,
+ });
+ expect(structuredContext).not.toHaveProperty("error");
+ });
+
+ it("turns non-Error failures into safe errors without logging the thrown value", async () => {
+ const { options } = await importLogger("production");
+ const [context] = applyLogMethodHook(options, [
+ { err: { token: "secret", reason: "provider failed" } },
+ "Provider failure",
+ ]);
+
+ expect(context).toMatchObject({
+ err: expect.objectContaining({
+ name: "NonErrorThrown",
+ message: "A non-Error value was thrown",
+ thrownType: "object",
+ }),
+ });
+ expect(JSON.stringify(context)).not.toContain("secret");
+ expect(JSON.stringify(context)).not.toContain("provider failed");
+ });
+
+ it("recursively censors sensitive structured fields without mutating caller data", async () => {
+ const { options } = await importLogger("production");
+ const input = {
+ eventType: "activate",
+ integration: {
+ auth: { newPassword: "password-secret" },
+ contact: { recipientEmail: "[email protected]" },
+ },
+ promptPreview: "show revenue by customer",
+ };
+
+ const [context] = applyLogMethodHook(options, [input, "Safe event"]);
+
+ expect(context).toEqual({
+ eventType: "activate",
+ integration: {
+ auth: { newPassword: "[REDACTED]" },
+ contact: { recipientEmail: "[REDACTED]" },
+ },
+ promptPreview: "[REDACTED]",
+ });
+ expect(input.integration.auth.newPassword).toBe("password-secret");
+ });
+
+ it("emits a normalized, redacted JSON event through Pino", async () => {
+ const { options } = await importLogger("production");
+ const { pino: actualPino } = await vi.importActual<typeof import("pino")>("pino");
+ const output: string[] = [];
+ class ProviderError extends Error {
+ details = { requestId: "provider-request", responseBodyPreview: "private provider response" };
+ }
+
+ const outputLogger = actualPino(
+ { ...options, base: undefined, timestamp: false },
+ { write: (line: string) => output.push(line) }
+ );
+ outputLogger
+ .child({ email: "[email protected]" })
+ .error(
+ { error: new ProviderError("provider failed"), nested: { apiKey: "api-secret" } },
+ "Provider request failed"
+ );
+
+ expect(output).toHaveLength(1);
+ expect(JSON.parse(output[0])).toMatchObject({
+ level: 50,
+ email: "[REDACTED]",
+ nested: { apiKey: "[REDACTED]" },
+ err: {
+ type: "ProviderError",
+ message: "provider failed",
+ details: { requestId: "provider-request", responseBodyPreview: "[REDACTED]" },
+ },
+ msg: "Provider request failed",
+ });
+ });
});
diff --git a/server/src/lib/logger/logger.ts b/server/src/lib/logger/logger.ts
index 08706a8a0..54c1f16e1 100644
--- a/server/src/lib/logger/logger.ts
+++ b/server/src/lib/logger/logger.ts
@@ -1,7 +1,52 @@
import "dotenv/config";
import type { FastifyBaseLogger } from "fastify";
-import { pino, type LoggerOptions } from "pino";
+import { pino, type LogFn, type LoggerOptions } from "pino";
+
+const REDACTED_VALUE = "[REDACTED]";
+
+const SENSITIVE_LOG_FIELDS = new Set([
+ "authorization",
+ "cookie",
+ "cookies",
+ "setcookie",
+ "credentials",
+ "password",
+ "passphrase",
+ "token",
+ "accesstoken",
+ "refreshtoken",
+ "apikey",
+ "secret",
+ "clientsecret",
+ "licensekey",
+ "prevlicensekey",
+ "parentlicensekey",
+ "email",
+ "emails",
+ "emailaddress",
+ "phone",
+ "phonenumber",
+ "phonenumbers",
+ "webhookurl",
+ "slackwebhookurl",
+ "payload",
+ "body",
+ "requestbody",
+ "responsebody",
+ "responsebodypreview",
+ "responseerror",
+ "prompt",
+ "promptpreview",
+ "currentquery",
+ "currentquerypreview",
+ "generated",
+ "generatedpreview",
+ "query",
+ "querypreview",
+ "sql",
+ "sqlpreview",
+]);
const REDACTED_LOG_PATHS = [
"authorization",
@@ -27,8 +72,139 @@ const REDACTED_LOG_PATHS = [
"payload.license_key",
"payload.prev_license_key",
"payload.parent_license_key",
+ "email",
+ "emailAddress",
+ "phoneNumber",
+ "webhookUrl",
+ "slackWebhookUrl",
+ "payload",
+ "body",
+ "requestBody",
+ "responseBody",
+ "responseBodyPreview",
+ "responseError",
+ "prompt",
+ "promptPreview",
+ "currentQuery",
+ "currentQueryPreview",
+ "generated",
+ "generatedPreview",
+ "query",
+ "queryPreview",
+ "sql",
+ "sqlPreview",
+ "err.details.responseBodyPreview",
+ "err.details.responseError",
] as const;
+function normalizedFieldName(field: string): string {
+ return field.replace(/[^a-zA-Z0-9]/g, "").toLowerCase();
+}
+
+function isSensitiveLogField(field: string): boolean {
+ const normalized = normalizedFieldName(field);
+ if (SENSITIVE_LOG_FIELDS.has(normalized)) return true;
+
+ if (/(password|passphrase|token|apikey|secret|licensekey|authorization|cookie)/.test(normalized)) return true;
+ if (/(email|emails|emailaddress|phone|phonenumber|phonenumbers)$/.test(normalized)) return true;
+ return /(payload|body|prompt|query|sql|generated)(preview)?$/.test(normalized);
+}
+
+function isPlainObject(value: unknown): value is Record<string, unknown> {
+ if (value === null || typeof value !== "object") return false;
+ const prototype = Object.getPrototypeOf(value);
+ return prototype === Object.prototype || prototype === null;
+}
+
+class NonErrorThrown extends Error {
+ readonly thrownType: string;
+
+ constructor(value: unknown) {
+ super("A non-Error value was thrown");
+ Object.defineProperty(this, "name", { value: "NonErrorThrown", configurable: true });
+ this.thrownType = value === null ? "null" : typeof value;
+ }
+}
+
+function sanitizeError(error: Error, seen: WeakMap<object, unknown>): Error {
+ const sanitized = Object.create(Object.getPrototypeOf(error)) as Error;
+ seen.set(error, sanitized);
+ Object.defineProperties(sanitized, {
+ message: { value: error.message, configurable: true, writable: true },
+ name: { value: error.name, configurable: true, writable: true },
+ stack: { value: error.stack, configurable: true, writable: true },
+ });
+
+ for (const field of Object.keys(error)) {
+ const value = (error as unknown as Record<string, unknown>)[field];
+ (sanitized as unknown as Record<string, unknown>)[field] = sanitizeLogValue(value, field, seen);
+ }
+
+ const errorRecord = error as unknown as Record<string, unknown>;
+ for (const field of ["cause", "errors"]) {
+ if (field in errorRecord && !Object.prototype.propertyIsEnumerable.call(error, field)) {
+ (sanitized as unknown as Record<string, unknown>)[field] = sanitizeLogValue(errorRecord[field], field, seen);
+ }
+ }
+
+ return sanitized;
+}
+
+function sanitizeLogValue(value: unknown, field: string | undefined, seen: WeakMap<object, unknown>): unknown {
+ if (field && isSensitiveLogField(field)) return REDACTED_VALUE;
+ if (value === null || typeof value !== "object") return value;
+
+ const existing = seen.get(value);
+ if (existing) return existing;
+
+ if (value instanceof Error) return sanitizeError(value, seen);
+
+ if (Array.isArray(value)) {
+ const sanitized: unknown[] = [];
+ seen.set(value, sanitized);
+ value.forEach(item => sanitized.push(sanitizeLogValue(item, undefined, seen)));
+ return sanitized;
+ }
+
+ if (!isPlainObject(value)) return value;
+
+ const sanitized: Record<string, unknown> = {};
+ seen.set(value, sanitized);
+ for (const [nestedField, nestedValue] of Object.entries(value)) {
+ sanitized[nestedField] = sanitizeLogValue(nestedValue, nestedField, seen);
+ }
+ return sanitized;
+}
+
+function normalizeError(error: unknown): Error {
+ if (error instanceof Error) {
+ return sanitizeError(error, new WeakMap());
+ }
+
+ return new NonErrorThrown(error);
+}
+
+function normalizeLogArguments(inputArgs: Parameters<LogFn>): Parameters<LogFn> {
+ if (inputArgs.length === 0) return inputArgs;
+
+ const [context, ...remainingArgs] = inputArgs;
+ if (context instanceof Error) {
+ return [{ err: normalizeError(context) }, ...remainingArgs] as Parameters<LogFn>;
+ }
+
+ if (!isPlainObject(context)) return inputArgs;
+
+ const normalized = sanitizeLogValue(context, undefined, new WeakMap()) as Record<string, unknown>;
+ if ("err" in context) {
+ normalized.err = normalizeError(context.err);
+ } else if ("error" in context) {
+ normalized.err = normalizeError(context.error);
+ }
+ delete normalized.error;
+
+ return [normalized, ...remainingArgs] as Parameters<LogFn>;
+}
+
function defaultLogLevel(): string {
if (process.env.NODE_ENV === "production") return "info";
if (process.env.NODE_ENV === "test") return "silent";
@@ -41,7 +217,12 @@ function createLogger(name: string): FastifyBaseLogger {
level: process.env.LOG_LEVEL || defaultLogLevel(),
redact: {
paths: [...REDACTED_LOG_PATHS],
- censor: "[REDACTED]",
+ censor: REDACTED_VALUE,
+ },
+ hooks: {
+ logMethod(inputArgs, method) {
+ method.apply(this, normalizeLogArguments(inputArgs));
+ },
},
serializers: {
req(request) {
diff --git a/server/src/lib/twilio.ts b/server/src/lib/twilio.ts
index 92461cba0..5ea818ebe 100644
--- a/server/src/lib/twilio.ts
+++ b/server/src/lib/twilio.ts
@@ -29,10 +29,10 @@ export const sendSMS = async (phoneNumber: string, message: string): Promise<{ s
to: phoneNumber,
});
- logger.info({ phoneNumber, sid: result.sid }, "Successfully sent SMS");
+ logger.info({ messageId: result.sid }, "Successfully sent SMS");
return { success: true };
} catch (error) {
- logger.error({ phoneNumber, error }, "Failed to send SMS");
+ logger.error({ err: error }, "Failed to send SMS");
return {
success: false,
error: error instanceof Error ? error.message : "Failed to send SMS",
diff --git a/server/src/services/pdfReports/pdfReportService.ts b/server/src/services/pdfReports/pdfReportService.ts
index 47edcf768..c564bfb4c 100644
--- a/server/src/services/pdfReports/pdfReportService.ts
+++ b/server/src/services/pdfReports/pdfReportService.ts
@@ -162,7 +162,7 @@ class PdfReportService {
return await processResults<ChartDataPoint>(result);
} catch (error) {
- this.logger.error({ error, siteId }, "Error fetching chart data");
+ this.logger.error({ err: error, siteId }, "Error fetching chart data");
return [];
}
}
@@ -226,7 +226,7 @@ class PdfReportService {
const data = await processResults<OverviewData>(result);
return data[0] || null;
} catch (error) {
- this.logger.error({ error, siteId }, "Error fetching overview data");
+ this.logger.error({ err: error, siteId }, "Error fetching overview data");
return null;
}
}
@@ -454,7 +454,7 @@ class PdfReportService {
return await processResults<MetricData>(result);
} catch (error) {
- this.logger.error({ error, siteId, parameter }, "Error fetching top N data");
+ this.logger.error({ err: error, siteId, parameter }, "Error fetching top N data");
return [];
}
}
diff --git a/server/src/services/reengagement/reengagementService.ts b/server/src/services/reengagement/reengagementService.ts
index 99292f990..82a4d0b50 100644
--- a/server/src/services/reengagement/reengagementService.ts
+++ b/server/src/services/reengagement/reengagementService.ts
@@ -70,7 +70,7 @@ class ReengagementService {
// No sites have events - return the first site for the dashboard link
return { hasEvents: false, siteId: userSites[0].siteId, domain: userSites[0].domain };
} catch (error) {
- this.logger.error({ error, userId }, "Error checking user sites for events");
+ this.logger.error({ err: error, userId }, "Error checking user sites for events");
return { hasEvents: true, siteId: null, domain: null }; // On error, assume they have events (don't send email)
}
}
@@ -129,7 +129,7 @@ class ReengagementService {
break;
}
} catch (error) {
- this.logger.error({ error, targetDay }, "Error processing re-engagement emails");
+ this.logger.error({ err: error, targetDay }, "Error processing re-engagement emails");
}
}
diff --git a/server/src/services/tracker/botBlocking/anomalyScorer.ts b/server/src/services/tracker/botBlocking/anomalyScorer.ts
index e329530ff..8256ea4ba 100644
--- a/server/src/services/tracker/botBlocking/anomalyScorer.ts
+++ b/server/src/services/tracker/botBlocking/anomalyScorer.ts
@@ -369,7 +369,7 @@ export async function observeTrackingAnomaly(input: AnomalyInput): Promise<Anoma
// A Redis blip must never break ingestion. Fall back to the in-process
// counters β€” accuracy degrades under clustering, but detection keeps working
// and recovers automatically once Redis is back.
- logger.error(error as Error, "Redis anomaly counters failed; using in-process fallback");
+ logger.error({ err: error }, "Redis anomaly counters failed; using in-process fallback");
counters = observeViaLocal(plan, nowMs);
}
} else {
diff --git a/server/src/services/tracker/botBlocking/botEventQueue.ts b/server/src/services/tracker/botBlocking/botEventQueue.ts
index 207ce1618..4fc7a2ccf 100644
--- a/server/src/services/tracker/botBlocking/botEventQueue.ts
+++ b/server/src/services/tracker/botBlocking/botEventQueue.ts
@@ -85,7 +85,7 @@ class BotEventQueue {
format: "JSONEachRow",
});
} catch (error) {
- this.logger.error(error, "Error processing bot event queue");
+ this.logger.error({ err: error }, "Error processing bot event queue");
} finally {
this.processing = false;
}
diff --git a/server/src/services/tracker/identifyService.ts b/server/src/services/tracker/identifyService.ts
index a424a1467..46e000090 100644
--- a/server/src/services/tracker/identifyService.ts
+++ b/server/src/services/tracker/identifyService.ts
@@ -67,7 +67,7 @@ export async function backfillIdentifiedUserId(
}
logger.info({ siteId, anonymousId, userId }, "Backfilled identified_user_id in ClickHouse");
} catch (error) {
- logger.error({ siteId, anonymousId, userId, error }, "Error backfilling identified_user_id");
+ logger.error({ siteId, anonymousId, userId, err: error }, "Error backfilling identified_user_id");
}
}
@@ -112,7 +112,7 @@ export async function handleIdentify(request: FastifyRequest, reply: FastifyRepl
try {
await db.insert(userProfiles).values({ siteId, userId: user_id }).onConflictDoNothing();
} catch (error) {
- logger.error({ siteId, userId: user_id, error }, "Error creating user profile shell");
+ logger.error({ siteId, userId: user_id, err: error }, "Error creating user profile shell");
}
try {
@@ -141,7 +141,7 @@ export async function handleIdentify(request: FastifyRequest, reply: FastifyRepl
}
} catch (error) {
// Handle unique constraint violation gracefully (race condition)
- logger.debug({ siteId, anonymousId, userId: user_id, error }, "Alias may already exist");
+ logger.debug({ siteId, anonymousId, userId: user_id, err: error }, "Alias may already exist");
}
}
@@ -171,7 +171,7 @@ export async function handleIdentify(request: FastifyRequest, reply: FastifyRepl
},
});
} catch (error) {
- logger.error({ siteId, userId: user_id, error }, "Error updating user profile");
+ logger.error({ siteId, userId: user_id, err: error }, "Error updating user profile");
}
}
@@ -179,7 +179,7 @@ export async function handleIdentify(request: FastifyRequest, reply: FastifyRepl
success: true,
});
} catch (error) {
- logger.error(error, "Error handling identify");
+ logger.error({ err: error }, "Error handling identify");
return reply.status(500).send({
success: false,
error: "Failed to process identify",
diff --git a/server/src/services/tracker/pageviewQueue.ts b/server/src/services/tracker/pageviewQueue.ts
index 1b901d26c..1d9d71b12 100644
--- a/server/src/services/tracker/pageviewQueue.ts
+++ b/server/src/services/tracker/pageviewQueue.ts
@@ -124,7 +124,7 @@ class PageviewQueue {
format: "JSONEachRow",
});
} catch (error) {
- this.logger.error(error, "Error processing pageview queue");
+ this.logger.error({ err: error }, "Error processing pageview queue");
} finally {
this.processing = false;
}
diff --git a/server/src/services/tracker/trackEvent.ts b/server/src/services/tracker/trackEvent.ts
index 7d6559e95..e709a66ad 100644
--- a/server/src/services/tracker/trackEvent.ts
+++ b/server/src/services/tracker/trackEvent.ts
@@ -373,7 +373,7 @@ export async function trackEvent(request: FastifyRequest, reply: FastifyReply) {
success: true,
});
} catch (error) {
- logger.error(error, "Error tracking event");
+ logger.error({ err: error }, "Error tracking event");
if (error instanceof ZodError) {
return reply.status(400).send({
success: false,
diff --git a/server/src/services/uptime/monitorExecutor.ts b/server/src/services/uptime/monitorExecutor.ts
index 5e1c8c562..f1459bf21 100644
--- a/server/src/services/uptime/monitorExecutor.ts
+++ b/server/src/services/uptime/monitorExecutor.ts
@@ -64,7 +64,7 @@ export class MonitorExecutor {
}
async start(): Promise<void> {
- this.logger.info(`Starting BullMQ monitor executor with concurrency: ${this.concurrency}`);
+ this.logger.info({ concurrency: this.concurrency }, "Starting BullMQ monitor executor");
// Create worker to process jobs
this.worker = new Worker(
@@ -91,11 +91,11 @@ export class MonitorExecutor {
// });
this.worker.on("failed", (job, err) => {
- this.logger.error(err as Error, `Job ${job?.id} failed`);
+ this.logger.error({ err, jobId: job?.id }, "Monitor job failed");
});
this.worker.on("error", err => {
- this.logger.error(err as Error, "Worker error");
+ this.logger.error({ err }, "Monitor worker error");
});
this.worker.on("ready", () => {
@@ -122,7 +122,7 @@ export class MonitorExecutor {
const { monitorId } = jobData;
try {
- this.logger.debug(`πŸ” Starting to process monitor check for monitor ID: ${monitorId}`);
+ this.logger.debug({ monitorId }, "Processing monitor check");
// Fetch monitor configuration
const monitor = await db.query.uptimeMonitors.findFirst({
@@ -130,7 +130,7 @@ export class MonitorExecutor {
});
if (!monitor || !monitor.enabled) {
- this.logger.debug(`Monitor ${monitorId} not found or disabled`);
+ this.logger.debug({ monitorId }, "Monitor not found or disabled");
return;
}
@@ -193,9 +193,12 @@ export class MonitorExecutor {
// Update monitor status in PostgreSQL
await this.updateMonitorStatus(monitor.id, result);
- this.logger.info(`βœ… Monitor check completed: ${monitorId} - ${result.status} (${result.responseTimeMs}ms`);
+ this.logger.info(
+ { monitorId, monitorStatus: result.status, responseTimeMs: result.responseTimeMs },
+ "Monitor check completed"
+ );
} catch (error) {
- this.logger.error(error as Error, `Error processing monitor check ${monitorId}`);
+ this.logger.error({ err: error, monitorId }, "Error processing monitor check");
// Try to store the error event
try {
@@ -221,7 +224,7 @@ export class MonitorExecutor {
await this.updateMonitorStatus(monitor.id, errorResult);
}
} catch (innerError) {
- this.logger.error(innerError as Error, "Failed to store error event");
+ this.logger.error({ err: innerError, monitorId }, "Failed to store monitor error event");
}
}
}
@@ -232,7 +235,7 @@ export class MonitorExecutor {
const globalRegions = monitor.selectedRegions.filter((r: string) => r !== "local");
if (globalRegions.length === 0) {
- this.logger.debug(`Monitor ${monitor.id} has no global regions selected`);
+ this.logger.debug({ monitorId: monitor.id }, "Monitor has no global regions selected");
return;
}
@@ -246,14 +249,17 @@ export class MonitorExecutor {
});
if (regions.length === 0) {
- this.logger.warn(`No healthy regions found for monitor ${monitor.id}`);
+ this.logger.warn({ monitorId: monitor.id }, "No healthy regions found for monitor");
return;
}
// Execute checks in parallel across all regions
const regionPromises = regions.map(region =>
this.executeAgentCheck(monitor, region).catch(error => {
- this.logger.error(error as Error, `Error executing check in region ${region.code}`);
+ this.logger.error(
+ { err: error, monitorId: monitor.id, regionCode: region.code },
+ "Error executing regional monitor check"
+ );
return {
region: region.code,
result: {
@@ -307,10 +313,11 @@ export class MonitorExecutor {
await this.updateMonitorStatus(monitor.id, aggregatedResult);
this.logger.info(
- `βœ… Global monitor check completed: ${monitor.id} - ${overallStatus} (${regionResults.length} regions)`
+ { monitorId: monitor.id, monitorStatus: overallStatus, regionCount: regionResults.length },
+ "Global monitor check completed"
);
} catch (error) {
- this.logger.error(error as Error, `Error processing global monitor check ${monitor.id}`);
+ this.logger.error({ err: error, monitorId: monitor.id }, "Error processing global monitor check");
}
}
@@ -366,7 +373,10 @@ export class MonitorExecutor {
return { region: region.code, result };
} catch (error) {
- this.logger.error(error as Error, `Failed to execute check via agent ${region.code}`);
+ this.logger.error(
+ { err: error, monitorId: monitor.id, regionCode: region.code },
+ "Failed to execute monitor check through agent"
+ );
throw error;
}
}
@@ -413,7 +423,7 @@ export class MonitorExecutor {
format: "JSONEachRow",
});
} catch (error) {
- this.logger.error(error as Error, "Failed to store monitor event in ClickHouse");
+ this.logger.error({ err: error, monitorId: monitor.id, regionCode }, "Failed to store monitor event");
}
}
@@ -472,7 +482,7 @@ export class MonitorExecutor {
consecutiveSuccesses
);
} catch (error) {
- this.logger.error(error as Error, "Failed to update monitor status");
+ this.logger.error({ err: error, monitorId }, "Failed to update monitor status");
}
}
@@ -491,7 +501,7 @@ export class MonitorExecutor {
});
if (!monitor) {
- this.logger.error(`Monitor ${monitorId} not found for incident management`);
+ this.logger.error({ monitorId }, "Monitor not found for incident management");
return;
}
@@ -519,7 +529,7 @@ export class MonitorExecutor {
failureCount: 1,
})
.returning();
- this.logger.info(`Created new incident for monitor ${monitorId} (${monitor.name})`);
+ this.logger.info({ incidentId: newIncident.id, monitorId }, "Created monitor incident");
// Send notifications for new incident
await this.notificationService.sendIncidentNotifications(monitor, newIncident, "down");
@@ -535,7 +545,7 @@ export class MonitorExecutor {
resolvedAt: now,
})
.where(eq(uptimeIncidents.id, activeIncident.id));
- this.logger.info(`Resolved incident ${activeIncident.id} for monitor ${monitorId} (${monitor.name})`);
+ this.logger.info({ incidentId: activeIncident.id, monitorId }, "Resolved monitor incident");
// Send recovery notifications
await this.notificationService.sendIncidentNotifications(
@@ -556,7 +566,7 @@ export class MonitorExecutor {
.where(eq(uptimeIncidents.id, activeIncident.id));
}
} catch (error) {
- this.logger.error(error as Error, "Failed to manage incident");
+ this.logger.error({ err: error, monitorId }, "Failed to manage monitor incident");
}
}
@@ -633,7 +643,10 @@ export class MonitorExecutor {
failureCount: 1,
})
.returning();
- this.logger.info(`Created new incident for monitor ${monitor.id} (${monitor.name}) in region ${region}`);
+ this.logger.info(
+ { incidentId: newIncident.id, monitorId: monitor.id, regionCode: region },
+ "Created regional monitor incident"
+ );
// Send notifications for new regional incident
await this.notificationService.sendIncidentNotifications(monitor, newIncident, "down");
@@ -651,7 +664,8 @@ export class MonitorExecutor {
})
.where(eq(uptimeIncidents.id, activeIncident.id));
this.logger.info(
- `Resolved incident ${activeIncident.id} for monitor ${monitor.id} (${monitor.name}) in region ${region}`
+ { incidentId: activeIncident.id, monitorId: monitor.id, regionCode: region },
+ "Resolved regional monitor incident"
);
// Send recovery notifications for regional incident
@@ -673,7 +687,10 @@ export class MonitorExecutor {
.where(eq(uptimeIncidents.id, activeIncident.id));
}
} catch (error) {
- this.logger.error(error as Error, `Failed to manage regional incident for region ${region}`);
+ this.logger.error(
+ { err: error, monitorId: monitor.id, regionCode: region },
+ "Failed to manage regional monitor incident"
+ );
}
}
@@ -690,7 +707,7 @@ export class MonitorExecutor {
]);
this.logger.info("BullMQ monitor executor shut down successfully");
} catch (error) {
- this.logger.error(error as Error, "Error closing worker");
+ this.logger.error({ err: error }, "Error closing monitor worker");
// Force close if needed
this.worker = null;
}
diff --git a/server/src/services/uptime/monitorScheduler.ts b/server/src/services/uptime/monitorScheduler.ts
index 4a3c40b8a..6cf9aa2b3 100644
--- a/server/src/services/uptime/monitorScheduler.ts
+++ b/server/src/services/uptime/monitorScheduler.ts
@@ -80,19 +80,19 @@ export class MonitorScheduler {
try {
await job.remove();
} catch (err) {
- this.logger.warn({ jobId: job.id, error: err }, "Failed to remove job");
+ this.logger.warn({ jobId: job.id, err }, "Failed to remove job");
}
}
this.logger.info("Cleared stale jobs");
} catch (err) {
- this.logger.warn({ error: err }, "Could not clear all job types");
+ this.logger.warn({ err }, "Could not clear all job types");
}
} else {
this.logger.debug("No stale jobs to clear");
}
} catch (error) {
- this.logger.error(error, "Error clearing stale jobs");
+ this.logger.error({ err: error }, "Error clearing stale jobs");
// Continue anyway - this is not critical for startup
}
}
@@ -109,7 +109,7 @@ export class MonitorScheduler {
await this.scheduleMonitor(monitor.id, monitor.intervalSeconds);
}
} catch (error) {
- this.logger.error(error, "Error loading monitors");
+ this.logger.error({ err: error }, "Error loading monitors");
}
}
@@ -136,7 +136,7 @@ export class MonitorScheduler {
this.logger.info({ monitorId, intervalSeconds, jobId: job.id }, "Scheduled monitor");
} catch (error) {
- this.logger.error({ monitorId, error }, "Error scheduling monitor");
+ this.logger.error({ monitorId, err: error }, "Error scheduling monitor");
}
}
@@ -163,14 +163,14 @@ export class MonitorScheduler {
await job.remove();
} catch (err) {
// Ignore errors for jobs that can't be removed (e.g., repeat jobs)
- this.logger.warn({ jobId: job.id, error: err }, "Could not remove job");
+ this.logger.warn({ jobId: job.id, err }, "Could not remove job");
}
})
);
this.logger.info({ monitorId }, "Removed schedule for monitor");
} catch (error) {
- this.logger.error({ monitorId, error }, "Error removing monitor schedule");
+ this.logger.error({ monitorId, err: error }, "Error removing monitor schedule");
}
}
@@ -196,7 +196,7 @@ export class MonitorScheduler {
this.logger.info({ monitorId, jobId: job.id }, "Triggered immediate check for monitor");
} catch (error) {
- this.logger.error({ monitorId, error }, "Error triggering immediate check for monitor");
+ this.logger.error({ monitorId, err: error }, "Error triggering immediate check for monitor");
}
}
@@ -210,16 +210,16 @@ export class MonitorScheduler {
Promise.race([
this.queueEvents.close(),
new Promise((_, reject) => setTimeout(() => reject(new Error("Queue events close timeout")), 3000)),
- ]).catch(err => this.logger.error(err, "Queue events close error")),
+ ]).catch(err => this.logger.error({ err }, "Queue events close error")),
Promise.race([
this.queue.close(),
new Promise((_, reject) => setTimeout(() => reject(new Error("Queue close timeout")), 3000)),
- ]).catch(err => this.logger.error(err, "Queue close error")),
+ ]).catch(err => this.logger.error({ err }, "Queue close error")),
]);
this.logger.info("BullMQ monitor scheduler shut down successfully");
} catch (error) {
- this.logger.error(error, "Error during scheduler shutdown");
+ this.logger.error({ err: error }, "Error during scheduler shutdown");
}
}
diff --git a/server/src/services/uptime/notificationService.ts b/server/src/services/uptime/notificationService.ts
index eac4e5a46..4061defbe 100644
--- a/server/src/services/uptime/notificationService.ts
+++ b/server/src/services/uptime/notificationService.ts
@@ -59,7 +59,7 @@ export class NotificationService {
await this.sendSMSNotification(channel.config.phoneNumber, monitor, incident, eventType);
}
} catch (error) {
- this.logger.error({ channelType: channel.type, error }, "Failed to send test notification");
+ this.logger.error({ channelType: channel.type, err: error }, "Failed to send test notification");
throw error;
}
}
@@ -125,11 +125,14 @@ export class NotificationService {
.set({ lastNotifiedAt: now.toISOString() })
.where(eq(notificationChannels.id, channel.id));
} catch (error) {
- this.logger.error({ channelType: channel.type, channelId: channel.id, error }, "Failed to send notification");
+ this.logger.error(
+ { channelType: channel.type, channelId: channel.id, err: error },
+ "Failed to send notification"
+ );
}
}
} catch (error) {
- this.logger.error(error, "Failed to send incident notifications");
+ this.logger.error({ err: error }, "Failed to send incident notifications");
}
}
@@ -183,7 +186,7 @@ export class NotificationService {
}
await sendEmail(email, subject, html);
- this.logger.info({ eventType, email, monitorId: monitor.id }, "Sent email notification");
+ this.logger.info({ eventType, monitorId: monitor.id }, "Sent email notification");
}
private async sendDiscordNotification(
@@ -403,6 +406,6 @@ export class NotificationService {
throw new Error(result.error || "Failed to send SMS");
}
- this.logger.info({ eventType, phoneNumber, monitorId: monitor.id }, "Sent SMS notification");
+ this.logger.info({ eventType, monitorId: monitor.id }, "Sent SMS notification");
}
}
diff --git a/server/src/services/uptime/regionHealthChecker.ts b/server/src/services/uptime/regionHealthChecker.ts
index 84e73afad..17b5055f8 100644
--- a/server/src/services/uptime/regionHealthChecker.ts
+++ b/server/src/services/uptime/regionHealthChecker.ts
@@ -14,7 +14,7 @@ export class RegionHealthChecker {
}
async start(): Promise<void> {
- this.logger.info(`Starting region health checker with interval: ${this.intervalMs}ms`);
+ this.logger.info({ intervalMs: this.intervalMs }, "Starting region health checker");
// Run initial check
await this.checkAllRegions();
@@ -42,11 +42,11 @@ export class RegionHealthChecker {
// Filter out local region
const remoteRegions = regions.filter(r => r.code !== "local");
- this.logger.debug(`Checking health of ${remoteRegions.length} remote regions`);
+ this.logger.debug({ regionCount: remoteRegions.length }, "Checking remote region health");
const healthPromises = remoteRegions.map(region =>
this.checkRegionHealth(region).catch(error => {
- this.logger.error(error, `Error checking health of region ${region.code}`);
+ this.logger.error({ err: error, regionCode: region.code }, "Error checking region health");
return { region, isHealthy: false };
})
);
@@ -67,7 +67,7 @@ export class RegionHealthChecker {
const healthyCount = results.filter(r => r.isHealthy).length;
this.logger.info({ healthyCount, totalRegions: remoteRegions.length }, "Region health check complete");
} catch (error) {
- this.logger.error(error, "Error in region health check");
+ this.logger.error({ err: error }, "Error in region health check");
}
}
@@ -93,7 +93,7 @@ export class RegionHealthChecker {
this.logger.warn({ regionCode: region.code, response: data }, "Region returned unexpected response");
return { region, isHealthy: false };
} catch (error) {
- this.logger.error({ regionCode: region.code, error }, "Health check failed for region");
+ this.logger.error({ regionCode: region.code, err: error }, "Health check failed for region");
return { region, isHealthy: false };
}
}
diff --git a/server/src/services/uptime/uptimeService.ts b/server/src/services/uptime/uptimeService.ts
index 46b04a288..d94397505 100644
--- a/server/src/services/uptime/uptimeService.ts
+++ b/server/src/services/uptime/uptimeService.ts
@@ -49,7 +49,7 @@ class UptimeService {
this.initialized = true;
this.logger.info("BullMQ uptime monitoring service initialized successfully");
} catch (error) {
- this.logger.error(error, "Failed to initialize uptime service");
+ this.logger.error({ err: error }, "Failed to initialize uptime service");
this.initializationPromise = null; // Reset on failure
throw error;
}
@@ -71,7 +71,7 @@ class UptimeService {
this.initialized = false;
this.logger.info("BullMQ uptime monitoring service shut down successfully");
} catch (error) {
- this.logger.error(error, "Error during uptime service shutdown");
+ this.logger.error({ err: error }, "Error during uptime service shutdown");
}
}
diff --git a/server/src/services/usageService.ts b/server/src/services/usageService.ts
index cdd41bee0..9326749cf 100644
--- a/server/src/services/usageService.ts
+++ b/server/src/services/usageService.ts
@@ -126,7 +126,7 @@ class UsageService {
return owners.map(owner => owner.email);
} catch (error) {
- this.logger.error(error as Error, `Error getting owner emails for organization ${organizationId}`);
+ this.logger.error({ err: error, organizationId }, "Error getting organization owner emails");
return [];
}
}
@@ -333,7 +333,8 @@ class UsageService {
const isOverLimit = eventCount > eventLimit;
const replayCount = orgStats?.replayCount || 0;
- const replayBlocked = !subscriptionIncludesReplay(subscription) || replayCount >= getReplayLimit(subscription);
+ const replayBlocked =
+ !subscriptionIncludesReplay(subscription) || replayCount >= getReplayLimit(subscription);
let sendApproaching = false;
if (!alreadyNotifiedApproaching && !isOverLimit && Number.isFinite(eventLimit) && daysRemaining >= 2) {
@@ -362,16 +363,19 @@ class UsageService {
for (const ownerEmail of ownerEmails) {
try {
await sendLimitExceededEmail(ownerEmail, orgData.name, eventCount, eventLimit);
- this.logger.info(`Sent limit exceeded email to owner ${ownerEmail} for organization ${orgData.name}`);
+ this.logger.info({ organizationId: orgData.id }, "Sent limit-exceeded email to organization owner");
} catch (error) {
this.logger.error(
- error as Error,
- `Failed to send limit exceeded email to owner ${ownerEmail} for organization ${orgData.name}`
+ { err: error, organizationId: orgData.id },
+ "Failed to send limit-exceeded email to organization owner"
);
}
}
} else {
- this.logger.warn(`No owners found for organization ${orgData.name}, skipping limit exceeded email`);
+ this.logger.warn(
+ { organizationId: orgData.id },
+ "No organization owners found; skipping limit-exceeded email"
+ );
}
}
@@ -382,17 +386,21 @@ class UsageService {
try {
await sendApproachingLimitEmail(ownerEmail, orgData.name, eventCount, eventLimit);
this.logger.info(
- `Sent approaching-limit email to owner ${ownerEmail} for organization ${orgData.name}`
+ { organizationId: orgData.id },
+ "Sent approaching-limit email to organization owner"
);
} catch (error) {
this.logger.error(
- error as Error,
- `Failed to send approaching-limit email to owner ${ownerEmail} for organization ${orgData.name}`
+ { err: error, organizationId: orgData.id },
+ "Failed to send approaching-limit email to organization owner"
);
}
}
} else {
- this.logger.warn(`No owners found for organization ${orgData.name}, skipping approaching-limit email`);
+ this.logger.warn(
+ { organizationId: orgData.id },
+ "No organization owners found; skipping approaching-limit email"
+ );
}
}
diff --git a/server/src/services/weekyReports/weeklyReportService.ts b/server/src/services/weekyReports/weeklyReportService.ts
index 6eb8d747f..1b9fd1215 100644
--- a/server/src/services/weekyReports/weeklyReportService.ts
+++ b/server/src/services/weekyReports/weeklyReportService.ts
@@ -79,7 +79,7 @@ class WeeklyReportService {
const data = await processResults<OverviewData>(result);
return data[0] || null;
} catch (error) {
- this.logger.error({ error, siteId }, "Error fetching overview data");
+ this.logger.error({ err: error, siteId }, "Error fetching overview data");
return null;
}
}
@@ -217,7 +217,7 @@ class WeeklyReportService {
return await processResults<MetricData>(result);
} catch (error) {
- this.logger.error({ error, siteId, parameter }, "Error fetching top N data");
+ this.logger.error({ err: error, siteId, parameter }, "Error fetching top N data");
return [];
}
}
@@ -275,7 +275,7 @@ class WeeklyReportService {
deviceBreakdown,
};
} catch (error) {
- this.logger.error({ error, siteId }, "Error generating site report");
+ this.logger.error({ err: error, siteId }, "Error generating site report");
return null;
}
}
@@ -323,7 +323,7 @@ class WeeklyReportService {
sites: siteReports,
};
} catch (error) {
- this.logger.error({ error, organizationId }, "Error generating organization report");
+ this.logger.error({ err: error, organizationId }, "Error generating organization report");
return null;
}
}
@@ -370,28 +370,25 @@ class WeeklyReportService {
}
for (const site of allowedSites) {
-
try {
await sendWeeklyReportEmail(memberData.email, memberData.name, report.organizationName, site);
this.logger.info(
{
- email: memberData.email,
organizationId: report.organizationId,
siteId: site.siteId,
- siteName: site.siteName,
},
"Sent weekly report email for site"
);
} catch (error) {
this.logger.error(
- { error, email: memberData.email, organizationId: report.organizationId, siteId: site.siteId },
+ { err: error, organizationId: report.organizationId, siteId: site.siteId },
"Failed to send email to member for site"
);
}
}
}
} catch (error) {
- this.logger.error({ error, organizationId: report.organizationId }, "Error sending reports to organization");
+ this.logger.error({ err: error, organizationId: report.organizationId }, "Error sending reports to organization");
}
}
@@ -441,7 +438,7 @@ class WeeklyReportService {
"Completed weekly report generation and sending"
);
} catch (error) {
- this.logger.error({ error }, "Error in weekly report generation");
+ this.logger.error({ err: error }, "Error in weekly report generation");
}
}
@@ -460,7 +457,7 @@ class WeeklyReportService {
try {
await this.generateAndSendReports();
} catch (error) {
- this.logger.error(error as Error, "Error during weekly report generation");
+ this.logger.error({ err: error }, "Error during weekly report generation");
}
},
{ timezone: "UTC" }