From 44fb5997a1bbd7b75d4b30de8e0be67d6335a437 Mon Sep 17 00:00:00 2001 From: Renato Alcara Date: Tue, 20 Jan 2026 17:30:55 -0300 Subject: [PATCH] fix(logger): avoid pino-pretty dependency --- src/Utils/logger.ts | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/Utils/logger.ts b/src/Utils/logger.ts index f9fcbbe1..24ff980c 100644 --- a/src/Utils/logger.ts +++ b/src/Utils/logger.ts @@ -5,7 +5,7 @@ * - BAILEYS_LOG: Enable/disable logging (default: true) * - BAILEYS_LOG_LEVEL: Log level - trace/debug/info/warn/error/fatal/silent (default: info) * - USE_STRUCTURED_LOGS: Use structured logger with advanced features (default: false) - * - LOG_FORMAT: Output format - 'json' or 'pretty' (default: json in production, pretty otherwise) + * - LOG_FORMAT: Output format - 'json' or 'pretty' (default: json) * - LOGGER_INFO: Enable info level (default: true) * - LOGGER_WARN: Enable warn level (default: true) * - LOGGER_ERROR: Enable error level (default: true) @@ -13,8 +13,11 @@ * @module Utils/logger */ +import { createRequire } from 'module' import P, { type Logger as PinoLogger } from 'pino' +const require = createRequire(import.meta.url) + export interface ILogger { level: string child(obj: Record): ILogger @@ -44,12 +47,10 @@ interface LoggerConfig { * Load logger configuration from environment */ function loadLoggerConfig(): LoggerConfig { - const isProduction = process.env.NODE_ENV === 'production' - return { enabled: process.env.BAILEYS_LOG !== 'false', level: process.env.BAILEYS_LOG_LEVEL || 'info', - format: (process.env.LOG_FORMAT as 'json' | 'pretty') || (isProduction ? 'json' : 'pretty'), + format: (process.env.LOG_FORMAT as 'json' | 'pretty') || 'json', useStructuredLogs: process.env.USE_STRUCTURED_LOGS === 'true', levelFilters: { info: process.env.LOGGER_INFO !== 'false', @@ -59,6 +60,15 @@ function loadLoggerConfig(): LoggerConfig { } } +function canUsePrettyTransport(): boolean { + try { + require.resolve('pino-pretty') + return true + } catch { + return false + } +} + /** * Create a filtered logger that respects LOGGER_INFO/WARN/ERROR settings */ @@ -117,8 +127,8 @@ function createLogger(): ILogger { timestamp: () => `,"time":"${new Date().toJSON()}"`, } - // Add pretty printing in non-production or when explicitly set - if (config.format === 'pretty') { + // Add pretty printing when explicitly set and available + if (config.format === 'pretty' && canUsePrettyTransport()) { pinoOptions.transport = { target: 'pino-pretty', options: {