humanbehavior-js 0.0.8 → 0.1.0
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.
- package/dist/cjs/index.js +558 -184
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/react/index.js +197 -86
- package/dist/cjs/react/index.js.map +1 -1
- package/dist/esm/index.js +554 -185
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/react/index.js +195 -87
- package/dist/esm/react/index.js.map +1 -1
- package/dist/index.min.js +2 -2
- package/dist/index.min.js.map +1 -1
- package/dist/types/index.d.ts +115 -10
- package/dist/types/react/index.d.ts +8 -9
- package/package.json +3 -2
- package/readme.md +127 -105
- package/simple-spa.html +544 -0
- package/src/api.ts +9 -137
- package/src/index.ts +3 -0
- package/src/react/index.tsx +137 -81
- package/src/redact.ts +19 -17
- package/src/tracker.ts +498 -62
- package/src/utils/logger.ts +144 -0
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
export enum LogLevel {
|
|
2
|
+
NONE = 0,
|
|
3
|
+
ERROR = 1,
|
|
4
|
+
WARN = 2,
|
|
5
|
+
INFO = 3,
|
|
6
|
+
DEBUG = 4
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface LoggerConfig {
|
|
10
|
+
level: LogLevel;
|
|
11
|
+
enableConsole: boolean;
|
|
12
|
+
enableStorage: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
class Logger {
|
|
16
|
+
private config: LoggerConfig = {
|
|
17
|
+
level: LogLevel.ERROR, // Default to only errors in production
|
|
18
|
+
enableConsole: true,
|
|
19
|
+
enableStorage: false
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
private isBrowser = typeof window !== 'undefined';
|
|
23
|
+
|
|
24
|
+
constructor(config?: Partial<LoggerConfig>) {
|
|
25
|
+
if (config) {
|
|
26
|
+
this.config = { ...this.config, ...config };
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
setConfig(config: Partial<LoggerConfig>): void {
|
|
31
|
+
this.config = { ...this.config, ...config };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
private shouldLog(level: LogLevel): boolean {
|
|
35
|
+
return level <= this.config.level;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
private formatMessage(level: string, message: string, ...args: any[]): string {
|
|
39
|
+
const timestamp = new Date().toISOString();
|
|
40
|
+
return `[HumanBehavior ${level}] ${timestamp}: ${message}`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
error(message: string, ...args: any[]): void {
|
|
44
|
+
if (!this.shouldLog(LogLevel.ERROR)) return;
|
|
45
|
+
|
|
46
|
+
const formattedMessage = this.formatMessage('ERROR', message);
|
|
47
|
+
|
|
48
|
+
if (this.config.enableConsole) {
|
|
49
|
+
console.error(formattedMessage, ...args);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (this.config.enableStorage && this.isBrowser) {
|
|
53
|
+
this.logToStorage(formattedMessage, args);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
warn(message: string, ...args: any[]): void {
|
|
58
|
+
if (!this.shouldLog(LogLevel.WARN)) return;
|
|
59
|
+
|
|
60
|
+
const formattedMessage = this.formatMessage('WARN', message);
|
|
61
|
+
|
|
62
|
+
if (this.config.enableConsole) {
|
|
63
|
+
console.warn(formattedMessage, ...args);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (this.config.enableStorage && this.isBrowser) {
|
|
67
|
+
this.logToStorage(formattedMessage, args);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
info(message: string, ...args: any[]): void {
|
|
72
|
+
if (!this.shouldLog(LogLevel.INFO)) return;
|
|
73
|
+
|
|
74
|
+
const formattedMessage = this.formatMessage('INFO', message);
|
|
75
|
+
|
|
76
|
+
if (this.config.enableConsole) {
|
|
77
|
+
console.log(formattedMessage, ...args);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (this.config.enableStorage && this.isBrowser) {
|
|
81
|
+
this.logToStorage(formattedMessage, args);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
debug(message: string, ...args: any[]): void {
|
|
86
|
+
if (!this.shouldLog(LogLevel.DEBUG)) return;
|
|
87
|
+
|
|
88
|
+
const formattedMessage = this.formatMessage('DEBUG', message);
|
|
89
|
+
|
|
90
|
+
if (this.config.enableConsole) {
|
|
91
|
+
console.log(formattedMessage, ...args);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (this.config.enableStorage && this.isBrowser) {
|
|
95
|
+
this.logToStorage(formattedMessage, args);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private logToStorage(message: string, args: any[]): void {
|
|
100
|
+
try {
|
|
101
|
+
const logs = JSON.parse(localStorage.getItem('human_behavior_logs') || '[]');
|
|
102
|
+
const logEntry = {
|
|
103
|
+
message,
|
|
104
|
+
args: args.length > 0 ? args : undefined,
|
|
105
|
+
timestamp: Date.now()
|
|
106
|
+
};
|
|
107
|
+
logs.push(logEntry);
|
|
108
|
+
|
|
109
|
+
// Keep only last 1000 logs to prevent storage bloat
|
|
110
|
+
if (logs.length > 1000) {
|
|
111
|
+
logs.splice(0, logs.length - 1000);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
localStorage.setItem('human_behavior_logs', JSON.stringify(logs));
|
|
115
|
+
} catch (e) {
|
|
116
|
+
// Silently fail if storage is not available
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
getLogs(): any[] {
|
|
121
|
+
if (!this.isBrowser) return [];
|
|
122
|
+
|
|
123
|
+
try {
|
|
124
|
+
return JSON.parse(localStorage.getItem('human_behavior_logs') || '[]');
|
|
125
|
+
} catch (e) {
|
|
126
|
+
return [];
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
clearLogs(): void {
|
|
131
|
+
if (this.isBrowser) {
|
|
132
|
+
localStorage.removeItem('human_behavior_logs');
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Create singleton instance
|
|
138
|
+
export const logger = new Logger();
|
|
139
|
+
|
|
140
|
+
// Export convenience methods
|
|
141
|
+
export const logError = (message: string, ...args: any[]) => logger.error(message, ...args);
|
|
142
|
+
export const logWarn = (message: string, ...args: any[]) => logger.warn(message, ...args);
|
|
143
|
+
export const logInfo = (message: string, ...args: any[]) => logger.info(message, ...args);
|
|
144
|
+
export const logDebug = (message: string, ...args: any[]) => logger.debug(message, ...args);
|