diff --git a/src/internals/env.test.ts b/src/internals/env.test.ts new file mode 100644 index 00000000..b55ba915 --- /dev/null +++ b/src/internals/env.test.ts @@ -0,0 +1,21 @@ +import { parseEnv } from "@/internals/env.js"; +import { z } from "zod"; + +describe("Parsing ENV", () => { + afterEach(() => { + vi.unstubAllEnvs(); + }); + + it("Correctly parses a string", () => { + vi.stubEnv("LOG_LEVEL", "info"); + expect(parseEnv("LOG_LEVEL", z.string().min(1))).toBe("info"); + }); + + it("Correctly parses a boolean", () => { + vi.stubEnv("ENABLE_LOGGING", " true"); + expect(parseEnv.asBoolean("ENABLE_LOGGING")).toBe(true); + + vi.stubEnv("ENABLE_LOGGING", "false"); + expect(parseEnv.asBoolean("ENABLE_LOGGING")).toBe(false); + }); +}); diff --git a/src/internals/env.ts b/src/internals/env.ts index 594cae54..f6699445 100644 --- a/src/internals/env.ts +++ b/src/internals/env.ts @@ -34,6 +34,9 @@ export function parseEnv(key: string, schema: z.ZodType, defaultValue?: T) } return result.data; } +parseEnv.asBoolean = (key: string, fallback = false) => { + return parseEnv(key, z.string(), String(fallback)).trim().toLowerCase() === "true"; +}; export function hasEnv(key: string) { return getProp(process.env, [key]) !== undefined; diff --git a/src/logger/logger.ts b/src/logger/logger.ts index 840bb8e0..9d0fc9b1 100644 --- a/src/logger/logger.ts +++ b/src/logger/logger.ts @@ -128,7 +128,7 @@ export class Logger extends Serializable implements pinoLogger.BaseLogger { static get defaults(): Omit & { pretty: boolean } { return { name: undefined, - pretty: Boolean(parseEnv("BEE_FRAMEWORK_LOG_PRETTY", z.coerce.boolean().default(false))), + pretty: parseEnv.asBoolean("BEE_FRAMEWORK_LOG_PRETTY", false), bindings: {}, level: parseEnv( "BEE_FRAMEWORK_LOG_LEVEL", @@ -184,10 +184,7 @@ export class Logger extends Serializable implements pinoLogger.BaseLogger { errorKey: "error", colorize: true, sync: true, - singleLine: parseEnv( - "BEE_FRAMEWORK_LOG_SINGLE_LINE", - z.coerce.boolean().default(true), - ), + singleLine: parseEnv.asBoolean("BEE_FRAMEWORK_LOG_SINGLE_LINE", false), } as PrettyOptions, }, }),