diff --git a/MobileApp/src/api/pushDevice.ts b/MobileApp/src/api/pushDevice.ts index 9ff0f269e7..ab443d96f2 100644 --- a/MobileApp/src/api/pushDevice.ts +++ b/MobileApp/src/api/pushDevice.ts @@ -1,6 +1,7 @@ import { Platform } from "react-native"; import * as Device from "expo-device"; import apiClient from "./client"; +import logger from "../utils/logger"; export async function registerPushDevice(params: { deviceToken: string; @@ -20,7 +21,7 @@ export async function registerPushDevice(params: { deviceName: Device.modelName || "Unknown Device", projectId: params.projectId, }); - console.info( + logger.info( `[PushNotifications] Device registered successfully for project ${params.projectId}`, ); } catch (error: unknown) { @@ -33,14 +34,14 @@ export async function registerPushDevice(params: { // Treat "already registered" as success if (status === 400 && message.includes("already registered")) { - console.info( + logger.info( `[PushNotifications] Device already registered for project ${params.projectId}`, ); return; } // Log and re-throw other errors - console.error( + logger.error( `[PushNotifications] Registration failed (status=${status}): ${message}`, ); throw error; diff --git a/MobileApp/src/hooks/usePushNotifications.ts b/MobileApp/src/hooks/usePushNotifications.ts index 1e4a46f728..09949dd11e 100644 --- a/MobileApp/src/hooks/usePushNotifications.ts +++ b/MobileApp/src/hooks/usePushNotifications.ts @@ -15,6 +15,7 @@ import { registerPushDevice } from "../api/pushDevice"; import { useAuth } from "./useAuth"; import { useProject } from "./useProject"; import { PUSH_TOKEN_KEY } from "./pushTokenUtils"; +import logger from "../utils/logger"; const RETRY_DELAY_MS: number = 5000; const MAX_RETRIES: number = 3; @@ -58,7 +59,7 @@ export function usePushNotifications(navigationRef: unknown): void { if (!token && !cancelled) { attempt++; if (attempt < MAX_RETRIES) { - console.warn( + logger.warn( `[PushNotifications] Push token not available, retrying in ${RETRY_DELAY_MS}ms (attempt ${attempt}/${MAX_RETRIES})`, ); await new Promise((resolve: () => void): void => { @@ -70,7 +71,7 @@ export function usePushNotifications(navigationRef: unknown): void { if (!token || cancelled) { if (!token) { - console.warn( + logger.warn( "[PushNotifications] Could not obtain push token after all retries — device will not be registered", ); } @@ -90,7 +91,7 @@ export function usePushNotifications(navigationRef: unknown): void { projectId: project._id, }); } catch (error: unknown) { - console.warn( + logger.warn( `[PushNotifications] Failed to register device for project ${project._id}:`, error, ); @@ -99,7 +100,7 @@ export function usePushNotifications(navigationRef: unknown): void { }; register().catch((error: unknown): void => { - console.error( + logger.error( "[PushNotifications] Unexpected error during push registration:", error, ); diff --git a/MobileApp/src/notifications/setup.ts b/MobileApp/src/notifications/setup.ts index 004498db93..fb67d4ea9c 100644 --- a/MobileApp/src/notifications/setup.ts +++ b/MobileApp/src/notifications/setup.ts @@ -4,6 +4,7 @@ import * as Device from "expo-device"; import Constants from "expo-constants"; import { Platform } from "react-native"; import { PermissionStatus } from "expo-modules-core"; +import logger from "../utils/logger"; // Show notifications when app is in foreground Notifications.setNotificationHandler({ @@ -80,7 +81,7 @@ export async function setupNotificationCategories(): Promise { export async function requestPermissionsAndGetToken(): Promise { if (!Device.isDevice) { - console.warn( + logger.warn( "[PushNotifications] Not a physical device — skipping push token registration", ); return null; @@ -95,7 +96,7 @@ export async function requestPermissionsAndGetToken(): Promise { } if (finalStatus !== "granted") { - console.warn( + logger.warn( "[PushNotifications] Push notification permission not granted:", finalStatus, ); @@ -107,7 +108,7 @@ export async function requestPermissionsAndGetToken(): Promise { Constants.easConfig?.projectId; if (!projectId) { - console.warn( + logger.warn( "[PushNotifications] EAS project ID not found — cannot register for push notifications", ); return null; @@ -120,7 +121,7 @@ export async function requestPermissionsAndGetToken(): Promise { return tokenData.data; } catch (error: unknown) { - console.error("[PushNotifications] Failed to get push token:", error); + logger.error("[PushNotifications] Failed to get push token:", error); return null; } } diff --git a/MobileApp/src/utils/logger.ts b/MobileApp/src/utils/logger.ts new file mode 100644 index 0000000000..1a973956f1 --- /dev/null +++ b/MobileApp/src/utils/logger.ts @@ -0,0 +1,19 @@ +/* eslint-disable no-console */ + +const logger: { + info: (...args: unknown[]) => void; + warn: (...args: unknown[]) => void; + error: (...args: unknown[]) => void; +} = { + info: (...args: unknown[]): void => { + console.info(...args); + }, + warn: (...args: unknown[]): void => { + console.warn(...args); + }, + error: (...args: unknown[]): void => { + console.error(...args); + }, +}; + +export default logger;