diff --git a/api/src/server.ts b/api/src/server.ts index a999115bd53..30ef89dd842 100644 --- a/api/src/server.ts +++ b/api/src/server.ts @@ -4,7 +4,12 @@ // eslint-disable-next-line @typescript-eslint/triple-slash-reference /// import { build } from './app'; -import { FREECODECAMP_NODE_ENV, HOST, PORT } from './utils/env'; +import { + FREECODECAMP_NODE_ENV, + FCC_API_LOG_LEVEL, + HOST, + PORT +} from './utils/env'; const envToLogger = { development: { @@ -15,10 +20,9 @@ const envToLogger = { ignore: 'pid,hostname' } }, - level: 'debug' + level: FCC_API_LOG_LEVEL }, - // TODO: is this the right level for production or should we use 'error'? - production: { level: 'fatal' }, + production: { level: FCC_API_LOG_LEVEL }, test: undefined }; diff --git a/api/src/utils/env.ts b/api/src/utils/env.ts index 813df7a561c..99ed813f053 100644 --- a/api/src/utils/env.ts +++ b/api/src/utils/env.ts @@ -1,6 +1,7 @@ import assert from 'node:assert'; import path from 'node:path'; import { config } from 'dotenv'; +import { LogLevel } from 'fastify'; const envPath = path.resolve(__dirname, '../../../.env'); const { error } = config({ path: envPath }); @@ -57,6 +58,25 @@ assert.ok(process.env.SHOW_UPCOMING_CHANGES); assert.ok(process.env.MONGOHQ_URL); assert.ok(process.env.COOKIE_SECRET); +const LOG_LEVELS: LogLevel[] = [ + 'fatal', + 'error', + 'warn', + 'info', + 'debug', + 'trace', + 'silent' +] as const; + +function assertLogLevel(level: unknown): level is LogLevel { + return typeof level === 'string' && LOG_LEVELS.includes(level); +} + +assert.ok( + assertLogLevel(process.env.FCC_API_LOG_LEVEL), + `FCC_API_LOG_LEVEL must be one of ${LOG_LEVELS.join(',')}. Found ${process.env.FCC_API_LOG_LEVEL}` +); + if (process.env.FREECODECAMP_NODE_ENV !== 'development') { assert.ok(process.env.SES_ID); assert.ok(process.env.SES_SECRET); @@ -137,6 +157,7 @@ export const FCC_ENABLE_EXAM_ENVIRONMENT = process.env.FCC_ENABLE_EXAM_ENVIRONMENT === 'true'; export const FCC_ENABLE_SENTRY_ROUTES = process.env.FCC_ENABLE_SENTRY_ROUTES === 'true'; +export const FCC_API_LOG_LEVEL = process.env.FCC_API_LOG_LEVEL; export const SENTRY_DSN = process.env.SENTRY_DSN === 'dsn_from_sentry_dashboard' ? '' diff --git a/sample.env b/sample.env index 9eaca7eb8c6..0a80a69f83c 100644 --- a/sample.env +++ b/sample.env @@ -71,6 +71,7 @@ FCC_ENABLE_DEV_LOGIN_MODE=true FCC_ENABLE_SHADOW_CAPTURE=false FCC_ENABLE_EXAM_ENVIRONMENT=false FCC_ENABLE_SENTRY_ROUTES=false +FCC_API_LOG_LEVEL=debug # Email # use ses in production