@moonwatch/js 0.1.8

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,132 @@
1
+ type LogLevel = 'DEBUG' | 'INFO' | 'WARN' | 'ERROR' | 'FATAL';
2
+ interface LoggerConfig {
3
+ logId: string;
4
+ apiKey?: string;
5
+ group?: string;
6
+ /** Only send logs at this level and above to the remote service (default: 'DEBUG' — sends everything) */
7
+ level?: LogLevel;
8
+ onError?: (error: Error, logs: InternalLogEntry[]) => void;
9
+ traceId?: string;
10
+ /** When true, skip console output from logger.* methods; default false */
11
+ silent?: boolean;
12
+ /** When true, log SDK internal operations to console for troubleshooting */
13
+ debug?: boolean;
14
+ /** @internal Override base URL for development (e.g. 'http://localhost:3500') */
15
+ _endpoint?: string;
16
+ /** @internal Override WebSocket base URL for development (e.g. 'ws://localhost:3501') */
17
+ _wsEndpoint?: string;
18
+ /** @internal Full URL for log ingestion proxy (e.g. '/api/internal/ingest'). When set, uses this as the POST URL directly (no /ingest/http appended), skips WebSocket. */
19
+ _ingestUrl?: string;
20
+ }
21
+ interface InternalLogEntry {
22
+ timestamp: string;
23
+ level: LogLevel;
24
+ message: string;
25
+ logId: string;
26
+ group?: string;
27
+ trace_id?: string;
28
+ stack?: string;
29
+ error_type?: string;
30
+ release_id?: string;
31
+ watcher_id?: string;
32
+ metadata?: Record<string, unknown>;
33
+ }
34
+ /** Object form for logger.info({ message, group?, metadata? }) */
35
+ interface LogEntryObject {
36
+ message: string;
37
+ group?: string;
38
+ traceId?: string;
39
+ watcherId?: string;
40
+ metadata?: Record<string, unknown>;
41
+ }
42
+ type ConnectionState = 'disconnected' | 'connecting' | 'connected' | 'failed';
43
+ declare class Logger {
44
+ private config;
45
+ private baseUrl;
46
+ private httpIngestUrl;
47
+ private wsIngestUrl;
48
+ private proxyMode;
49
+ private buffer;
50
+ private flushTimer;
51
+ private flushPromise;
52
+ private consoleIntercepted;
53
+ private httpBackoffMs;
54
+ private maxBackoffMs;
55
+ private consecutiveHttpFailures;
56
+ private _onError;
57
+ private _onUnhandledRejection;
58
+ private ws;
59
+ private wsState;
60
+ private wsReconnectTimer;
61
+ private wsReconnectAttempts;
62
+ private maxWsReconnectAttempts;
63
+ private _onVisibilityChange;
64
+ private _onBeforeUnload;
65
+ constructor(config: LoggerConfig);
66
+ private debugLog;
67
+ private connectWebSocket;
68
+ private startFlushTimer;
69
+ private shouldLog;
70
+ private formatArgs;
71
+ private static readonly CONSOLE_METHOD;
72
+ private log;
73
+ private parseArgs;
74
+ debug(entry: LogEntryObject): void;
75
+ debug(message: string, metadata?: Record<string, unknown>): void;
76
+ info(entry: LogEntryObject): void;
77
+ info(message: string, metadata?: Record<string, unknown>): void;
78
+ warn(entry: LogEntryObject): void;
79
+ warn(message: string, metadata?: Record<string, unknown>): void;
80
+ error(entry: LogEntryObject): void;
81
+ error(message: string, metadata?: Record<string, unknown>): void;
82
+ error(err: Error, metadata?: Record<string, unknown>): void;
83
+ fatal(entry: LogEntryObject): void;
84
+ fatal(message: string, metadata?: Record<string, unknown>): void;
85
+ fatal(err: Error, metadata?: Record<string, unknown>): void;
86
+ withTraceId(traceId: string): ScopedLogger;
87
+ withGroup(group: string): ScopedLogger;
88
+ withWatcher(watcherId: string): ScopedLogger;
89
+ setTraceId(traceId: string | undefined): void;
90
+ setGroup(group: string | undefined): void;
91
+ interceptConsole(group?: string, options?: {
92
+ wrapErrors?: boolean;
93
+ }): void;
94
+ restoreConsole(): void;
95
+ flush(): Promise<void>;
96
+ private doFlush;
97
+ private sendViaWebSocket;
98
+ private sendViaHttp;
99
+ /** Fire-and-forget flush using fetch with keepalive (works during page unload) */
100
+ private flushSync;
101
+ private registerLifecycleHooks;
102
+ /** Check server connectivity and API key validity */
103
+ ping(): Promise<{
104
+ ok: boolean;
105
+ latencyMs: number;
106
+ error?: string;
107
+ }>;
108
+ /** Get current connection status */
109
+ getConnectionStatus(): {
110
+ transport: 'websocket' | 'http';
111
+ state: ConnectionState;
112
+ };
113
+ }
114
+ declare class ScopedLogger {
115
+ private parent;
116
+ private group?;
117
+ private traceId?;
118
+ private watcherId?;
119
+ constructor(parent: Logger, group?: string | undefined, traceId?: string | undefined, watcherId?: string | undefined);
120
+ withGroup(group: string): ScopedLogger;
121
+ withTraceId(traceId: string): ScopedLogger;
122
+ withWatcher(watcherId: string): ScopedLogger;
123
+ private opts;
124
+ debug(message: string, metadata?: Record<string, unknown>): void;
125
+ info(message: string, metadata?: Record<string, unknown>): void;
126
+ warn(message: string, metadata?: Record<string, unknown>): void;
127
+ error(message: string, metadata?: Record<string, unknown>): void;
128
+ fatal(message: string, metadata?: Record<string, unknown>): void;
129
+ }
130
+ declare function createLogger(config: LoggerConfig): Logger;
131
+
132
+ export { type InternalLogEntry, type LogEntryObject, type LogLevel, Logger, type LoggerConfig, createLogger, createLogger as default };