From 3557f69743a8b1289238992bf46a0053381de1aa Mon Sep 17 00:00:00 2001 From: tecc Date: Sat, 9 Sep 2023 19:25:00 +0200 Subject: [PATCH] change(config): `LOG_LEVEL` environment variable, fixed erroneous port for default `HTTP_PUBLIC_URL` --- new/backend/example.env | 6 +++++- new/backend/src/config.ts | 5 ++++- new/backend/src/log.ts | 13 ++++++++++--- new/common/src/util.ts | 4 ++++ 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/new/backend/example.env b/new/backend/example.env index 699cff5a8..9aa1a2516 100644 --- a/new/backend/example.env +++ b/new/backend/example.env @@ -6,13 +6,17 @@ # "development", "dev": Development environment # "production", "prod": Production environment ENVIRONMENT=development +# -- By default, ENVIRONMENT dictates the log level. +# -- However, the log level can be set manually through LOG_LEVEL. +# -- See pino's log levels. They're conventional. This value is also case-insensitive. +# LOG_LEVEL= # -- Whether or not to allow API calls for unpublished (e.g. not-yet-ready) locales ALLOW_UNPUBLISHED_LOCALES=true # -- This base URL only refers to the backend. # -- This also means we don't need HOME_URL, since the API doesn't have a homepage per se. -HTTP_BASE_URL=http://localhost:5000 +HTTP_BASE_URL=http://localhost:4000 # -- These two variables configure the bind address. It defaults to 0.0.0.0:4000. # HTTP_HOST=0.0.0.0 # HTTP_PORT=4000 diff --git a/new/backend/src/config.ts b/new/backend/src/config.ts index f1577f826..956aa55c3 100644 --- a/new/backend/src/config.ts +++ b/new/backend/src/config.ts @@ -1,7 +1,8 @@ import "dotenv/config"; -import { identity, parseBool, parseIntOrThrow } from "@pronounspage/common/util"; +import { identity, parseBool, parseIntOrThrow, toLowerCase } from "@pronounspage/common/util"; import * as path from "node:path"; import * as fs from "node:fs"; +import type { log } from "#self/log"; export enum Environment { DEVELOPMENT = "dev", @@ -10,6 +11,7 @@ export enum Environment { export interface Config { environment: Environment; + logLevel?: typeof log["level"]; http: { baseUrl: string; host: string; @@ -73,6 +75,7 @@ export function loadConfigFromEnv(): Config { parseEnvironment, Environment.DEVELOPMENT, ), + logLevel: envVarOrDefault("LOG_LEVEL", toLowerCase, undefined), http: { baseUrl: envVarNotNull("HTTP_BASE_URL", identity), host: envVarOrDefault("HTTP_HOST", identity, "0.0.0.0"), diff --git a/new/backend/src/log.ts b/new/backend/src/log.ts index 62cb7473e..881883db9 100644 --- a/new/backend/src/log.ts +++ b/new/backend/src/log.ts @@ -18,12 +18,19 @@ const loggerConfigurations = { } satisfies Record; -let environment; +let environment, logLevel; try { - environment = getConfig().environment + const config = getConfig(); + environment = config.environment + logLevel = config.logLevel; } catch (e) { environment = Environment.DEVELOPMENT; } -export const log = pino(loggerConfigurations[environment]); + +const configuration = loggerConfigurations[environment]; +if (logLevel) { + configuration.level = logLevel; +} +export const log = pino(configuration); export default log; diff --git a/new/common/src/util.ts b/new/common/src/util.ts index 633c6e375..fa644f141 100644 --- a/new/common/src/util.ts +++ b/new/common/src/util.ts @@ -1,6 +1,10 @@ export function identity(value: T): T { return value; } +// This is unnecessarily typed but I don't really care, it works doesn't it? +export function toLowerCase(value: S): Lowercase { + return value.toLowerCase() as Lowercase +} export function parseIntOrThrow(value: string, radix?: number): number { if (radix != null && (radix < 2 || radix > 36)) {