From b23bf6574438e517b7922b5a94b6cc02bf4819ee Mon Sep 17 00:00:00 2001 From: "Samuel EF. Tinnerholm" Date: Sun, 24 May 2026 19:48:00 +0300 Subject: [PATCH] fix: replace console.warn with structured logger in TS SDK router Fixes #396 --- sdks/typescript/pmxt/logger.ts | 78 ++++++++++++++++++++++++++++++++++ sdks/typescript/pmxt/router.ts | 3 +- 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 sdks/typescript/pmxt/logger.ts diff --git a/sdks/typescript/pmxt/logger.ts b/sdks/typescript/pmxt/logger.ts new file mode 100644 index 00000000..d04b6ce3 --- /dev/null +++ b/sdks/typescript/pmxt/logger.ts @@ -0,0 +1,78 @@ +/** + * Structured logger for pmxt TypeScript SDK. + * + * Thin abstraction over console that: + * - Attaches a `[pmxt]` prefix for easy filtering + * - Supports log levels (debug, info, warn, error) + * - Respects a configurable level threshold via PMXT_LOG_LEVEL + * - Can be swapped for a real transport later + */ + +export type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent'; + +const LEVEL_PRIORITY: Record = { + debug: 0, + info: 1, + warn: 2, + error: 3, + silent: 4, +}; + +function getDefaultLevel(): LogLevel { + if (typeof process !== 'undefined' && process.env?.PMXT_LOG_LEVEL) { + return process.env.PMXT_LOG_LEVEL as LogLevel; + } + return 'info'; +} + +let currentLevel: LogLevel = getDefaultLevel(); + +function shouldLog(level: LogLevel): boolean { + return LEVEL_PRIORITY[level] >= LEVEL_PRIORITY[currentLevel]; +} + +export const logger = { + debug(message: string, context?: Record): void { + if (!shouldLog('debug')) return; + if (context) { + console.debug(`[pmxt] ${message}`, context); + } else { + console.debug(`[pmxt] ${message}`); + } + }, + + info(message: string, context?: Record): void { + if (!shouldLog('info')) return; + if (context) { + console.info(`[pmxt] ${message}`, context); + } else { + console.info(`[pmxt] ${message}`); + } + }, + + warn(message: string, context?: Record): void { + if (!shouldLog('warn')) return; + if (context) { + console.warn(`[pmxt] ${message}`, context); + } else { + console.warn(`[pmxt] ${message}`); + } + }, + + error(message: string, context?: Record): void { + if (!shouldLog('error')) return; + if (context) { + console.error(`[pmxt] ${message}`, context); + } else { + console.error(`[pmxt] ${message}`); + } + }, + + setLevel(level: LogLevel): void { + currentLevel = level; + }, + + getLevel(): LogLevel { + return currentLevel; + }, +}; diff --git a/sdks/typescript/pmxt/router.ts b/sdks/typescript/pmxt/router.ts index 669afebc..8edfd0f2 100644 --- a/sdks/typescript/pmxt/router.ts +++ b/sdks/typescript/pmxt/router.ts @@ -6,6 +6,7 @@ */ import { Exchange, ExchangeOptions } from "./client.js"; +import { logger } from "./logger.js"; import { MatchResult, MatchRelation, @@ -204,7 +205,7 @@ export class Router extends Exchange { limit?: number; includePrices?: boolean; } = {}): Promise { - console.warn('[pmxt] fetchMatches is deprecated, use fetchMarketMatches instead'); + logger.warn('fetchMatches is deprecated, use fetchMarketMatches instead'); return this.fetchMarketMatches(marketOrParams as any); }