Merged in chore/create-logger-tests (pull request #2996)

Tests for common/logger

* chore: add tests for createLogger

* chore: add tests for createLogger


Approved-by: Linus Flood
This commit is contained in:
Joakim Jäderberg
2025-10-24 13:28:56 +00:00
parent 3b3e7308cc
commit 108bb2b319
6 changed files with 53 additions and 23 deletions

View File

@@ -1,28 +1,10 @@
import * as Sentry from "@sentry/nextjs"
import { flatten } from "flat"
const logLevels = ["debug", "info", "warn", "error"] as const
const minimumLogLevel = (() => {
const configuredMinimumLogLevel = (
(process.env.MINIMUM_LOG_LEVEL ||
process.env.NEXT_PUBLIC_MINIMUM_LOG_LEVEL) ??
"info"
).toLowerCase() as (typeof logLevels)[number]
import { minimumLogLevel } from "./minimumLogLevel"
import { shouldLog } from "./shouldLog"
if (!logLevels.includes(configuredMinimumLogLevel)) {
console.warn(
`Invalid log level configured: ${configuredMinimumLogLevel}, defaulting to 'info'`
)
return "info"
}
return configuredMinimumLogLevel
})()
function shouldLog(level: (typeof logLevels)[number]) {
return logLevels.indexOf(level) >= logLevels.indexOf(minimumLogLevel)
}
import type { logLevels } from "./logLevels"
function getLogValue(args: unknown[]): Record<string, unknown> | undefined {
if (!args || args.length === 0) {
@@ -55,7 +37,7 @@ export function createLogger(loggerPrefix: string | (() => Promise<string>)) {
message: string,
...args: unknown[]
) {
if (!shouldLog(level)) {
if (!shouldLog(level, minimumLogLevel)) {
return
}

View File

@@ -0,0 +1,2 @@
export const logLevels = ["debug", "info", "warn", "error"] as const
export type LogLevel = (typeof logLevels)[number]

View File

@@ -0,0 +1,19 @@
import { logLevels } from "./logLevels"
export const minimumLogLevel = (() => {
const configuredMinimumLogLevel = (
(process.env.MINIMUM_LOG_LEVEL ||
process.env.NEXT_PUBLIC_MINIMUM_LOG_LEVEL) ??
"info"
).toLowerCase() as (typeof logLevels)[number]
if (!logLevels.includes(configuredMinimumLogLevel)) {
console.warn(
`Invalid log level configured: ${configuredMinimumLogLevel}, defaulting to 'info'`
)
return "info"
}
return configuredMinimumLogLevel
})()

View File

@@ -0,0 +1,22 @@
import { describe, expect, it } from "vitest"
import { shouldLog } from "./shouldLog"
describe("shouldLog", () => {
it("returns true when log level is higher or equal to minimum", () => {
expect(shouldLog("info", "debug")).toBe(true)
expect(shouldLog("info", "info")).toBe(true)
expect(shouldLog("warn", "info")).toBe(true)
expect(shouldLog("warn", "warn")).toBe(true)
expect(shouldLog("error", "error")).toBe(true)
})
it("returns false when log level is lower than minimum", () => {
expect(shouldLog("debug", "info")).toBe(false)
expect(shouldLog("debug", "warn")).toBe(false)
expect(shouldLog("debug", "error")).toBe(false)
expect(shouldLog("info", "warn")).toBe(false)
expect(shouldLog("info", "error")).toBe(false)
expect(shouldLog("warn", "error")).toBe(false)
})
})

View File

@@ -0,0 +1,5 @@
import { type LogLevel, logLevels } from "./logLevels"
export function shouldLog(level: LogLevel, minimumLogLevel: LogLevel) {
return logLevels.indexOf(level) >= logLevels.indexOf(minimumLogLevel)
}

View File

@@ -37,7 +37,7 @@
"./global.d.ts": "./global.d.ts",
"./hooks/*": "./hooks/*.ts",
"./logger": "./logger/index.ts",
"./logger/*": "./logger/*.ts",
"./logger/createLogger": "./logger/createLogger/index.ts",
"./stores/*": "./stores/*.ts",
"./telemetry": "./telemetry/index.ts",
"./tokenManager": "./tokenManager/index.ts",