keystone-design-bootstrap 1.0.97 → 1.0.99

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.
@@ -27,9 +27,20 @@ type OTelInitOptions = {
27
27
  accountId?: number;
28
28
  accountName?: string;
29
29
  };
30
+
31
+ type PendingShipLog = {
32
+ level: LogLevel;
33
+ channel: string;
34
+ event: string;
35
+ detail: Record<string, unknown>;
36
+ };
37
+
38
+ const MAX_PENDING_POSTHOG_LOGS = 500;
30
39
  let otelProvider: LoggerProvider | null = null;
31
40
  let otelLogger: ReturnType<typeof logs.getLogger> | null = null;
32
41
  let otelInitFingerprint = '';
42
+ const pendingPostHogLogs: PendingShipLog[] = [];
43
+ const browserConsole = globalThis?.['console'];
33
44
 
34
45
  /**
35
46
  * Channel → accent colour. Unregistered channels fall through to gray.
@@ -88,6 +99,23 @@ function stripTrailingSlash(host: string): string {
88
99
  return host.replace(/\/+$/, '');
89
100
  }
90
101
 
102
+ function enqueuePendingPostHogLog(entry: PendingShipLog): void {
103
+ if (pendingPostHogLogs.length >= MAX_PENDING_POSTHOG_LOGS) {
104
+ pendingPostHogLogs.shift();
105
+ }
106
+ pendingPostHogLogs.push(entry);
107
+ }
108
+
109
+ function flushPendingPostHogLogs(): void {
110
+ if (!otelLogger || pendingPostHogLogs.length === 0) return;
111
+
112
+ while (pendingPostHogLogs.length > 0) {
113
+ const entry = pendingPostHogLogs.shift();
114
+ if (!entry) break;
115
+ emitToPostHog(entry.level, entry.channel, entry.event, entry.detail);
116
+ }
117
+ }
118
+
91
119
  export function initializePostHogLogging(options: OTelInitOptions): void {
92
120
  if (typeof window === 'undefined') return;
93
121
  if (!options.apiKey?.trim()) return;
@@ -129,6 +157,7 @@ export function initializePostHogLogging(options: OTelInitOptions): void {
129
157
  logs.setGlobalLoggerProvider(otelProvider);
130
158
  otelLogger = logs.getLogger(serviceName);
131
159
  otelInitFingerprint = fingerprint;
160
+ flushPendingPostHogLogs();
132
161
  }
133
162
 
134
163
  function isConsoleEnabled(): boolean {
@@ -167,18 +196,18 @@ function emitConsole(
167
196
  const detailStyle = 'color:#999; font-weight:normal';
168
197
 
169
198
  if (level === 'error') {
170
- console.error(message, channelStyle, eventStyle, detailStyle);
199
+ browserConsole?.error?.(message, channelStyle, eventStyle, detailStyle);
171
200
  return;
172
201
  }
173
202
  if (level === 'warn') {
174
- console.warn(message, channelStyle, eventStyle, detailStyle);
203
+ browserConsole?.warn?.(message, channelStyle, eventStyle, detailStyle);
175
204
  return;
176
205
  }
177
- console.log(message, channelStyle, eventStyle, detailStyle);
206
+ browserConsole?.log?.(message, channelStyle, eventStyle, detailStyle);
178
207
  }
179
208
 
180
- function emitToPostHog(level: LogLevel, channel: string, event: string, detail: Record<string, unknown>): void {
181
- if (!otelLogger) return;
209
+ function emitToPostHog(level: LogLevel, channel: string, event: string, detail: Record<string, unknown>): boolean {
210
+ if (!otelLogger) return false;
182
211
 
183
212
  const severityText = level === 'warn' ? 'WARNING' : level === 'error' ? 'ERROR' : 'INFO';
184
213
  const attributes = flattenAttributes({ channel, ...detail });
@@ -188,6 +217,7 @@ function emitToPostHog(level: LogLevel, channel: string, event: string, detail:
188
217
  body: event,
189
218
  attributes,
190
219
  });
220
+ return true;
191
221
  }
192
222
 
193
223
  function emit(level: LogLevel, channel: string, event: string, detail: Record<string, unknown>, options?: LogOptions): void {
@@ -197,7 +227,10 @@ function emit(level: LogLevel, channel: string, event: string, detail: Record<st
197
227
  emitConsole(level, channel, event, detail);
198
228
  }
199
229
  if (shouldShip) {
200
- emitToPostHog(level, channel, event, detail);
230
+ const shipped = emitToPostHog(level, channel, event, detail);
231
+ if (!shipped) {
232
+ enqueuePendingPostHogLog({ level, channel, event, detail });
233
+ }
201
234
  }
202
235
  }
203
236