From 7dc6df2ad6d46263cdafa633f6018ca6e1c42c5a Mon Sep 17 00:00:00 2001 From: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com> Date: Wed, 29 Nov 2023 19:39:25 +0530 Subject: [PATCH] chore: improve logs (#12467) * chore: improve logs * fix: import * chore: use safe stringify * chore: add more logs to google calendar * chore: use this.log --------- Co-authored-by: sean-brydon <55134778+sean-brydon@users.noreply.github.com> --- packages/app-store/alby/lib/PaymentService.ts | 3 ++- .../googlecalendar/lib/CalendarService.ts | 23 +++++++++++++++---- .../app-store/paypal/lib/PaymentService.ts | 9 ++++++-- .../stripepayment/lib/PaymentService.ts | 10 ++++---- packages/core/videoClient.ts | 2 +- .../bookings/lib/handleBookingRequested.ts | 3 ++- .../bookings/lib/handleConfirmation.ts | 3 ++- packages/features/ee/payments/api/webhook.ts | 3 ++- .../features/webhooks/lib/scheduleTrigger.ts | 14 ++++++++--- packages/lib/redactError.ts | 3 ++- 10 files changed, 53 insertions(+), 20 deletions(-) diff --git a/packages/app-store/alby/lib/PaymentService.ts b/packages/app-store/alby/lib/PaymentService.ts index c29b08427a..99dc4c728c 100644 --- a/packages/app-store/alby/lib/PaymentService.ts +++ b/packages/app-store/alby/lib/PaymentService.ts @@ -5,6 +5,7 @@ import type z from "zod"; import { ErrorCode } from "@calcom/lib/errorCodes"; import logger from "@calcom/lib/logger"; +import { safeStringify } from "@calcom/lib/safeStringify"; import prisma from "@calcom/prisma"; import type { CalendarEvent } from "@calcom/types/Calendar"; import type { IAbstractPaymentService } from "@calcom/types/PaymentService"; @@ -87,7 +88,7 @@ export class PaymentService implements IAbstractPaymentService { } return paymentData; } catch (error) { - log.error("Alby: Payment could not be created", bookingId, JSON.stringify(error)); + log.error("Alby: Payment could not be created", bookingId, safeStringify(error)); throw new Error(ErrorCode.PaymentCreationFailure); } } diff --git a/packages/app-store/googlecalendar/lib/CalendarService.ts b/packages/app-store/googlecalendar/lib/CalendarService.ts index e01982378b..0ffc9a0442 100644 --- a/packages/app-store/googlecalendar/lib/CalendarService.ts +++ b/packages/app-store/googlecalendar/lib/CalendarService.ts @@ -9,6 +9,7 @@ import { getFeatureFlagMap } from "@calcom/features/flags/server/utils"; import { getLocation, getRichDescription } from "@calcom/lib/CalEventParser"; import type CalendarService from "@calcom/lib/CalendarService"; import logger from "@calcom/lib/logger"; +import { safeStringify } from "@calcom/lib/safeStringify"; import prisma from "@calcom/prisma"; import type { Calendar, @@ -106,6 +107,7 @@ export default class GoogleCalendarService implements Calendar { }); myGoogleAuth.setCredentials(googleCredentials); } catch (err) { + this.log.error("Error Refreshing Google Token", safeStringify(err)); let message; if (err instanceof Error) message = err.message; else message = String(err); @@ -251,7 +253,10 @@ export default class GoogleCalendarService implements Calendar { iCalUID: event.data.iCalUID, }; } catch (error) { - console.error("There was an error contacting google calendar service: ", error); + this.log.error( + "There was an error creating event in google calendar: ", + safeStringify({ error, selectedCalendar, credentialId }) + ); throw error; } } @@ -332,7 +337,10 @@ export default class GoogleCalendarService implements Calendar { } return evt?.data; } catch (error) { - console.error("There was an error contacting google calendar service: ", error); + this.log.error( + "There was an error updating event in google calendar: ", + safeStringify({ error, event, uid }) + ); throw error; } } @@ -354,6 +362,10 @@ export default class GoogleCalendarService implements Calendar { }); return event?.data; } catch (error) { + this.log.error( + "There was an error deleting event from google calendar: ", + safeStringify({ error, event, externalCalendarId }) + ); const err = error as GoogleCalError; /** * 410 is when an event is already deleted on the Google cal before on cal.com @@ -502,7 +514,10 @@ export default class GoogleCalendarService implements Calendar { return busyData; } } catch (error) { - this.log.error("There was an error contacting google calendar service: ", error); + this.log.error( + "There was an error getting availability from google calendar: ", + safeStringify({ error, selectedCalendars }) + ); throw error; } } @@ -524,7 +539,7 @@ export default class GoogleCalendarService implements Calendar { } satisfies IntegrationCalendar) ); } catch (error) { - this.log.error("There was an error contacting google calendar service: ", error); + this.log.error("There was an error getting calendars: ", safeStringify(error)); throw error; } } diff --git a/packages/app-store/paypal/lib/PaymentService.ts b/packages/app-store/paypal/lib/PaymentService.ts index 9b6d479d09..9ad68c6846 100644 --- a/packages/app-store/paypal/lib/PaymentService.ts +++ b/packages/app-store/paypal/lib/PaymentService.ts @@ -6,6 +6,7 @@ import Paypal from "@calcom/app-store/paypal/lib/Paypal"; import { WEBAPP_URL } from "@calcom/lib/constants"; import { ErrorCode } from "@calcom/lib/errorCodes"; import logger from "@calcom/lib/logger"; +import { safeStringify } from "@calcom/lib/safeStringify"; import prisma from "@calcom/prisma"; import type { CalendarEvent } from "@calcom/types/Calendar"; import type { IAbstractPaymentService } from "@calcom/types/PaymentService"; @@ -91,7 +92,7 @@ export class PaymentService implements IAbstractPaymentService { } return paymentData; } catch (error) { - log.error("Paypal: Payment could not be created for bookingId", bookingId); + log.error("Paypal: Payment could not be created for bookingId", bookingId, safeStringify(error)); throw new Error(ErrorCode.PaymentCreationFailure); } } @@ -170,7 +171,11 @@ export class PaymentService implements IAbstractPaymentService { } return paymentData; } catch (error) { - log.error("Paypal: Payment method could not be collected for bookingId", bookingId); + log.error( + "Paypal: Payment method could not be collected for bookingId", + bookingId, + safeStringify(error) + ); throw new Error("Paypal: Payment method could not be collected"); } } diff --git a/packages/app-store/stripepayment/lib/PaymentService.ts b/packages/app-store/stripepayment/lib/PaymentService.ts index ed8d1bb3e9..5218ac8180 100644 --- a/packages/app-store/stripepayment/lib/PaymentService.ts +++ b/packages/app-store/stripepayment/lib/PaymentService.ts @@ -7,6 +7,7 @@ import { sendAwaitingPaymentEmail } from "@calcom/emails"; import { ErrorCode } from "@calcom/lib/errorCodes"; import { getErrorFromUnknown } from "@calcom/lib/errors"; import logger from "@calcom/lib/logger"; +import { safeStringify } from "@calcom/lib/safeStringify"; import prisma from "@calcom/prisma"; import type { CalendarEvent } from "@calcom/types/Calendar"; import type { IAbstractPaymentService } from "@calcom/types/PaymentService"; @@ -132,8 +133,7 @@ export class PaymentService implements IAbstractPaymentService { } return paymentData; } catch (error) { - console.error(`Payment could not be created for bookingId ${bookingId}`, error); - log.error("Stripe: Payment could not be created", bookingId, JSON.stringify(error)); + log.error("Stripe: Payment could not be created", bookingId, safeStringify(error)); throw new Error("payment_not_created_error"); } } @@ -207,7 +207,7 @@ export class PaymentService implements IAbstractPaymentService { log.error( "Stripe: Payment method could not be collected for bookingId", bookingId, - JSON.stringify(error) + safeStringify(error) ); throw new Error("Stripe: Payment method could not be collected"); } @@ -286,7 +286,7 @@ export class PaymentService implements IAbstractPaymentService { return paymentData; } catch (error) { - log.error("Stripe: Could not charge card for payment", _bookingId, JSON.stringify(error)); + log.error("Stripe: Could not charge card for payment", _bookingId, safeStringify(error)); throw new Error(ErrorCode.ChargeCardFailure); } } @@ -378,7 +378,7 @@ export class PaymentService implements IAbstractPaymentService { await this.stripe.paymentIntents.cancel(payment.externalId, { stripeAccount }); return true; } catch (e) { - log.error("Stripe: Unable to delete Payment in stripe of paymentId", paymentId, JSON.stringify(e)); + log.error("Stripe: Unable to delete Payment in stripe of paymentId", paymentId, safeStringify(e)); return false; } } diff --git a/packages/core/videoClient.ts b/packages/core/videoClient.ts index a2585ab1dd..7bb3118949 100644 --- a/packages/core/videoClient.ts +++ b/packages/core/videoClient.ts @@ -144,7 +144,7 @@ const updateMeeting = async ( if (!updatedMeeting) { log.error( "updateMeeting failed", - JSON.stringify({ bookingRef, canCallUpdateMeeting, calEvent, credential }) + safeStringify({ bookingRef, canCallUpdateMeeting, calEvent, credential }) ); return { appName: credential.appId || "", diff --git a/packages/features/bookings/lib/handleBookingRequested.ts b/packages/features/bookings/lib/handleBookingRequested.ts index 1abe2f2042..397e7ba2c0 100644 --- a/packages/features/bookings/lib/handleBookingRequested.ts +++ b/packages/features/bookings/lib/handleBookingRequested.ts @@ -3,6 +3,7 @@ import { getWebhookPayloadForBooking } from "@calcom/features/bookings/lib/getWe import getWebhooks from "@calcom/features/webhooks/lib/getWebhooks"; import sendPayload from "@calcom/features/webhooks/lib/sendPayload"; import logger from "@calcom/lib/logger"; +import { safeStringify } from "@calcom/lib/safeStringify"; import { WebhookTriggerEvents } from "@calcom/prisma/enums"; import type { CalendarEvent } from "@calcom/types/Calendar"; @@ -65,6 +66,6 @@ export async function handleBookingRequested(args: { await Promise.all(promises); } catch (error) { // Silently fail - log.error(error); + log.error("Error in handleBookingRequested", safeStringify(error)); } } diff --git a/packages/features/bookings/lib/handleConfirmation.ts b/packages/features/bookings/lib/handleConfirmation.ts index 0db3b9c000..289e6f7148 100644 --- a/packages/features/bookings/lib/handleConfirmation.ts +++ b/packages/features/bookings/lib/handleConfirmation.ts @@ -10,6 +10,7 @@ import type { EventTypeInfo } from "@calcom/features/webhooks/lib/sendPayload"; import sendPayload from "@calcom/features/webhooks/lib/sendPayload"; import { getTeamIdFromEventType } from "@calcom/lib/getTeamIdFromEventType"; import logger from "@calcom/lib/logger"; +import { safeStringify } from "@calcom/lib/safeStringify"; import type { PrismaClient } from "@calcom/prisma"; import { BookingStatus, WebhookTriggerEvents } from "@calcom/prisma/enums"; import { EventTypeMetaDataSchema } from "@calcom/prisma/zod-utils"; @@ -65,7 +66,7 @@ export async function handleConfirmation(args: { message: "Booking failed", }; - log.error(`Booking ${user.username} failed`, JSON.stringify({ error, results })); + log.error(`Booking ${user.username} failed`, safeStringify({ error, results })); } else { if (results.length) { // TODO: Handle created event metadata more elegantly diff --git a/packages/features/ee/payments/api/webhook.ts b/packages/features/ee/payments/api/webhook.ts index 2e64d5a480..720b4c04dd 100644 --- a/packages/features/ee/payments/api/webhook.ts +++ b/packages/features/ee/payments/api/webhook.ts @@ -14,6 +14,7 @@ import { HttpError as HttpCode } from "@calcom/lib/http-error"; import logger from "@calcom/lib/logger"; import { getBooking } from "@calcom/lib/payment/getBooking"; import { handlePaymentSuccess } from "@calcom/lib/payment/handlePaymentSuccess"; +import { safeStringify } from "@calcom/lib/safeStringify"; import { prisma } from "@calcom/prisma"; import { BookingStatus } from "@calcom/prisma/enums"; @@ -38,7 +39,7 @@ export async function handleStripePaymentSuccess(event: Stripe.Event) { }); if (!payment?.bookingId) { - log.error(JSON.stringify(paymentIntent), JSON.stringify(payment)); + log.error("Stripe: Payment Not Found", safeStringify(paymentIntent), safeStringify(payment)); throw new HttpCode({ statusCode: 204, message: "Payment not found" }); } if (!payment?.bookingId) throw new HttpCode({ statusCode: 204, message: "Payment not found" }); diff --git a/packages/features/webhooks/lib/scheduleTrigger.ts b/packages/features/webhooks/lib/scheduleTrigger.ts index 5d78af9608..ab44805f21 100644 --- a/packages/features/webhooks/lib/scheduleTrigger.ts +++ b/packages/features/webhooks/lib/scheduleTrigger.ts @@ -4,6 +4,7 @@ import { v4 } from "uuid"; import { getHumanReadableLocationValue } from "@calcom/core/location"; import { getCalEventResponses } from "@calcom/features/bookings/lib/getCalEventResponses"; import logger from "@calcom/lib/logger"; +import { safeStringify } from "@calcom/lib/safeStringify"; import { getTranslation } from "@calcom/lib/server"; import prisma from "@calcom/prisma"; import type { ApiKey } from "@calcom/prisma/client"; @@ -75,7 +76,10 @@ export async function addSubscription({ const userId = appApiKey ? appApiKey.userId : account && !account.isTeam ? account.id : null; const teamId = appApiKey ? appApiKey.teamId : account && account.isTeam ? account.id : null; - log.error(`Error creating subscription for ${teamId ? `team ${teamId}` : `user ${userId}`}.`); + log.error( + `Error creating subscription for ${teamId ? `team ${teamId}` : `user ${userId}`}.`, + safeStringify(error) + ); } } @@ -157,7 +161,8 @@ export async function deleteSubscription({ log.error( `Error deleting subscription for user ${ teamId ? `team ${teamId}` : `userId ${userId}` - }, webhookId ${webhookId}` + }, webhookId ${webhookId}`, + safeStringify(err) ); } } @@ -260,7 +265,10 @@ export async function listBookings( const userId = appApiKey ? appApiKey.userId : account && !account.isTeam ? account.id : null; const teamId = appApiKey ? appApiKey.teamId : account && account.isTeam ? account.id : null; - log.error(`Error retrieving list of bookings for ${teamId ? `team ${teamId}` : `user ${userId}`}.`); + log.error( + `Error retrieving list of bookings for ${teamId ? `team ${teamId}` : `user ${userId}`}.`, + safeStringify(err) + ); } } diff --git a/packages/lib/redactError.ts b/packages/lib/redactError.ts index de9fd404b3..dc4d890ac3 100644 --- a/packages/lib/redactError.ts +++ b/packages/lib/redactError.ts @@ -1,6 +1,7 @@ import { Prisma } from "@prisma/client"; import logger from "@calcom/lib/logger"; +import { safeStringify } from "@calcom/lib/safeStringify"; const log = logger.getSubLogger({ prefix: [`[[redactError]`] }); @@ -19,7 +20,7 @@ export const redactError = (error: T) => { } log.debug("Type of Error: ", error.constructor); if (shouldRedact(error)) { - log.error("Error: ", JSON.stringify(error)); + log.error("Error: ", safeStringify(error)); return new Error("An error occured while querying the database."); } return error;