feat: Add centralized logging system with @link-stack/logger package

- Create new @link-stack/logger package wrapping Pino for structured logging
- Replace all console.log/error/warn statements across the monorepo
- Configure environment-aware logging (pretty-print in dev, JSON in prod)
- Add automatic redaction of sensitive fields (passwords, tokens, etc.)
- Remove dead commented-out logger file from bridge-worker
- Follow Pino's standard argument order (context object first, message second)
- Support log levels via LOG_LEVEL environment variable
- Export TypeScript types for better IDE support

This provides consistent, structured logging across all applications
and packages, making debugging easier and production logs more parseable.
This commit is contained in:
Darren Clarke 2025-08-20 11:37:39 +02:00
parent 5b89bfce7c
commit c1feaa4cb1
42 changed files with 3824 additions and 2422 deletions

View file

@ -1,4 +1,7 @@
import { db } from "@link-stack/bridge-common";
import { createLogger } from "@link-stack/logger";
const logger = createLogger('notify-webhooks');
export interface NotifyWebhooksOptions {
backendId: string;
@ -10,11 +13,10 @@ const notifyWebhooksTask = async (
): Promise<void> => {
const { backendId, payload } = options;
console.log(`[notify-webhooks] Processing webhook notification:`, {
logger.debug({
backendId,
payloadKeys: Object.keys(payload),
payload: JSON.stringify(payload, null, 2),
});
}, 'Processing webhook notification');
const webhooks = await db
.selectFrom("Webhook")
@ -22,20 +24,19 @@ const notifyWebhooksTask = async (
.where("backendId", "=", backendId)
.execute();
console.log(`[notify-webhooks] Found ${webhooks.length} webhooks for backend ${backendId}`);
logger.debug({ count: webhooks.length, backendId }, 'Found webhooks');
for (const webhook of webhooks) {
const { endpointUrl, httpMethod, headers } = webhook;
const finalHeaders = { "Content-Type": "application/json", ...headers };
const body = JSON.stringify(payload);
console.log(`[notify-webhooks] Sending webhook:`, {
logger.debug({
url: endpointUrl,
method: httpMethod,
bodyLength: body.length,
headers: Object.keys(finalHeaders),
payload: body,
});
headerKeys: Object.keys(finalHeaders),
}, 'Sending webhook');
try {
const result = await fetch(endpointUrl, {
@ -44,26 +45,26 @@ const notifyWebhooksTask = async (
body,
});
console.log(`[notify-webhooks] Webhook response:`, {
logger.debug({
url: endpointUrl,
status: result.status,
statusText: result.statusText,
ok: result.ok,
});
}, 'Webhook response');
if (!result.ok) {
const responseText = await result.text();
console.error(`[notify-webhooks] Webhook error response:`, {
logger.error({
url: endpointUrl,
status: result.status,
response: responseText.substring(0, 500), // First 500 chars
});
responseSample: responseText.substring(0, 500),
}, 'Webhook error response');
}
} catch (error) {
console.error(`[notify-webhooks] Webhook request failed:`, {
logger.error({
url: endpointUrl,
error: error instanceof Error ? error.message : error,
});
}, 'Webhook request failed');
}
}
};

View file

@ -1,4 +1,7 @@
import { db } from "@link-stack/bridge-common";
import { createLogger } from "@link-stack/logger";
const logger = createLogger('bridge-worker-send-facebook-message');
interface SendFacebookMessageTaskOptions {
token: string;
@ -32,7 +35,7 @@ const sendFacebookMessageTask = async (
body: JSON.stringify(outgoingMessage),
});
} catch (error) {
console.error({ error });
logger.error({ error });
throw error;
}
};

View file

@ -1,6 +1,9 @@
import { db, getWorkerUtils } from "@link-stack/bridge-common";
import { createLogger } from "@link-stack/logger";
import * as signalApi from "@link-stack/signal-api";
const logger = createLogger('fetch-signal-messages');
const { Configuration, MessagesApi, AttachmentsApi } = signalApi;
const config = new Configuration({
basePath: process.env.BRIDGE_SIGNAL_URL,
@ -59,8 +62,7 @@ const processMessage = async ({
const { attachments } = dataMessage;
const rawTimestamp = dataMessage?.timestamp;
// Debug logging for group detection
console.log(`[fetch-signal-messages] Processing message:`, {
logger.debug({
sourceUuid,
source,
rawTimestamp,
@ -71,7 +73,7 @@ const processMessage = async ({
groupV2Id: dataMessage?.groupV2?.id,
groupContextType: dataMessage?.groupContext?.type,
groupInfoType: dataMessage?.groupInfo?.type,
});
}, 'Processing message');
const timestamp = new Date(rawTimestamp);
const formattedAttachments = await fetchAttachments(attachments);
@ -148,9 +150,7 @@ const fetchSignalMessagesTask = async ({
number: phoneNumber,
});
console.log(
`[fetch-signal-messages] Fetching messages for bot ${id} (${phoneNumber})`,
);
logger.debug({ botId: id, phoneNumber }, 'Fetching messages for bot');
for (const message of messages) {
const formattedMessages = await processMessage({
@ -160,14 +160,14 @@ const fetchSignalMessagesTask = async ({
});
for (const formattedMessage of formattedMessages) {
if (formattedMessage.to !== formattedMessage.from) {
console.log(`[fetch-signal-messages] Creating job for message:`, {
logger.debug({
messageId: formattedMessage.messageId,
from: formattedMessage.from,
to: formattedMessage.to,
isGroup: formattedMessage.isGroup,
hasMessage: !!formattedMessage.message,
hasAttachment: !!formattedMessage.attachment,
});
}, 'Creating job for message');
await worker.addJob(
"signal/receive-signal-message",

View file

@ -2,6 +2,9 @@
/*
import { URLSearchParams } from "url";
import { withDb, AppDatabase } from "../../lib/db.js";
import { createLogger } from "@link-stack/logger";
const logger = createLogger('bridge-worker-import-leafcutter');
// import { loadConfig } from "@digiresilience/bridge-config";
const config: any = {};
@ -122,7 +125,7 @@ const sendToLeafcutter = async (tickets: LabelStudioTicket[]) => {
};
});
console.info("Sending to Leafcutter");
logger.info("Sending to Leafcutter");
const result = await fetch(opensearchApiUrl, {
method: "POST",

View file

@ -1,7 +1,10 @@
import { db, getWorkerUtils } from "@link-stack/bridge-common";
import { createLogger } from "@link-stack/logger";
import * as signalApi from "@link-stack/signal-api";
const { Configuration, GroupsApi } = signalApi;
const logger = createLogger('bridge-worker-receive-signal-message');
interface ReceiveSignalMessageTaskOptions {
token: string;
to: string;
@ -27,7 +30,7 @@ const receiveSignalMessageTask = async ({
mimeType,
isGroup,
}: ReceiveSignalMessageTaskOptions): Promise<void> => {
console.log(`[receive-signal-message] Processing incoming message:`, {
logger.debug({
messageId,
from,
to,
@ -35,7 +38,7 @@ const receiveSignalMessageTask = async ({
hasMessage: !!message,
hasAttachment: !!attachment,
token,
});
}, 'Processing incoming message');
const worker = await getWorkerUtils();
const row = await db
.selectFrom("SignalBot")
@ -50,20 +53,18 @@ const receiveSignalMessageTask = async ({
// Check if auto-group creation is enabled and this is NOT already a group message
const enableAutoGroups = process.env.BRIDGE_SIGNAL_AUTO_GROUPS === "true";
console.log(`[receive-signal-message] Auto-groups config:`, {
logger.debug({
enableAutoGroups,
isGroup,
shouldCreateGroup: enableAutoGroups && !isGroup && from && to,
});
}, 'Auto-groups config');
// If this is already a group message and auto-groups is enabled,
// use group provided in 'to'
if (enableAutoGroups && isGroup && to) {
// Signal sends the internal ID (base64) in group messages
// We should NOT add "group." prefix - that's for sending messages, not receiving
console.log(
`[receive-signal-message] Message is from existing group with internal ID`,
);
logger.debug('Message is from existing group with internal ID');
finalTo = to;
} else if (enableAutoGroups && !isGroup && from && to) {
@ -75,9 +76,7 @@ const receiveSignalMessageTask = async ({
// Always create a new group for direct messages to the helpdesk
// This ensures each conversation gets its own group/ticket
console.log(
`[receive-signal-message] Creating new group for user ${from}`,
);
logger.info({ from }, 'Creating new group for user');
// Include timestamp to make each group unique
const timestamp = new Date()
@ -96,10 +95,7 @@ const receiveSignalMessageTask = async ({
},
});
console.log(
`[receive-signal-message] Full group creation response from Signal API:`,
JSON.stringify(createGroupResponse, null, 2),
);
logger.debug({ createGroupResponse }, 'Group creation response from Signal API');
if (createGroupResponse.id) {
// The createGroupResponse.id already contains the full group identifier (group.BASE64)
@ -108,31 +104,21 @@ const receiveSignalMessageTask = async ({
// Fetch the group details to get the actual internalId
// The base64 part of the ID is NOT the same as the internalId!
try {
console.log(
`[receive-signal-message] Fetching group details to get internalId...`,
);
logger.debug('Fetching group details to get internalId');
const groups = await groupsClient.v1GroupsNumberGet({
number: row.phoneNumber,
});
console.log(
`[receive-signal-message] All groups for bot:`,
JSON.stringify(groups.slice(0, 3), null, 2), // Show first 3 to avoid too much output
);
logger.debug({ groupsSample: groups.slice(0, 3) }, 'Groups for bot');
const createdGroup = groups.find((g) => g.id === finalTo);
if (createdGroup) {
console.log(
`[receive-signal-message] Found created group details:`,
JSON.stringify(createdGroup, null, 2),
);
logger.debug({ createdGroup }, 'Found created group details');
}
if (createdGroup && createdGroup.internalId) {
createdInternalId = createdGroup.internalId;
console.log(
`[receive-signal-message] Got actual internalId: ${createdInternalId}`,
);
logger.debug({ createdInternalId }, 'Got actual internalId');
} else {
// Fallback: extract base64 part from ID
if (finalTo.startsWith("group.")) {
@ -140,36 +126,32 @@ const receiveSignalMessageTask = async ({
}
}
} catch (fetchError) {
console.log(
`[receive-signal-message] Could not fetch group details, using ID base64 part`,
);
logger.debug('Could not fetch group details, using ID base64 part');
// Fallback: extract base64 part from ID
if (finalTo.startsWith("group.")) {
createdInternalId = finalTo.substring(6);
}
}
console.log(`[receive-signal-message] Group created successfully:`, {
logger.debug({
fullGroupId: finalTo,
internalId: createdInternalId,
});
console.log(`[receive-signal-message] Created new Signal group:`, {
}, 'Group created successfully');
logger.debug({
groupId: finalTo,
internalId: createdInternalId,
groupName,
forPhoneNumber: from,
botNumber: row.phoneNumber,
response: createGroupResponse,
});
}, 'Created new Signal group');
}
// Now handle notifications and message forwarding for both new and existing groups
if (finalTo && finalTo.startsWith("group.")) {
// Forward the user's initial message to the group using quote feature
try {
console.log(
`[receive-signal-message] Forwarding initial message to group using quote feature`,
);
logger.debug('Forwarding initial message to group using quote feature');
const attributionMessage = `Message from ${from}:\n"${message}"\n\n---\nSupport team: Your request has been received. An agent will respond shortly.`;
@ -183,21 +165,14 @@ const receiveSignalMessageTask = async ({
quoteTimestamp: Date.parse(sentAt),
});
console.log(
`[receive-signal-message] Successfully forwarded initial message to group ${finalTo}`,
);
logger.debug({ finalTo }, 'Successfully forwarded initial message to group');
} catch (forwardError) {
console.error(
"[receive-signal-message] Error forwarding message to group:",
forwardError,
);
logger.error({ error: forwardError }, 'Error forwarding message to group');
}
// Send a response to the original DM informing about the group
try {
console.log(
`[receive-signal-message] Sending group notification to original DM`,
);
logger.debug('Sending group notification to original DM');
const dmNotification = `Hello! A private support group has been created for your conversation.\n\nGroup name: ${groupName}\n\nPlease look for the new group in your Signal app to continue the conversation. Our support team will respond there shortly.\n\nThank you for contacting support!`;
@ -208,14 +183,9 @@ const receiveSignalMessageTask = async ({
conversationId: null,
});
console.log(
`[receive-signal-message] Successfully sent group notification to user DM`,
);
logger.debug('Successfully sent group notification to user DM');
} catch (dmError) {
console.error(
"[receive-signal-message] Error sending DM notification:",
dmError,
);
logger.error({ error: dmError }, 'Error sending DM notification');
}
}
} catch (error: any) {
@ -227,16 +197,14 @@ const receiveSignalMessageTask = async ({
errorMessage?.toString().toLowerCase().includes("exists");
if (isAlreadyExists) {
console.log(
`[receive-signal-message] Group might already exist for ${from}, continuing with original recipient`,
);
logger.debug({ from }, 'Group might already exist, continuing with original recipient');
} else {
console.error("[receive-signal-message] Error creating Signal group:", {
logger.error({
error: errorMessage,
from,
to,
botNumber: row.phoneNumber,
});
}, 'Error creating Signal group');
}
}
}

View file

@ -1,7 +1,10 @@
import { db, getWorkerUtils } from "@link-stack/bridge-common";
import { createLogger } from "@link-stack/logger";
import * as signalApi from "@link-stack/signal-api";
const { Configuration, MessagesApi, GroupsApi } = signalApi;
const logger = createLogger('bridge-worker-send-signal-message');
interface SendSignalMessageTaskOptions {
token: string;
to: string;
@ -21,12 +24,12 @@ const sendSignalMessageTask = async ({
quoteAuthor,
quoteTimestamp,
}: SendSignalMessageTaskOptions): Promise<void> => {
console.log(`[send-signal-message] Processing outgoing message:`, {
logger.debug({
token,
to,
conversationId,
messageLength: message?.length,
});
}, 'Processing outgoing message');
const bot = await db
.selectFrom("SignalBot")
.selectAll()
@ -55,12 +58,12 @@ const sendSignalMessageTask = async ({
const isGroupId = isUUID || isGroupPrefix || isBase64;
const enableAutoGroups = process.env.BRIDGE_SIGNAL_AUTO_GROUPS === "true";
console.log(`[send-signal-message] Recipient analysis:`, {
logger.debug({
to,
isGroupId,
enableAutoGroups,
shouldCreateGroup: enableAutoGroups && !isGroupId && to && conversationId,
});
}, 'Recipient analysis');
// If sending to a phone number and auto-groups is enabled, create a group first
if (enableAutoGroups && !isGroupId && to && conversationId) {
@ -90,9 +93,7 @@ const sendSignalMessageTask = async ({
const createdGroup = groups.find((g) => g.id === finalTo);
if (createdGroup && createdGroup.internalId) {
internalId = createdGroup.internalId;
console.log(
`[send-signal-message] Got actual internalId: ${internalId}`,
);
logger.debug({ internalId }, 'Got actual internalId');
} else {
// Fallback: extract base64 part from ID
if (finalTo.startsWith("group.")) {
@ -100,22 +101,20 @@ const sendSignalMessageTask = async ({
}
}
} catch (fetchError) {
console.log(
`[send-signal-message] Could not fetch group details, using ID base64 part`,
);
logger.debug('Could not fetch group details, using ID base64 part');
// Fallback: extract base64 part from ID
if (finalTo.startsWith("group.")) {
internalId = finalTo.substring(6);
}
}
console.log(`[send-signal-message] Created new Signal group:`, {
logger.debug({
groupId: finalTo,
internalId,
groupName,
conversationId,
originalRecipient: to,
botNumber: bot.phoneNumber,
});
}, 'Created new Signal group');
// Notify Zammad about the new group ID via webhook
await worker.addJob("common/notify-webhooks", {
@ -131,23 +130,23 @@ const sendSignalMessageTask = async ({
});
}
} catch (groupError) {
console.error("[send-signal-message] Error creating Signal group:", {
logger.error({
error: groupError instanceof Error ? groupError.message : groupError,
to,
conversationId,
});
}, 'Error creating Signal group');
// Continue with original recipient if group creation fails
}
}
console.log(`[send-signal-message] Sending message via API:`, {
logger.debug({
fromNumber: number,
toRecipient: finalTo,
originalTo: to,
recipientChanged: to !== finalTo,
groupCreated,
isGroupRecipient: finalTo.startsWith("group."),
});
}, 'Sending message via API');
// Build the message data with optional quote parameters
const messageData: signalApi.ApiSendMessageV2 = {
@ -156,12 +155,12 @@ const sendSignalMessageTask = async ({
message,
};
console.log(`[send-signal-message] Message data being sent:`, {
logger.debug({
number,
recipients: [finalTo],
message: message.substring(0, 50) + "...",
hasQuoteParams: !!(quoteMessage && quoteAuthor && quoteTimestamp),
});
}, 'Message data being sent');
// Add quote parameters if all are provided
if (quoteMessage && quoteAuthor && quoteTimestamp) {
@ -169,28 +168,28 @@ const sendSignalMessageTask = async ({
messageData.quoteAuthor = quoteAuthor;
messageData.quoteMessage = quoteMessage;
console.log(`[send-signal-message] Including quote in message:`, {
logger.debug({
quoteAuthor,
quoteMessage: quoteMessage.substring(0, 50) + "...",
quoteTimestamp,
});
}, 'Including quote in message');
}
const response = await messagesClient.v2SendPost({
data: messageData,
});
console.log(`[send-signal-message] Message sent successfully:`, {
logger.debug({
to: finalTo,
groupCreated,
response: response?.timestamp || "no timestamp",
});
}, 'Message sent successfully');
} catch (error: any) {
// Try to get the actual error message from the response
if (error.response) {
try {
const errorBody = await error.response.text();
console.error(`[send-signal-message] Signal API error:`, {
logger.error({
status: error.response.status,
statusText: error.response.statusText,
body: errorBody,
@ -200,12 +199,12 @@ const sendSignalMessageTask = async ({
toRecipients: [finalTo],
hasQuote: !!quoteMessage,
},
});
}, 'Signal API error');
} catch (e) {
console.error(`[send-signal-message] Could not parse error response`);
logger.error('Could not parse error response');
}
}
console.error(`[send-signal-message] Full error:`, error);
logger.error({ error }, 'Full error details');
throw error;
}
};

View file

@ -3,6 +3,9 @@ import { withDb, AppDatabase } from "../../lib/db.js";
import { twilioClientFor } from "../../lib/common.js";
import { CallInstance } from "twilio/lib/rest/api/v2010/account/call";
import workerUtils from "../../lib/utils.js";
import { createLogger } from "@link-stack/logger";
const logger = createLogger('bridge-worker-twilio-recording');
interface WebhookPayload {
startTime: string;
@ -20,7 +23,7 @@ const getTwilioRecording = async (url: string) => {
const { payload } = await Wreck.get(url);
return { recording: payload as Buffer };
} catch (error: any) {
console.error(error.output);
logger.error(error.output);
return { error: error.output };
}
};

View file

@ -1,4 +1,7 @@
import { db } from "@link-stack/bridge-common";
import { createLogger } from "@link-stack/logger";
const logger = createLogger('bridge-worker-send-whatsapp-message');
interface SendWhatsappMessageTaskOptions {
token: string;
@ -26,7 +29,7 @@ const sendWhatsappMessageTask = async ({
body: JSON.stringify(params),
});
} catch (error) {
console.error({ error });
logger.error({ error });
throw new Error("Failed to send message");
}
};