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:
@@ -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()
|
||||
|
||||
44
packages/common/logger/createLogger.ts
Normal file
44
packages/common/logger/createLogger.ts
Normal 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
|
||||
)
|
||||
},
|
||||
}
|
||||
}
|
||||
3
packages/common/logger/index.ts
Normal file
3
packages/common/logger/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { createLogger } from "./createLogger"
|
||||
|
||||
export const logger = createLogger("")
|
||||
@@ -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",
|
||||
|
||||
@@ -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)
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user