@umituz/react-native-subscription 3.1.8 → 3.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,140 @@
1
+ /**
2
+ * Centralized logging utilities for development-only logging.
3
+ *
4
+ * Benefits:
5
+ * - Removes 8+ duplicated __DEV__ check patterns
6
+ * - Single source for logging configuration
7
+ * - Easier to add log aggregation later
8
+ * - Consistent formatting across all logs
9
+ */
10
+
11
+ /**
12
+ * Log level for categorizing log messages.
13
+ */
14
+ export enum LogLevel {
15
+ DEBUG = "DEBUG",
16
+ INFO = "INFO",
17
+ WARN = "WARN",
18
+ ERROR = "ERROR",
19
+ }
20
+
21
+ /**
22
+ * Structured log context for better debugging.
23
+ */
24
+ export interface LogContext {
25
+ /** Additional key-value pairs to include in the log */
26
+ [key: string]: unknown;
27
+ }
28
+
29
+ /**
30
+ * Internal logging function that only logs in __DEV__ mode.
31
+ */
32
+ function log(
33
+ level: LogLevel,
34
+ tag: string,
35
+ message: string,
36
+ context?: LogContext
37
+ ): void {
38
+ if (typeof __DEV__ === "undefined" || !__DEV__) {
39
+ return;
40
+ }
41
+
42
+ const timestamp = new Date().toISOString();
43
+ const logData = {
44
+ timestamp,
45
+ level,
46
+ tag,
47
+ message,
48
+ ...context,
49
+ };
50
+
51
+ switch (level) {
52
+ case LogLevel.DEBUG:
53
+ case LogLevel.INFO:
54
+ console.log(`[${tag}]`, message, context ? logData : "");
55
+ break;
56
+ case LogLevel.WARN:
57
+ console.warn(`[${tag}]`, message, context ? logData : "");
58
+ break;
59
+ case LogLevel.ERROR:
60
+ console.error(`[${tag}]`, message, context ? logData : "");
61
+ break;
62
+ }
63
+ }
64
+
65
+ /**
66
+ * Log a debug message. Only shown in __DEV__ mode.
67
+ *
68
+ * @param tag - Component or feature name for filtering
69
+ * @param message - Log message
70
+ * @param context - Optional additional context data
71
+ */
72
+ export function logDebug(tag: string, message: string, context?: LogContext): void {
73
+ log(LogLevel.DEBUG, tag, message, context);
74
+ }
75
+
76
+ /**
77
+ * Log an info message. Only shown in __DEV__ mode.
78
+ *
79
+ * @param tag - Component or feature name for filtering
80
+ * @param message - Log message
81
+ * @param context - Optional additional context data
82
+ */
83
+ export function logInfo(tag: string, message: string, context?: LogContext): void {
84
+ log(LogLevel.INFO, tag, message, context);
85
+ }
86
+
87
+ /**
88
+ * Log a warning message. Only shown in __DEV__ mode.
89
+ *
90
+ * @param tag - Component or feature name for filtering
91
+ * @param message - Warning message
92
+ * @param context - Optional additional context data
93
+ */
94
+ export function logWarn(tag: string, message: string, context?: LogContext): void {
95
+ log(LogLevel.WARN, tag, message, context);
96
+ }
97
+
98
+ /**
99
+ * Log an error message. Only shown in __DEV__ mode.
100
+ *
101
+ * @param tag - Component or feature name for filtering
102
+ * @param message - Error message
103
+ * @param error - Error object (will be serialized)
104
+ * @param context - Optional additional context data
105
+ */
106
+ export function logError(
107
+ tag: string,
108
+ message: string,
109
+ error?: Error | unknown,
110
+ context?: LogContext
111
+ ): void {
112
+ const errorContext = {
113
+ ...context,
114
+ error: error instanceof Error ? {
115
+ name: error.name,
116
+ message: error.message,
117
+ stack: error.stack,
118
+ } : error,
119
+ };
120
+ log(LogLevel.ERROR, tag, message, errorContext);
121
+ }
122
+
123
+ /**
124
+ * Create a tagged logger for a specific component or feature.
125
+ * Returns functions that automatically include the tag.
126
+ *
127
+ * @example
128
+ * const logger = createLogger("useCredits");
129
+ * logger.info("Credits loaded", { credits: 100 });
130
+ * logger.error("Failed to load", error);
131
+ */
132
+ export function createLogger(tag: string) {
133
+ return {
134
+ debug: (message: string, context?: LogContext) => logDebug(tag, message, context),
135
+ info: (message: string, context?: LogContext) => logInfo(tag, message, context),
136
+ warn: (message: string, context?: LogContext) => logWarn(tag, message, context),
137
+ error: (message: string, error?: Error | unknown, context?: LogContext) =>
138
+ logError(tag, message, error, context),
139
+ };
140
+ }