From 00002a62d2903253c90e3c0da1760e5e1fbb9ebe Mon Sep 17 00:00:00 2001 From: Thomas Ruoff Date: Thu, 23 Jun 2022 21:36:43 +0200 Subject: [PATCH] improve logging? --- context/book.tsx | 3 ++- helpers/log.ts | 1 + helpers/mail.ts | 30 ++++++--------------- helpers/storage.ts | 3 ++- lib/googlecalendar.ts | 3 ++- next.conf.js | 6 +++-- package-lock.json | 39 +++++++++++++++++++++++++++ package.json | 1 + pages/_app.tsx | 1 + pages/admin/bookings/[uuid]/bill.tsx | 8 +++--- pages/admin/bookings/[uuid]/index.tsx | 3 ++- pages/api/bookings/[uuid]/bill.ts | 5 ++-- pages/api/bookings/[uuid]/index.ts | 3 ++- pages/api/bookings/index.ts | 12 ++++++--- pages/bookings/[uuid]/index.tsx | 23 ++++++++-------- 15 files changed, 91 insertions(+), 50 deletions(-) create mode 100644 helpers/log.ts diff --git a/context/book.tsx b/context/book.tsx index 4bdf3b0..45c8ba3 100644 --- a/context/book.tsx +++ b/context/book.tsx @@ -1,6 +1,7 @@ import React, { useEffect, useReducer } from 'react' import { useRouter } from 'next/router' import { clearBookingData, loadBookingData } from '../helpers/storage' +import { log } from '../helpers/log' import { createBooking } from '../helpers/booking' import { Booking } from '../db/booking' @@ -156,7 +157,7 @@ export default function BookProvider({ children }) { const booking = await createBooking(state.formData) router.push(`/bookings/${booking.uuid}/stored`) } catch (error) { - console.error(error) + log.error('Failed to store booking', error) dispatch({ type: ACTIONS.POST_DATA_ERROR, payload: error.message }) } } diff --git a/helpers/log.ts b/helpers/log.ts new file mode 100644 index 0000000..b98d747 --- /dev/null +++ b/helpers/log.ts @@ -0,0 +1 @@ +export { log } from 'next-axiom' diff --git a/helpers/mail.ts b/helpers/mail.ts index 554129b..f09865a 100644 --- a/helpers/mail.ts +++ b/helpers/mail.ts @@ -1,5 +1,6 @@ import { Booking } from '../db/booking' import { getBaseURL } from '../helpers/url' +import { log } from '../helpers/log' import { daysFormatFrontend } from './date' import { generateCalendarEntry } from './ical' import sgMail from '@sendgrid/mail' @@ -112,10 +113,7 @@ export async function sendReceivedBookingAdminMail( textPlainContent: getReceivedBookingAdminText(booking), }) } catch (error) { - console.error( - `Failed in sendReceivedBookingMail for ${booking.uuid}`, - error - ) + log.error(`Failed in sendReceivedBookingMail for ${booking.uuid}`, error) } } @@ -130,10 +128,7 @@ export async function sendReceivedBookingBookerMail( textPlainContent: getReceivedBookingBookerText(booking), }) } catch (error) { - console.error( - `Failed in sendReceivedBookingMail for ${booking.uuid}`, - error - ) + log.error(`Failed in sendReceivedBookingMail for ${booking.uuid}`, error) } } @@ -155,10 +150,7 @@ export async function sendBookingConfirmed(booking: Booking): Promise { ], }) } catch (error) { - console.error( - `Failed in sendBookingConfirmedMail for ${booking.uuid}`, - error - ) + log.error(`Failed in sendBookingConfirmedMail for ${booking.uuid}`, error) } } @@ -171,10 +163,7 @@ export async function sendBookingRejected(booking: Booking): Promise { textPlainContent: getBookingRejectedText(booking), }) } catch (error) { - console.error( - `Failed in sendBookingRejectedMail for ${booking.uuid}`, - error - ) + log.error(`Failed in sendBookingRejectedMail for ${booking.uuid}`, error) } } @@ -187,10 +176,7 @@ export async function sendBookingCanceled(booking: Booking): Promise { textPlainContent: getBookingCanceledText(booking), }) } catch (error) { - console.error( - `Failed in sendBookingCanceledMail for ${booking.uuid}`, - error - ) + log.error(`Failed in sendBookingCanceledMail for ${booking.uuid}`, error) } } @@ -222,10 +208,10 @@ async function sendMail({ try { await sgMail.send(data) } catch (error) { - console.error(error) + log.error(error) if (error.response) { - console.error(error.response.body) + log.error(error.response.body) } // TODO: stuff into DB if failed and retry later diff --git a/helpers/storage.ts b/helpers/storage.ts index 52dc19a..2992922 100644 --- a/helpers/storage.ts +++ b/helpers/storage.ts @@ -1,4 +1,5 @@ import { Booking } from '../db/booking' +import { log } from '../helpers/log' const BOOKING_DATA_KEY = 'pfadiBussleBookingData' @@ -25,7 +26,7 @@ export function loadBookingData() { try { result = JSON.parse(dataAsString) } catch (e) { - console.error(`localStorage ${BOOKING_DATA_KEY} has invalid data stored`) + log.error(`localStorage ${BOOKING_DATA_KEY} has invalid data stored`) } return result diff --git a/lib/googlecalendar.ts b/lib/googlecalendar.ts index f1d6f02..4de4b81 100644 --- a/lib/googlecalendar.ts +++ b/lib/googlecalendar.ts @@ -2,6 +2,7 @@ import { google } from 'googleapis' import { getBaseURL } from '../helpers/url' import { Booking } from '../db/booking' import { getDays } from '../helpers/date' +import { log } from '../helpers/log' const calendarId = process.env.GOOGLE_CALENDAR_ID let credentials: object @@ -9,7 +10,7 @@ let credentials: object try { credentials = JSON.parse(process.env.GOOGLE_SERVICE_ACCOUNT_KEY_JSON) } catch (error) { - console.error( + log.error( 'Unable to parse process.env.GOOGLE_SERVICE_ACCOUNT_KEY_JSON - invalid JSON?' ) throw error diff --git a/next.conf.js b/next.conf.js index b778475..fcd18af 100644 --- a/next.conf.js +++ b/next.conf.js @@ -1,3 +1,5 @@ -module.exports = { +const { withAxiom } = require('next-axiom') + +module.exports = withAxiom({ swcMinify: true, -} +}) diff --git a/package-lock.json b/package-lock.json index aa12f7b..10041f4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,6 +19,7 @@ "mongoose": "6.3.8", "next": "12.1.6", "next-auth": "4.6.1", + "next-axiom": "^0.8.0", "next-mdx-remote": "4.0.3", "nodemailer": "6.7.5", "react": "18.2.0", @@ -2726,6 +2727,14 @@ "url": "https://opencollective.com/core-js" } }, + "node_modules/cross-fetch": { + "version": "3.1.5", + "resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.1.5.tgz", + "integrity": "sha512-lvb1SBsI0Z7GDwmuid+mU3kWVBwTVUbe7S0H52yaaAdQOXq2YktTCZdlAcNKFzE6QtRz0snpw9bNiPeOIkkQvw==", + "dependencies": { + "node-fetch": "2.6.7" + } + }, "node_modules/cross-spawn": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", @@ -6768,6 +6777,20 @@ } } }, + "node_modules/next-axiom": { + "version": "0.8.0", + "resolved": "https://registry.npmjs.org/next-axiom/-/next-axiom-0.8.0.tgz", + "integrity": "sha512-lSgUHBhcSDR97uSoUVqrFiMIGuH47lR8x57oUzUn98r+aMo7FDgqcmNPtWkYZGATTrBfIt/ZmaCq6YBhNYuR/g==", + "dependencies": { + "cross-fetch": "^3.1.5" + }, + "engines": { + "node": ">=15" + }, + "peerDependencies": { + "next": "^12.1.4" + } + }, "node_modules/next-mdx-remote": { "version": "4.0.3", "resolved": "https://registry.npmjs.org/next-mdx-remote/-/next-mdx-remote-4.0.3.tgz", @@ -11266,6 +11289,14 @@ "integrity": "sha512-12VZfFIu+wyVbBebyHmRTuEE/tZrB4tJToWcwAMcsp3h4+sHR+fMJWbKpYiCRWlhFBq+KNyO8rIV9rTkeVmznQ==", "dev": true }, + "cross-fetch": { + "version": "3.1.5", + "resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.1.5.tgz", + "integrity": "sha512-lvb1SBsI0Z7GDwmuid+mU3kWVBwTVUbe7S0H52yaaAdQOXq2YktTCZdlAcNKFzE6QtRz0snpw9bNiPeOIkkQvw==", + "requires": { + "node-fetch": "2.6.7" + } + }, "cross-spawn": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", @@ -14119,6 +14150,14 @@ "uuid": "^8.3.2" } }, + "next-axiom": { + "version": "0.8.0", + "resolved": "https://registry.npmjs.org/next-axiom/-/next-axiom-0.8.0.tgz", + "integrity": "sha512-lSgUHBhcSDR97uSoUVqrFiMIGuH47lR8x57oUzUn98r+aMo7FDgqcmNPtWkYZGATTrBfIt/ZmaCq6YBhNYuR/g==", + "requires": { + "cross-fetch": "^3.1.5" + } + }, "next-mdx-remote": { "version": "4.0.3", "resolved": "https://registry.npmjs.org/next-mdx-remote/-/next-mdx-remote-4.0.3.tgz", diff --git a/package.json b/package.json index 0c432fd..b36914e 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "mongoose": "6.3.8", "next": "12.1.6", "next-auth": "4.6.1", + "next-axiom": "^0.8.0", "next-mdx-remote": "4.0.3", "nodemailer": "6.7.5", "react": "18.2.0", diff --git a/pages/_app.tsx b/pages/_app.tsx index 7d4ac12..488150d 100644 --- a/pages/_app.tsx +++ b/pages/_app.tsx @@ -1,3 +1,4 @@ +export { reportWebVitals } from 'next-axiom' import { useEffect } from 'react' import { useSession, signIn, SessionProvider } from 'next-auth/react' diff --git a/pages/admin/bookings/[uuid]/bill.tsx b/pages/admin/bookings/[uuid]/bill.tsx index f9bcbca..a10755c 100644 --- a/pages/admin/bookings/[uuid]/bill.tsx +++ b/pages/admin/bookings/[uuid]/bill.tsx @@ -6,6 +6,7 @@ import { Booking } from '../../../../db/booking' import { BILL_STATUS, MILAGE_TARIFS } from '../../../../db/enums' import { getMilageMax } from '../../../../db/index' import { daysFormatFrontend } from '../../../../helpers/date' +import { log } from '../../../../helpers/log' import { getBillTotal, createBill, patchBill } from '../../../../helpers/bill' import { getBookingStatus } from '../../../../helpers/booking' import { getServerSideBooking } from '../../../../lib/getServerSideProps' @@ -109,7 +110,7 @@ function BookingBillPage({ setBooking(booking) } catch (error) { setStoringError('Buchung konnte nicht gespeichert werden!') - console.error('Failed to store booking', error) + log.error('Failed to store booking', error) } setStoringInProgress(false) } @@ -197,9 +198,8 @@ function BookingBillPage({ > - - +
- -
- )} +
+ +
+ )} ) }