From 78670ce7124f6040f41a7efa8015937b6fc5c411 Mon Sep 17 00:00:00 2001 From: Krzysztof Wolski Date: Wed, 5 Jul 2023 17:17:49 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=A7=20Improve=20log=20formatting=20(#7?= =?UTF-8?q?24)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Improve log formatting --- .changeset/witty-plants-stare.md | 5 +++ .../event-handlers/send-event-messages.ts | 2 +- .../sendgrid-config-migration-v1-to-v2.ts | 2 +- .../sendgrid-configuration.service.ts | 2 +- .../sendgrid-metadata-manager.ts | 2 +- .../src/modules/sendgrid/send-sendgrid.ts | 2 +- .../smtp/compile-handlebars-template.ts | 2 +- .../src/modules/smtp/compile-mjml.ts | 2 +- .../smtp-config-migration-v1-to-v2.ts | 6 +--- .../smtp-configuration.service.ts | 2 +- .../configuration/smtp-metadata-manager.ts | 2 +- .../src/modules/smtp/html-to-plaintext.ts | 2 +- .../src/modules/smtp/send-email-with-smtp.ts | 2 +- .../src/modules/smtp/send-smtp.ts | 30 +++++++---------- .../src/pages/api/webhooks/gift-card-sent.ts | 8 ++--- .../src/pages/api/webhooks/invoice-sent.ts | 8 ++--- .../src/pages/api/webhooks/notify.ts | 33 +++++++++---------- .../src/pages/api/webhooks/order-cancelled.ts | 8 ++--- .../src/pages/api/webhooks/order-confirmed.ts | 8 ++--- .../src/pages/api/webhooks/order-created.ts | 8 ++--- .../src/pages/api/webhooks/order-fulfilled.ts | 8 ++--- .../pages/api/webhooks/order-fully-paid.ts | 8 ++--- 22 files changed, 73 insertions(+), 79 deletions(-) create mode 100644 .changeset/witty-plants-stare.md diff --git a/.changeset/witty-plants-stare.md b/.changeset/witty-plants-stare.md new file mode 100644 index 0000000..7c0ae29 --- /dev/null +++ b/.changeset/witty-plants-stare.md @@ -0,0 +1,5 @@ +--- +"saleor-app-emails-and-messages": patch +--- + +Changed formatting in the debugging logs to be more concise. diff --git a/apps/emails-and-messages/src/modules/event-handlers/send-event-messages.ts b/apps/emails-and-messages/src/modules/event-handlers/send-event-messages.ts index 4a615ac..e5e1969 100644 --- a/apps/emails-and-messages/src/modules/event-handlers/send-event-messages.ts +++ b/apps/emails-and-messages/src/modules/event-handlers/send-event-messages.ts @@ -29,7 +29,7 @@ export const sendEventMessages = async ({ client, }: SendEventMessagesArgs) => { const logger = createLogger({ - fn: "sendEventMessages", + name: "sendEventMessages", }); logger.debug("Function called"); diff --git a/apps/emails-and-messages/src/modules/sendgrid/configuration/migrations/sendgrid-config-migration-v1-to-v2.ts b/apps/emails-and-messages/src/modules/sendgrid/configuration/migrations/sendgrid-config-migration-v1-to-v2.ts index 81a356e..c1d6a60 100644 --- a/apps/emails-and-messages/src/modules/sendgrid/configuration/migrations/sendgrid-config-migration-v1-to-v2.ts +++ b/apps/emails-and-messages/src/modules/sendgrid/configuration/migrations/sendgrid-config-migration-v1-to-v2.ts @@ -5,7 +5,7 @@ import { sendgridTransformV1toV2 } from "./sendgrid-transform-v1-to-v2"; import { createLogger } from "@saleor/apps-shared"; const logger = createLogger({ - fn: "sendgridConfigMigrationV1ToV2", + name: "sendgridConfigMigrationV1ToV2", }); interface SendgridConfigMigrationV1ToV1Args { diff --git a/apps/emails-and-messages/src/modules/sendgrid/configuration/sendgrid-configuration.service.ts b/apps/emails-and-messages/src/modules/sendgrid/configuration/sendgrid-configuration.service.ts index d978e80..772ccd4 100644 --- a/apps/emails-and-messages/src/modules/sendgrid/configuration/sendgrid-configuration.service.ts +++ b/apps/emails-and-messages/src/modules/sendgrid/configuration/sendgrid-configuration.service.ts @@ -12,7 +12,7 @@ import { SendgridPrivateMetadataManager } from "./sendgrid-metadata-manager"; import { FeatureFlagService } from "../../feature-flag-service/feature-flag-service"; const logger = createLogger({ - service: "SendgridConfigurationService", + name: "SendgridConfigurationService", }); export type SendgridConfigurationServiceErrorType = diff --git a/apps/emails-and-messages/src/modules/sendgrid/configuration/sendgrid-metadata-manager.ts b/apps/emails-and-messages/src/modules/sendgrid/configuration/sendgrid-metadata-manager.ts index 06732b3..55c0b6b 100644 --- a/apps/emails-and-messages/src/modules/sendgrid/configuration/sendgrid-metadata-manager.ts +++ b/apps/emails-and-messages/src/modules/sendgrid/configuration/sendgrid-metadata-manager.ts @@ -5,7 +5,7 @@ import { sendgridConfigMigrationV1ToV2 } from "./migrations/sendgrid-config-migr import { createLogger } from "@saleor/apps-shared"; const logger = createLogger({ - fn: "SendgridPrivateMetadataManager", + name: "SendgridPrivateMetadataManager", }); export class SendgridPrivateMetadataManager { diff --git a/apps/emails-and-messages/src/modules/sendgrid/send-sendgrid.ts b/apps/emails-and-messages/src/modules/sendgrid/send-sendgrid.ts index 90fcae3..bbb6a30 100644 --- a/apps/emails-and-messages/src/modules/sendgrid/send-sendgrid.ts +++ b/apps/emails-and-messages/src/modules/sendgrid/send-sendgrid.ts @@ -24,7 +24,7 @@ export const sendSendgrid = async ({ sendgridConfiguration, }: SendSendgridArgs) => { const logger = createLogger({ - fn: "sendSendgrid", + name: "sendSendgrid", event, }); diff --git a/apps/emails-and-messages/src/modules/smtp/compile-handlebars-template.ts b/apps/emails-and-messages/src/modules/smtp/compile-handlebars-template.ts index 51d1a7e..da1d6e4 100644 --- a/apps/emails-and-messages/src/modules/smtp/compile-handlebars-template.ts +++ b/apps/emails-and-messages/src/modules/smtp/compile-handlebars-template.ts @@ -2,7 +2,7 @@ import Handlebars from "handlebars"; import { createLogger } from "@saleor/apps-shared"; const logger = createLogger({ - fn: "compileHandlebarsTemplate", + name: "compileHandlebarsTemplate", }); export const compileHandlebarsTemplate = (template: string, variables: any) => { diff --git a/apps/emails-and-messages/src/modules/smtp/compile-mjml.ts b/apps/emails-and-messages/src/modules/smtp/compile-mjml.ts index 1fea8f5..d8a00c7 100644 --- a/apps/emails-and-messages/src/modules/smtp/compile-mjml.ts +++ b/apps/emails-and-messages/src/modules/smtp/compile-mjml.ts @@ -2,7 +2,7 @@ import mjml2html from "mjml"; import { createLogger } from "@saleor/apps-shared"; const logger = createLogger({ - fn: "compileMjml", + name: "compileMjml", }); export const compileMjml = (mjml: string) => { diff --git a/apps/emails-and-messages/src/modules/smtp/configuration/migrations/smtp-config-migration-v1-to-v2.ts b/apps/emails-and-messages/src/modules/smtp/configuration/migrations/smtp-config-migration-v1-to-v2.ts index 7644fb6..e37b21c 100644 --- a/apps/emails-and-messages/src/modules/smtp/configuration/migrations/smtp-config-migration-v1-to-v2.ts +++ b/apps/emails-and-messages/src/modules/smtp/configuration/migrations/smtp-config-migration-v1-to-v2.ts @@ -1,15 +1,11 @@ import { AppConfigPrivateMetadataManager } from "../../../app-configuration/app-config-metadata-manager"; -import { AppConfig } from "../../../app-configuration/app-config-schema"; -import { ChannelConfiguration } from "../../../channels/channel-configuration-schema"; import { SettingsManager } from "@saleor/app-sdk/settings-manager"; import { MjmlPrivateMetadataManager } from "../mjml-metadata-manager"; -import { SmtpConfigV2 } from "./smtp-config-schema-v2"; -import { getChannelsAssignedToConfigId } from "../../../app-configuration/migrations/get-channels-assigned-to-config-id"; import { smtpTransformV1toV2 } from "./smtp-transform-v1-to-v2"; import { createLogger } from "@saleor/apps-shared"; const logger = createLogger({ - fn: "smtpConfigMigrationV1ToV2", + name: "smtpConfigMigrationV1ToV2", }); interface SmtpConfigMigrationV1ToV1Args { diff --git a/apps/emails-and-messages/src/modules/smtp/configuration/smtp-configuration.service.ts b/apps/emails-and-messages/src/modules/smtp/configuration/smtp-configuration.service.ts index 658d0fd..60babc9 100644 --- a/apps/emails-and-messages/src/modules/smtp/configuration/smtp-configuration.service.ts +++ b/apps/emails-and-messages/src/modules/smtp/configuration/smtp-configuration.service.ts @@ -8,7 +8,7 @@ import { filterConfigurations } from "../../app-configuration/filter-configurati import { FeatureFlagService } from "../../feature-flag-service/feature-flag-service"; const logger = createLogger({ - service: "SmtpConfigurationService", + name: "SmtpConfigurationService", }); export type SmtpConfigurationServiceErrorType = diff --git a/apps/emails-and-messages/src/modules/smtp/configuration/smtp-metadata-manager.ts b/apps/emails-and-messages/src/modules/smtp/configuration/smtp-metadata-manager.ts index 50d0aa7..ee060c9 100644 --- a/apps/emails-and-messages/src/modules/smtp/configuration/smtp-metadata-manager.ts +++ b/apps/emails-and-messages/src/modules/smtp/configuration/smtp-metadata-manager.ts @@ -5,7 +5,7 @@ import { smtpConfigMigrationV1ToV2 } from "./migrations/smtp-config-migration-v1 import { createLogger } from "@saleor/apps-shared"; const logger = createLogger({ - fn: "SmtpPrivateMetadataManager", + name: "SmtpPrivateMetadataManager", }); export class SmtpPrivateMetadataManager { diff --git a/apps/emails-and-messages/src/modules/smtp/html-to-plaintext.ts b/apps/emails-and-messages/src/modules/smtp/html-to-plaintext.ts index b0bfaf0..8ac49ec 100644 --- a/apps/emails-and-messages/src/modules/smtp/html-to-plaintext.ts +++ b/apps/emails-and-messages/src/modules/smtp/html-to-plaintext.ts @@ -2,7 +2,7 @@ import { convert } from "html-to-text"; import { createLogger } from "@saleor/apps-shared"; const logger = createLogger({ - fn: "htmlToPlaintext", + name: "htmlToPlaintext", }); export const htmlToPlaintext = (html: string) => { diff --git a/apps/emails-and-messages/src/modules/smtp/send-email-with-smtp.ts b/apps/emails-and-messages/src/modules/smtp/send-email-with-smtp.ts index 7368357..04d7b2d 100644 --- a/apps/emails-and-messages/src/modules/smtp/send-email-with-smtp.ts +++ b/apps/emails-and-messages/src/modules/smtp/send-email-with-smtp.ts @@ -3,7 +3,7 @@ import { createLogger } from "@saleor/apps-shared"; import { SmtpEncryptionType } from "./configuration/migrations/mjml-config-schema-v1"; const logger = createLogger({ - fn: "sendEmailWithSmtp", + name: "sendEmailWithSmtp", }); export interface SendMailArgs { diff --git a/apps/emails-and-messages/src/modules/smtp/send-smtp.ts b/apps/emails-and-messages/src/modules/smtp/send-smtp.ts index 35a4800..608659c 100644 --- a/apps/emails-and-messages/src/modules/smtp/send-smtp.ts +++ b/apps/emails-and-messages/src/modules/smtp/send-smtp.ts @@ -27,7 +27,7 @@ export const sendSmtp = async ({ smtpConfiguration, }: SendSmtpArgs) => { const logger = createLogger({ - fn: "sendSmtp", + name: "sendSmtp", event, }); @@ -35,24 +35,12 @@ export const sendSmtp = async ({ if (!eventSettings) { logger.debug("No active settings for this event, skipping"); - return { - errors: [ - { - message: "No active settings for this event", - }, - ], - }; + return; } if (!eventSettings.active) { logger.debug("Event settings are not active, skipping"); - return { - errors: [ - { - message: "Event settings are not active", - }, - ], - }; + return; } logger.debug("Sending an email using MJML"); @@ -64,8 +52,6 @@ export const sendSmtp = async ({ payload ); - logger.warn(`email subject ${emailSubject} ${subject}`); - if (handlebarsSubjectErrors?.length) { logger.error("Error during the handlebars subject template compilation"); return { @@ -80,6 +66,8 @@ export const sendSmtp = async ({ }; } + logger.debug({ emailSubject }, "Subject compiled"); + const { template: mjmlTemplate, errors: handlebarsErrors } = compileHandlebarsTemplate( rawTemplate, payload @@ -99,6 +87,8 @@ export const sendSmtp = async ({ }; } + logger.debug("Handlebars template compiled"); + const { html: emailBodyHtml, errors: mjmlCompilationErrors } = compileMjml(mjmlTemplate); if (mjmlCompilationErrors.length) { @@ -120,6 +110,8 @@ export const sendSmtp = async ({ }; } + logger.debug("MJML template compiled"); + const { plaintext: emailBodyPlaintext } = htmlToPlaintext(emailBodyHtml); if (!emailBodyPlaintext || !emailBodyPlaintext?.length) { @@ -129,6 +121,8 @@ export const sendSmtp = async ({ }; } + logger.debug("Email body converted to plaintext"); + const sendEmailSettings: SendMailArgs = { mailData: { text: emailBodyPlaintext, @@ -156,5 +150,5 @@ export const sendSmtp = async ({ if (smtpErrors?.length) { return { errors: smtpErrors }; } - logger.debug(response?.response); + logger.debug(response?.response, "Email sent"); }; diff --git a/apps/emails-and-messages/src/pages/api/webhooks/gift-card-sent.ts b/apps/emails-and-messages/src/pages/api/webhooks/gift-card-sent.ts index 15227b4..5e3cf5f 100644 --- a/apps/emails-and-messages/src/pages/api/webhooks/gift-card-sent.ts +++ b/apps/emails-and-messages/src/pages/api/webhooks/gift-card-sent.ts @@ -57,15 +57,15 @@ export const giftCardSentWebhook = new SaleorAsyncWebhook = async ( req, res, context ) => { - const logger = createLogger({ - webhook: giftCardSentWebhook.name, - }); - logger.debug("Webhook received"); const { payload, authData } = context; diff --git a/apps/emails-and-messages/src/pages/api/webhooks/invoice-sent.ts b/apps/emails-and-messages/src/pages/api/webhooks/invoice-sent.ts index 2e1fbc3..8c37ef4 100644 --- a/apps/emails-and-messages/src/pages/api/webhooks/invoice-sent.ts +++ b/apps/emails-and-messages/src/pages/api/webhooks/invoice-sent.ts @@ -43,15 +43,15 @@ export const invoiceSentWebhook = new SaleorAsyncWebhook = async ( req, res, context ) => { - const logger = createLogger({ - webhook: invoiceSentWebhook.name, - }); - logger.debug("Webhook received"); const { payload, authData } = context; diff --git a/apps/emails-and-messages/src/pages/api/webhooks/notify.ts b/apps/emails-and-messages/src/pages/api/webhooks/notify.ts index 6c6d93c..76046f1 100644 --- a/apps/emails-and-messages/src/pages/api/webhooks/notify.ts +++ b/apps/emails-and-messages/src/pages/api/webhooks/notify.ts @@ -4,6 +4,15 @@ import { createLogger, createGraphQLClient } from "@saleor/apps-shared"; import { sendEventMessages } from "../../../modules/event-handlers/send-event-messages"; import { MessageEventTypes } from "../../../modules/event-handlers/message-event-types"; +// Notify webhook event groups multiple event types under the one webhook. We need to map it to events recognized by the App +const notifyEventMapping: Record = { + account_confirmation: "ACCOUNT_CONFIRMATION", + account_delete: "ACCOUNT_DELETE", + account_password_reset: "ACCOUNT_PASSWORD_RESET", + account_change_email_request: "ACCOUNT_CHANGE_EMAIL_REQUEST", + account_change_email_confirm: "ACCOUNT_CHANGE_EMAIL_CONFIRM", +}; + /* * Notify event handles multiple event types which are recognized based on payload field `notify_event`. * Handler recognizes if event is one of the supported typed and sends appropriate message. @@ -62,11 +71,11 @@ export const notifyWebhook = new SaleorAsyncWebhook({ query: "{}", // We are using the default payload instead of subscription }); -const handler: NextWebhookApiHandler = async (req, res, context) => { - const logger = createLogger({ - webhook: notifyWebhook.name, - }); +const logger = createLogger({ + name: notifyWebhook.webhookPath, +}); +const handler: NextWebhookApiHandler = async (req, res, context) => { logger.debug("Webhook received"); const { payload, authData } = context; @@ -80,22 +89,12 @@ const handler: NextWebhookApiHandler = async (req, re .json({ error: "Email recipient has not been specified in the event payload." }); } - // Notify webhook event groups multiple event types under the one webhook. We need to map it to events recognized by the App - const notifyEventMapping: Record = { - account_confirmation: "ACCOUNT_CONFIRMATION", - account_delete: "ACCOUNT_DELETE", - account_password_reset: "ACCOUNT_PASSWORD_RESET", - account_change_email_request: "ACCOUNT_CHANGE_EMAIL_REQUEST", - account_change_email_confirm: "ACCOUNT_CHANGE_EMAIL_CONFIRM", - }; - const event = notifyEventMapping[payload.notify_event]; if (!event) { - logger.error(`The type of received notify event (${payload.notify_event}) is not supported.`); - return res - .status(200) - .json({ error: "Email recipient has not been specified in the event payload." }); + // NOTIFY webhook sends multiple events to the same endpoint. The app supports only a subset of them. + logger.debug(`The type of received notify event (${payload.notify_event}) is not supported.`); + return res.status(200).json({ message: `${payload.notify_event} event is not supported.` }); } const client = createGraphQLClient({ diff --git a/apps/emails-and-messages/src/pages/api/webhooks/order-cancelled.ts b/apps/emails-and-messages/src/pages/api/webhooks/order-cancelled.ts index 7a03231..f5cf61d 100644 --- a/apps/emails-and-messages/src/pages/api/webhooks/order-cancelled.ts +++ b/apps/emails-and-messages/src/pages/api/webhooks/order-cancelled.ts @@ -34,15 +34,15 @@ export const orderCancelledWebhook = new SaleorAsyncWebhook = async ( req, res, context ) => { - const logger = createLogger({ - webhook: orderCancelledWebhook.name, - }); - logger.debug("Webhook received"); const { payload, authData } = context; diff --git a/apps/emails-and-messages/src/pages/api/webhooks/order-confirmed.ts b/apps/emails-and-messages/src/pages/api/webhooks/order-confirmed.ts index ce1cfff..a1f1db7 100644 --- a/apps/emails-and-messages/src/pages/api/webhooks/order-confirmed.ts +++ b/apps/emails-and-messages/src/pages/api/webhooks/order-confirmed.ts @@ -35,15 +35,15 @@ export const orderConfirmedWebhook = new SaleorAsyncWebhook = async ( req, res, context ) => { - const logger = createLogger({ - webhook: orderConfirmedWebhook.name, - }); - logger.debug("Webhook received"); const { payload, authData } = context; diff --git a/apps/emails-and-messages/src/pages/api/webhooks/order-created.ts b/apps/emails-and-messages/src/pages/api/webhooks/order-created.ts index 161ba27..e64d62c 100644 --- a/apps/emails-and-messages/src/pages/api/webhooks/order-created.ts +++ b/apps/emails-and-messages/src/pages/api/webhooks/order-created.ts @@ -32,15 +32,15 @@ export const orderCreatedWebhook = new SaleorAsyncWebhook = async ( req, res, context ) => { - const logger = createLogger({ - webhook: orderCreatedWebhook.name, - }); - logger.debug("Webhook received"); const { payload, authData } = context; diff --git a/apps/emails-and-messages/src/pages/api/webhooks/order-fulfilled.ts b/apps/emails-and-messages/src/pages/api/webhooks/order-fulfilled.ts index cc4aa9a..fed70a0 100644 --- a/apps/emails-and-messages/src/pages/api/webhooks/order-fulfilled.ts +++ b/apps/emails-and-messages/src/pages/api/webhooks/order-fulfilled.ts @@ -35,15 +35,15 @@ export const orderFulfilledWebhook = new SaleorAsyncWebhook = async ( req, res, context ) => { - const logger = createLogger({ - webhook: orderFulfilledWebhook.name, - }); - logger.debug("Webhook received"); const { payload, authData } = context; diff --git a/apps/emails-and-messages/src/pages/api/webhooks/order-fully-paid.ts b/apps/emails-and-messages/src/pages/api/webhooks/order-fully-paid.ts index d63f24a..3f51447 100644 --- a/apps/emails-and-messages/src/pages/api/webhooks/order-fully-paid.ts +++ b/apps/emails-and-messages/src/pages/api/webhooks/order-fully-paid.ts @@ -35,15 +35,15 @@ export const orderFullyPaidWebhook = new SaleorAsyncWebhook = async ( req, res, context ) => { - const logger = createLogger({ - webhook: orderFullyPaidWebhook.name, - }); - logger.debug("Webhook received"); const { payload, authData } = context;