Merged in feature/wrap-logging (pull request #2511)

Feature/wrap logging

* feat: change all logging to go through our own logger function so that we can control log levels

* move packages/trpc to using our own logger

* merge


Approved-by: Linus Flood
This commit is contained in:
Joakim Jäderberg
2025-07-03 12:37:04 +00:00
parent 7e32ed294d
commit daf765f3d5
110 changed files with 681 additions and 441 deletions

View File

@@ -4,6 +4,8 @@ import { cookies } from "next/headers"
import { v4 as uuidv4 } from "uuid"
import { z } from "zod"
import { createLogger } from "@scandic-hotels/common/logger/createLogger"
import { env } from "../../../../../../env/server"
import { protectedProcedure } from "../../../../../procedures"
import { getSasToken } from "../../getSasToken"
@@ -37,6 +39,7 @@ const failureSchema = z.object({
const outputSchema = z.union([successSchema, failureSchema])
const sasLogger = createLogger("SAS")
export const requestOtp = protectedProcedure
.output(outputSchema)
.mutation(async function () {
@@ -47,8 +50,8 @@ export const requestOtp = protectedProcedure
}
const tokenResponse = await fetchRequestOtp({ sasAuthToken })
console.log(
"[SAS] requestOtp",
sasLogger.debug(
"requestOtp",
tokenResponse.status,
tokenResponse.statusText
)
@@ -56,7 +59,7 @@ export const requestOtp = protectedProcedure
const body = await tokenResponse.json()
const parseResult = outputSchema.safeParse(body)
if (!parseResult.success) {
console.error("[SAS] requestOtp error", body)
sasLogger.error("requestOtp error", body)
if (!tokenResponse.ok) {
throw createError(body)
@@ -67,8 +70,8 @@ export const requestOtp = protectedProcedure
if (parseResult.data.status === "SENT") {
setSASOtpCookie(parseResult.data)
} else {
const sasRequestOtpErrorMessage = `[SAS] requestOtp did not return SENT status with body: ${JSON.stringify(body)}`
console.warn(sasRequestOtpErrorMessage)
const sasRequestOtpErrorMessage = `requestOtp did not return SENT status with body: ${JSON.stringify(body)}`
sasLogger.warn(sasRequestOtpErrorMessage)
Sentry.captureMessage(sasRequestOtpErrorMessage)
}
@@ -87,7 +90,7 @@ function createError(
| RequestOtpGeneralError
): TRPCError {
const errorInfo = parseSASRequestOtpError(errorBody)
console.error("[SAS] createError", errorInfo)
sasLogger.error("createError", errorInfo)
return new TRPCError({
code: "BAD_REQUEST",
cause: errorInfo,
@@ -97,7 +100,7 @@ function createError(
async function fetchRequestOtp({ sasAuthToken }: { sasAuthToken: string }) {
const endpoint = `${env.SAS_API_ENDPOINT}/api/scandic-partnership/customer/send-otp`
console.log("[SAS]: Requesting OTP")
sasLogger.debug("Requesting OTP")
return await fetch(endpoint, {
method: "POST",

View File

@@ -1,5 +1,7 @@
import { z } from "zod"
import { createLogger } from "@scandic-hotels/common/logger/createLogger"
export type RequestOtpResponseError = "TOO_MANY_REQUESTS" | "UNKNOWN"
const requestOtpGeneralError = z.enum([
@@ -39,14 +41,14 @@ const SAS_REQUEST_OTP_ERROR_CODES: {
} = {
TOO_MANY_REQUESTS: 10,
}
const sasLogger = createLogger("SAS")
const getErrorCodeByNumber = (number: number): RequestOtpResponseError => {
const v =
Object.entries(SAS_REQUEST_OTP_ERROR_CODES).find(
([_, value]) => value === number
)?.[0] ?? "UNKNOWN"
console.log("[SAS] getErrorCodeByNumber", number, v)
sasLogger.debug("getErrorCodeByNumber", number, v)
return v as RequestOtpResponseError
}