diff --git a/backend/services/logging.ts b/backend/services/logging.ts new file mode 100644 index 0000000..5524d8c --- /dev/null +++ b/backend/services/logging.ts @@ -0,0 +1,65 @@ +export type LogLevel = 'debug' | 'info' | 'warn' | 'error'; + +const LOG_LEVEL_PRIORITY: Record = { + debug: 0, + info: 1, + warn: 2, + error: 3, +}; + +// Change this via env later +const CURRENT_LEVEL: LogLevel = __DEV__ ? 'debug' : 'info'; + +// Correlation ID generator (simple version) +const generateId = () => { + return Math.random().toString(36).substring(2) + Date.now().toString(36); +}; + +export interface LogContext { + [key: string]: any; + correlationId?: string; +} + +function shouldLog(level: LogLevel) { + return LOG_LEVEL_PRIORITY[level] >= LOG_LEVEL_PRIORITY[CURRENT_LEVEL]; +} + +function formatLog(level: LogLevel, message: string, context?: LogContext) { + return { + level, + message, + timestamp: new Date().toISOString(), + ...context, + }; +} + +function sendToConsole(logEntry: any) { + console.log(JSON.stringify(logEntry, null, 2)); +} + +// future: plug Sentry / API here +async function sendToRemote(_logEntry: any) { + // Example: + // await fetch("https://your-api/logs", { method: "POST", body: JSON.stringify(logEntry) }); +} + +function log(level: LogLevel, message: string, context?: LogContext) { + if (!shouldLog(level)) return; + + const logEntry = formatLog(level, message, context); + + sendToConsole(logEntry); + + if (level === 'error') { + sendToRemote(logEntry); + } +} + +export const logger = { + debug: (msg: string, ctx?: LogContext) => log('debug', msg, ctx), + info: (msg: string, ctx?: LogContext) => log('info', msg, ctx), + warn: (msg: string, ctx?: LogContext) => log('warn', msg, ctx), + error: (msg: string, ctx?: LogContext) => log('error', msg, ctx), + + createCorrelationId: generateId, +}; diff --git a/backend/tests/load/stress-wrapper.js b/backend/tests/load/stress-wrapper.js index f966087..bd1ad44 100644 --- a/backend/tests/load/stress-wrapper.js +++ b/backend/tests/load/stress-wrapper.js @@ -1,6 +1,6 @@ /** * Backend Load Test Wrapper - * This script can be used to trigger k6 tests from a backend context or + * This script can be used to trigger k6 tests from a backend context or * to provide a wrapper for internal stress testing of specific modules. */ @@ -8,7 +8,7 @@ const { spawn } = require('child_process'); function runLoadTest(scenario = 'subscription') { console.log(`Starting load test for scenario: ${scenario}...`); - + const k6 = spawn('k6', ['run', '-e', `SCENARIO=${scenario}`, '../../load-tests/run.js']); k6.stdout.on('data', (data) => {