// Copyright 2026 Tether Operations Limited // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /** * Logger utility for development and production * * Provides controlled logging that can be disabled in production * to improve performance and prevent information leakage. */ import { sanitizeErrorMessage } from './errorUtils' /** * Check if we're in development mode * React Native sets __DEV__ to true in development builds */ const isDevelopment = typeof __DEV__ !== 'undefined' ? __DEV__ : process.env.NODE_ENV !== 'production' /** * Sanitize an error for logging to prevent sensitive data leakage * Handles Error objects, strings, and arbitrary objects */ function sanitizeErrorForLogging(error: unknown): string { if (error instanceof Error) { return sanitizeErrorMessage(error.message, isDevelopment) } if (typeof error === 'string') { return sanitizeErrorMessage(error, isDevelopment) } // For objects, stringify and sanitize try { const stringified = JSON.stringify(error, null, 2) return sanitizeErrorMessage(stringified, isDevelopment) } catch { return '[Error object - could not stringify]' } } /** * Log a message (only in development) * * @param message - Message to log * @param args - Additional arguments to log */ export function log(...args: unknown[]): void { if (isDevelopment) { console.log(...args) } } /** * Log an error (always logged, but sanitized to prevent sensitive data leakage) * @param message - Error message * @param error - Error object or additional data */ export function logError(message: string, error?: unknown): void { const sanitizedMessage = sanitizeErrorMessage(message, isDevelopment) const sanitizedError = error !== undefined ? sanitizeErrorForLogging(error) : undefined if (isDevelopment) { console.error(sanitizedMessage, sanitizedError) } else { // In production, log sanitized errors only // This prevents information leakage while still allowing error tracking console.error(sanitizedMessage, sanitizedError) } } /** * Log a warning (only in development) * * @param message - Warning message * @param args - Additional arguments to log */ export function logWarn(...args: unknown[]): void { if (isDevelopment) { console.warn(...args) } }