From 5f7c098f0b0bb9d92529eb835521c775abcaa511 Mon Sep 17 00:00:00 2001 From: ZOMBIIIIIII <120676065+Metaton241@users.noreply.github.com> Date: Wed, 15 Apr 2026 13:37:02 +0300 Subject: [PATCH] chore: prepare production deploy bundle --- apps/api/src/lib/logger.ts | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/apps/api/src/lib/logger.ts b/apps/api/src/lib/logger.ts index 1b011ea..8d46698 100644 --- a/apps/api/src/lib/logger.ts +++ b/apps/api/src/lib/logger.ts @@ -1,21 +1,38 @@ +import path from 'path'; +import dotenv from 'dotenv'; import { generateUlid } from '../utils/ulid'; import { getTraceId } from './trace-store'; +// Load .env so LOG_LEVEL is available even if env.ts hasn't been imported yet +dotenv.config({ path: path.resolve(__dirname, '../../../../.env') }); + const instanceId = generateUlid(); +const LEVELS: Record = { DEBUG: 10, INFO: 20, WARN: 30, ERROR: 40 }; +const LOG_LEVEL = (process.env.LOG_LEVEL || 'INFO').toUpperCase(); +const MIN_LEVEL = LEVELS[LOG_LEVEL] ?? LEVELS.INFO; + +const LOGGER_FILE_MARKER = `${path.sep}lib${path.sep}logger.`; + function getCallerInfo(): { file: string; line: number } { const stack = new Error().stack; if (!stack) return { file: 'unknown', line: 0 }; const lines = stack.split('\n'); - // Skip: Error, logger method, actual caller - const callerLine = lines[3] || ''; - const match = callerLine.match(/\((.+):(\d+):\d+\)/) || callerLine.match(/at (.+):(\d+):\d+/); - if (match) return { file: match[1], line: parseInt(match[2]) }; + // Skip frames that are inside logger.ts / logger.js; first non-logger frame is the caller. + for (let i = 1; i < lines.length; i++) { + const line = lines[i]; + if (line.includes(LOGGER_FILE_MARKER) || line.includes('/lib/logger.')) continue; + + const match = line.match(/\((.+):(\d+):\d+\)/) || line.match(/at (.+?):(\d+):\d+/); + if (match) return { file: match[1], line: parseInt(match[2], 10) }; + } return { file: 'unknown', line: 0 }; } function log(level: string, message: string): void { + if ((LEVELS[level] ?? 0) < MIN_LEVEL) return; + const caller = getCallerInfo(); const entry = { timestamp: new Date().toISOString(), @@ -31,8 +48,9 @@ function log(level: string, message: string): void { export const logger = { instanceId, + level: LOG_LEVEL, + debug: (msg: string) => log('DEBUG', msg), info: (msg: string) => log('INFO', msg), warn: (msg: string) => log('WARN', msg), error: (msg: string) => log('ERROR', msg), - debug: (msg: string) => log('DEBUG', msg), };