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:
parent
5b89bfce7c
commit
c1feaa4cb1
42 changed files with 3824 additions and 2422 deletions
|
|
@ -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;
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue