From 0141963cf06f56146b2807674d8f3d92bb8de169 Mon Sep 17 00:00:00 2001 From: JEONG KITAE Date: Tue, 20 Oct 2026 09:53:47 +0900 Subject: [PATCH] feat(common): filter logs with LOG_LEVEL Every service logged at info with no way to quiet it, which is noisy for large audiences. The shared logger now drops entries below LOG_LEVEL (info, warn, or error; default info). Unknown values keep everything, and browsers, which have no environment, log as before. Also moves createLogger's doc comment back onto it. --- CHANGELOG.md | 2 ++ README.ko.md | 4 ++++ README.md | 4 ++++ common/src/index.ts | 17 ++++++++++++++++- common/test/index.test.ts | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 60 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c3c69c..b0c2230 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ All notable changes to OpenStreamGrid are documented in this file. ### Added +- `LOG_LEVEL` (`info`, `warn`, or `error`) for every service, to drop log + entries below that level. - Optional Helm `PrometheusRule` (`tracker.metrics.prometheusRule`) that alerts when no tracker is up, peers keep reporting integrity failures, most P2P attempts fall back to the origin, credential refusals spike, or the event diff --git a/README.ko.md b/README.ko.md index 8c2531f..ff077d6 100644 --- a/README.ko.md +++ b/README.ko.md @@ -319,6 +319,10 @@ WebSocket의 `sessionToken` 쿼리로 사용합니다. ## 환경 변수 참조 +모든 서비스(Tracker, Origin, 사이드카 작성기, Node 피어)는 JSON 로그를 출력하며, +`LOG_LEVEL`(`info`, `warn`, `error`, 기본 `info`)을 지정하면 그보다 낮은 수준의 +로그를 생략합니다. + ### Tracker | 변수 | 기본값 | 설명 | diff --git a/README.md b/README.md index 755fbcf..0787073 100644 --- a/README.md +++ b/README.md @@ -330,6 +330,10 @@ codes, and origin/peer endpoints. ## Configuration +Every service (tracker, origin, sidecar writer, and Node peer) writes JSON log +lines and accepts `LOG_LEVEL` (`info`, `warn`, or `error`; default `info`) to +drop entries below that level. + ### Tracker | Variable | Default | Description | diff --git a/common/src/index.ts b/common/src/index.ts index fed8464..f75fb90 100644 --- a/common/src/index.ts +++ b/common/src/index.ts @@ -295,6 +295,20 @@ export interface Logger { type LogLevel = "info" | "warn" | "error"; +const LOG_LEVEL_RANK: Readonly> = { info: 0, warn: 1, error: 2 }; + +/** + * The lowest level written, from the `LOG_LEVEL` environment variable (`info`, + * `warn`, or `error`; default `info`). Browsers have no environment and log + * everything. + */ +const minimumLogLevel = (): LogLevel => { + const configured = ( + globalThis as { process?: { env?: Record } } + ).process?.env?.LOG_LEVEL?.trim().toLowerCase(); + return configured === "warn" || configured === "error" ? configured : "info"; +}; + const writeLog = ( service: string, level: LogLevel, @@ -302,6 +316,7 @@ const writeLog = ( context: LogContext = {}, error?: unknown, ): void => { + if (LOG_LEVEL_RANK[level] < LOG_LEVEL_RANK[minimumLogLevel()]) return; const entry = { ...context, timestamp: new Date().toISOString(), @@ -323,7 +338,6 @@ const writeLog = ( else console.log(serialized); }; -/** Creates a structured logger whose entries always identify their service. */ /** * HLS media segments that peers cache and share: MPEG-TS (`.ts`) and * CMAF/fMP4 (`.m4s`). Init segments and playlists always load from the origin. @@ -359,6 +373,7 @@ export const renditionDirectory = (pathname: string): string | undefined => export const jitteredDelay = (delayMs: number, random: () => number = Math.random): number => delayMs / 2 + random() * (delayMs / 2); +/** Creates a structured logger whose entries always identify their service. */ export const createLogger = (service: string): Logger => ({ info: (event, context) => writeLog(service, "info", event, context), warn: (event, context) => writeLog(service, "warn", event, context), diff --git a/common/test/index.test.ts b/common/test/index.test.ts index 9d8a6f4..7e821b7 100644 --- a/common/test/index.test.ts +++ b/common/test/index.test.ts @@ -152,3 +152,37 @@ test("rejects peer addresses that reach local or link-local endpoints", () => { assert.throws(() => validatePeerHttpBaseUrl("http://user:pass@peer-a:9090"), /credentials/); assert.throws(() => validatePeerHttpBaseUrl("http://peer-a:9090/?x=1"), /query or fragment/); }); + +test("drops log entries below LOG_LEVEL", (context) => { + const entries: string[] = []; + context.mock.method(console, "log", (entry: string) => entries.push(entry)); + context.mock.method(console, "warn", (entry: string) => entries.push(entry)); + context.mock.method(console, "error", (entry: string) => entries.push(entry)); + const previous = process.env.LOG_LEVEL; + context.after(() => { + if (previous === undefined) delete process.env.LOG_LEVEL; + else process.env.LOG_LEVEL = previous; + }); + const logger = createLogger("test-service"); + const levels = (): string[] => + entries.splice(0).map((entry) => (JSON.parse(entry) as { level: string }).level); + const logAll = (): void => { + logger.info("a"); + logger.warn("b"); + logger.error("c", undefined); + }; + + process.env.LOG_LEVEL = "warn"; + logAll(); + assert.deepEqual(levels(), ["warn", "error"]); + process.env.LOG_LEVEL = " ERROR "; + logAll(); + assert.deepEqual(levels(), ["error"]); + // Unknown values keep everything rather than silencing logs. + process.env.LOG_LEVEL = "verbose"; + logAll(); + assert.deepEqual(levels(), ["info", "warn", "error"]); + delete process.env.LOG_LEVEL; + logAll(); + assert.deepEqual(levels(), ["info", "warn", "error"]); +});