feat: implement centralized logging utility and replace console statements in push notification handling

This commit is contained in:
Nawaz Dhandala
2026-02-18 15:08:30 +00:00
parent a31f9d9d28
commit 87501d8d1b
4 changed files with 33 additions and 11 deletions

View File

@@ -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;

View File

@@ -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<void>((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,
);

View File

@@ -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<void> {
export async function requestPermissionsAndGetToken(): Promise<string | null> {
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<string | null> {
}
if (finalStatus !== "granted") {
console.warn(
logger.warn(
"[PushNotifications] Push notification permission not granted:",
finalStatus,
);
@@ -107,7 +108,7 @@ export async function requestPermissionsAndGetToken(): Promise<string | null> {
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<string | null> {
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;
}
}

View File

@@ -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;