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

@@ -1,14 +1,6 @@
export const cacheLogger = {
async debug(message: string, ...args: unknown[]): Promise<void> {
console.debug(`${await loggerPrefix()} ${message}`, ...args)
},
async warn(message: string, ...args: unknown[]): Promise<void> {
console.warn(`${await loggerPrefix()} Warning - ${message}`, ...args)
},
async error(message: string, ...args: unknown[]): Promise<void> {
console.error(`${await loggerPrefix()} Error - ${message}`, ...args)
},
}
import { createLogger } from "../logger/createLogger"
export const cacheLogger = createLogger(loggerPrefix)
async function loggerPrefix() {
const instancePrefix = await getCachePrefix()

View File

@@ -0,0 +1,44 @@
export function createLogger(loggerPrefix: string | (() => Promise<string>)) {
const asyncWrapper: () => Promise<string> =
typeof loggerPrefix === "string" ? async () => loggerPrefix : loggerPrefix
const getLoggerPrefix = async () => {
const prefix = await asyncWrapper()
if (!prefix) {
return ""
}
return `[${prefix}]`
}
return {
async debug(message: string, ...args: unknown[]): Promise<void> {
// TODO: Make this configurable
if (process.env.NODE_ENV !== "development") {
return
}
console.debug(
"\x1b[36m%s\x1b[0m",
`${await getLoggerPrefix()} ${message}`.trim(),
...args
)
},
async info(message: string, ...args: unknown[]): Promise<void> {
console.info(`${await getLoggerPrefix()} ${message}`.trim(), ...args)
},
async warn(message: string, ...args: unknown[]): Promise<void> {
console.warn(
`${await getLoggerPrefix()} [warn] - ${message}`.trim(),
...args
)
},
async error(message: string, ...args: unknown[]): Promise<void> {
console.error(
`${await getLoggerPrefix()} [error] - ${message}`.trim(),
...args
)
},
}
}

View File

@@ -0,0 +1,3 @@
import { createLogger } from "./createLogger"
export const logger = createLogger("")

View File

@@ -16,6 +16,8 @@
"./telemetry": "./telemetry/index.ts",
"./tokenManager": "./tokenManager/index.ts",
"./dt": "./dt/dt.ts",
"./logger": "./logger/index.ts",
"./logger/*": "./logger/*.ts",
"./utils/isEdge": "./utils/isEdge.ts",
"./utils/safeTry": "./utils/safeTry.ts",
"./utils/url": "./utils/url.ts",

View File

@@ -23,6 +23,8 @@ import {
mapValues,
} from "lodash-es"
import { logger } from "../logger"
import type { ZodError } from "zod"
type AttributesInput = Record<string, unknown>
@@ -88,7 +90,7 @@ export function isValidAttributeValue(value: unknown): value is AttributeValue {
* };
*
* const sanitized = sanitize(input);
* console.log(sanitized);
* logger.log(sanitized);
* // {
* // key1: "Example",
* // key2: 10,
@@ -162,7 +164,7 @@ export function createCounter(meterName: string, counterName: string) {
const finalAttrs = sanitize(mergedAttrs)
counter.add(1, finalAttrs)
console.info(`[${fullName}] start:`, finalAttrs)
logger.info(`[${fullName}] start:`, finalAttrs)
},
/**
@@ -175,7 +177,7 @@ export function createCounter(meterName: string, counterName: string) {
const finalAttrs = sanitize(mergedAttrs)
success.add(1, finalAttrs)
console.info(`[${fullName}] success:`, finalAttrs)
logger.info(`[${fullName}] success:`, finalAttrs)
},
/**
@@ -199,7 +201,7 @@ export function createCounter(meterName: string, counterName: string) {
const finalAttrs = sanitize(mergedAttrs)
fail.add(1, finalAttrs)
console.error(`[${fullName}] dataError:`, finalAttrs)
logger.error(`[${fullName}] dataError:`, finalAttrs)
},
/**
@@ -221,7 +223,7 @@ export function createCounter(meterName: string, counterName: string) {
const finalAttrs = sanitize(mergedAttrs)
fail.add(1, finalAttrs)
console.error(`[${fullName}] noDataError:`, finalAttrs)
logger.error(`[${fullName}] noDataError:`, finalAttrs)
},
/**
@@ -241,7 +243,7 @@ export function createCounter(meterName: string, counterName: string) {
const finalAttrs = sanitize(mergedAttrs)
fail.add(1, finalAttrs)
console.error(`[${fullName}] validationError:`, finalAttrs)
logger.error(`[${fullName}] validationError:`, finalAttrs)
},
/**
@@ -271,7 +273,7 @@ export function createCounter(meterName: string, counterName: string) {
const finalAttrs = sanitize(mergedAttrs)
fail.add(1, finalAttrs)
console.error(`[${fullName}] httpError:`, finalAttrs)
logger.error(`[${fullName}] httpError:`, finalAttrs)
},
/**
@@ -301,7 +303,7 @@ export function createCounter(meterName: string, counterName: string) {
const finalAttrs = sanitize(mergedAttrs)
fail.add(1, finalAttrs)
console.error(`[${fullName}] fail:`, finalAttrs)
logger.error(`[${fullName}] fail:`, finalAttrs)
},
}
},

View File

@@ -1,4 +1,4 @@
import { describe, expect, test } from "vitest"
import { beforeEach, describe, expect, test } from "vitest"
import { passwordValidator } from "./passwordValidator"
@@ -88,7 +88,6 @@ describe("Should validate password the same way as Curity", () => {
curityPasswordRegex.lastIndex = 0
})
test.each(testCases)("$description", ({ password }) => {
console.log(password)
const curityResult = curityPasswordRegex.test(password)
const zodResult = passwordValidator().safeParse(password)