react-native-fxview 1.0.2-beta3 → 1.0.2-beta4

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,281 @@
1
+ // ========== Logger 类型定义 ==========
2
+
3
+ /**
4
+ * 日志级别枚举
5
+ */
6
+ export enum FXLogLevel {
7
+ DEBUG = 0, // 调试级别(最详细)
8
+ INFO = 1, // 信息级别
9
+ WARN = 2, // 警告级别
10
+ ERROR = 3, // 错误级别
11
+ SILENT = 4, // 静默模式(不输出任何日志)
12
+ }
13
+
14
+ /**
15
+ * Logger 配置接口
16
+ */
17
+ export interface FXLoggerConfig {
18
+ debug: boolean; // 是否启用调试模式
19
+ level: FXLogLevel; // 日志级别
20
+ showTimestamp: boolean; // 是否显示时间戳
21
+ showTags: boolean; // 是否显示标签
22
+ prefix: string; // 日志前缀
23
+ }
24
+
25
+ /**
26
+ * FX Logger - 专为 React Native 项目设计的调试日志系统
27
+ * 提供丰富的日志功能和灵活的配置选项
28
+ */
29
+ export class FXLogger {
30
+ private static instance: FXLogger;
31
+ private config: FXLoggerConfig;
32
+
33
+ /**
34
+ * 私有构造函数,实现单例模式
35
+ */
36
+ private constructor() {
37
+ // 自动检测环境
38
+ const isDevelopment = this.isDevelopmentEnvironment();
39
+
40
+ // 根据环境设置默认配置
41
+ this.config = {
42
+ debug: isDevelopment, // 开发环境启用调试模式,生产环境禁用
43
+ level: isDevelopment ? FXLogLevel.DEBUG : FXLogLevel.ERROR, // 生产环境只显示错误日志
44
+ showTimestamp: true,
45
+ showTags: true,
46
+ prefix: isDevelopment ? "[FX-DEV]" : "[FX-PROD]", // 不同环境使用不同前缀
47
+ };
48
+ }
49
+
50
+ /**
51
+ * 获取单例实例
52
+ */
53
+ public static getInstance(): FXLogger {
54
+ if (!FXLogger.instance) {
55
+ FXLogger.instance = new FXLogger();
56
+ }
57
+ return FXLogger.instance;
58
+ }
59
+
60
+ /**
61
+ * 检测当前是否为开发环境
62
+ */
63
+ private isDevelopmentEnvironment(): boolean {
64
+ try {
65
+ // 使用 Function 构造函数来安全地访问全局变量,避免 TypeScript 错误
66
+ const getGlobal = new Function("return this")();
67
+
68
+ // 检测 React Native 的 __DEV__ 变量
69
+ if (typeof getGlobal.__DEV__ !== "undefined" && getGlobal.__DEV__) {
70
+ return true;
71
+ }
72
+
73
+ // 检测 Node.js 环境变量
74
+ if (
75
+ typeof getGlobal.process !== "undefined" &&
76
+ getGlobal.process.env &&
77
+ getGlobal.process.env.NODE_ENV === "development"
78
+ ) {
79
+ return true;
80
+ }
81
+
82
+ // 检测 window 对象(浏览器环境)
83
+ if (typeof getGlobal.window !== "undefined" && getGlobal.window.__DEV__) {
84
+ return true;
85
+ }
86
+
87
+ // 检测全局对象属性
88
+ if (getGlobal.NODE_ENV === "development") {
89
+ return true;
90
+ }
91
+ } catch (error) {
92
+ // 如果检测过程中出错,默认返回 false(生产环境)
93
+ this.error("FXLogger", "Environment detection failed", error);
94
+ }
95
+
96
+ // 默认返回 false(生产环境)
97
+ return false;
98
+ }
99
+
100
+ /**
101
+ * 获取当前配置
102
+ */
103
+ public getConfig(): FXLoggerConfig {
104
+ return { ...this.config };
105
+ }
106
+
107
+ /**
108
+ * 配置 Logger
109
+ */
110
+ public configure(config: Partial<FXLoggerConfig>): void {
111
+ this.config = { ...this.config, ...config };
112
+ }
113
+
114
+ /**
115
+ * 启用调试模式
116
+ */
117
+ public enableDebug(): void {
118
+ this.config.debug = true;
119
+ }
120
+
121
+ /**
122
+ * 禁用调试模式
123
+ */
124
+ public disableDebug(): void {
125
+ this.config.debug = false;
126
+ }
127
+
128
+ /**
129
+ * 获取时间戳(包含年月日)
130
+ */
131
+ private getTimestamp(): string {
132
+ const now = new Date();
133
+ const year = now.getFullYear();
134
+ const month = (now.getMonth() + 1).toString().padStart(2, "0");
135
+ const day = now.getDate().toString().padStart(2, "0");
136
+ const hours = now.getHours().toString().padStart(2, "0");
137
+ const minutes = now.getMinutes().toString().padStart(2, "0");
138
+ const seconds = now.getSeconds().toString().padStart(2, "0");
139
+ const milliseconds = now.getMilliseconds().toString().padStart(3, "0");
140
+ return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}.${milliseconds}`;
141
+ }
142
+
143
+ /**
144
+ * 检查是否应该记录日志
145
+ */
146
+ private shouldLog(level: FXLogLevel): boolean {
147
+ if (!this.config.debug && level < FXLogLevel.ERROR) {
148
+ return false;
149
+ }
150
+ return level >= this.config.level;
151
+ }
152
+
153
+ /**
154
+ * 获取日志级别的颜色符号
155
+ */
156
+ private getLevelColor(level: FXLogLevel): string {
157
+ switch (level) {
158
+ case FXLogLevel.DEBUG:
159
+ return "🔵"; // 蓝色 - 调试
160
+ case FXLogLevel.INFO:
161
+ return "🟢"; // 绿色 - 信息
162
+ case FXLogLevel.WARN:
163
+ return "🟡"; // 黄色 - 警告
164
+ case FXLogLevel.ERROR:
165
+ return "🔴"; // 红色 - 错误
166
+ default:
167
+ return "⚪"; // 默认白色
168
+ }
169
+ }
170
+
171
+ /**
172
+ * 格式化日志消息
173
+ */
174
+ private formatMessage(level: string, tag: string, message: string): string {
175
+ const parts: string[] = [];
176
+
177
+ if (this.config.prefix) {
178
+ parts.push(this.config.prefix);
179
+ }
180
+
181
+ if (this.config.showTimestamp) {
182
+ parts.push(`[${this.getTimestamp()}]`);
183
+ }
184
+
185
+ // 根据日志级别添加颜色符号
186
+ const levelEnum =
187
+ FXLogLevel[level.toUpperCase() as keyof typeof FXLogLevel];
188
+ const colorSymbol = this.getLevelColor(levelEnum);
189
+ parts.push(`[${colorSymbol}${level.toUpperCase()}]`);
190
+
191
+ if (this.config.showTags && tag) {
192
+ parts.push(`[${tag}]`);
193
+ }
194
+
195
+ return parts.join("") + " " + message;
196
+ }
197
+
198
+ /**
199
+ * 调试日志
200
+ */
201
+ debug(tag: string, message: string, ...args: any[]): void {
202
+ if (!this.shouldLog(FXLogLevel.DEBUG)) return;
203
+
204
+ const formattedMessage = this.formatMessage("debug", tag, message);
205
+ console.log(formattedMessage, ...args);
206
+ }
207
+
208
+ /**
209
+ * 信息日志
210
+ */
211
+ info(tag: string, message: string, ...args: any[]): void {
212
+ if (!this.shouldLog(FXLogLevel.INFO)) return;
213
+
214
+ const formattedMessage = this.formatMessage("info", tag, message);
215
+ console.info(formattedMessage, ...args);
216
+ }
217
+
218
+ /**
219
+ * 警告日志
220
+ */
221
+ warn(tag: string, message: string, ...args: any[]): void {
222
+ if (!this.shouldLog(FXLogLevel.WARN)) return;
223
+
224
+ const formattedMessage = this.formatMessage("warn", tag, message);
225
+ console.warn(formattedMessage, ...args);
226
+ }
227
+
228
+ /**
229
+ * 错误日志
230
+ */
231
+ error(tag: string, message: string, ...args: any[]): void {
232
+ if (!this.shouldLog(FXLogLevel.ERROR)) return;
233
+
234
+ const formattedMessage = this.formatMessage("error", tag, message);
235
+ console.error(formattedMessage, ...args);
236
+ }
237
+
238
+ /**
239
+ * 分组日志
240
+ */
241
+ group(tag: string, label?: string): void {
242
+ if (!this.shouldLog(FXLogLevel.DEBUG)) return;
243
+
244
+ const groupLabel = label ? `[${tag}] ${label}` : `[${tag}] Group`;
245
+ console.group(groupLabel);
246
+ }
247
+
248
+ /**
249
+ * 结束分组日志
250
+ */
251
+ groupEnd(): void {
252
+ if (!this.shouldLog(FXLogLevel.DEBUG)) return;
253
+
254
+ console.groupEnd();
255
+ }
256
+
257
+ /**
258
+ * 表格日志
259
+ */
260
+ table(tag: string, data: any): void {
261
+ if (!this.shouldLog(FXLogLevel.DEBUG)) return;
262
+
263
+ console.log(`[${tag}] Table data:`);
264
+ console.table(data);
265
+ }
266
+
267
+ /**
268
+ * 追踪日志
269
+ */
270
+ trace(tag: string, message: string): void {
271
+ if (!this.shouldLog(FXLogLevel.DEBUG)) return;
272
+
273
+ console.trace(`[${tag}] ${message}`);
274
+ }
275
+ }
276
+
277
+ // 创建全局实例
278
+ const logger = FXLogger.getInstance();
279
+
280
+ // 导出默认实例
281
+ export default logger;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-fxview",
3
- "version": "1.0.2-beta3",
3
+ "version": "1.0.2-beta4",
4
4
  "description": "A dynamic view component for React Native that allows runtime UI updates and component management",
5
5
  "main": "index.ts",
6
6
  "types": "index.d.ts",
@@ -12,6 +12,10 @@
12
12
  "queue/**/*.js",
13
13
  "queue/**/*.ts",
14
14
  "queue/**/*.d.ts",
15
+ "logger/**/*.js",
16
+ "logger/**/*.ts",
17
+ "logger/**/*.d.ts",
18
+ "logger/**/*.md",
15
19
  "README.md",
16
20
  "LICENSE"
17
21
  ],