@oxog/log 1.0.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.
@@ -0,0 +1,854 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/plugins/index.ts
21
+ var plugins_exports = {};
22
+ __export(plugins_exports, {
23
+ addCorrelationId: () => addCorrelationId,
24
+ addSourceLocation: () => addSourceLocation,
25
+ addTimestamp: () => addTimestamp,
26
+ browserPlugin: () => browserPlugin,
27
+ bufferEntry: () => bufferEntry,
28
+ bufferPlugin: () => bufferPlugin,
29
+ clearBuffer: () => clearBuffer,
30
+ clearTimers: () => clearTimers,
31
+ correlationPlugin: () => correlationPlugin,
32
+ createTimer: () => createTimer,
33
+ detectFormat: () => detectFormat,
34
+ endGroup: () => endGroup,
35
+ endTimer: () => endTimer,
36
+ ensureCorrelationId: () => ensureCorrelationId,
37
+ flushBuffer: () => flushBuffer,
38
+ flushBufferSync: () => flushBufferSync,
39
+ formatEntry: () => formatEntry,
40
+ formatPlugin: () => formatPlugin,
41
+ fromIso: () => fromIso,
42
+ generateCorrelationId: () => generateCorrelationId,
43
+ getActiveTimers: () => getActiveTimers,
44
+ getBufferSize: () => getBufferSize,
45
+ getConsoleMethod: () => getConsoleMethod,
46
+ getCorrelationId: () => getCorrelationId,
47
+ getEffectiveFormat: () => getEffectiveFormat,
48
+ getLevelName: () => getLevelName,
49
+ getLevelStyles: () => getLevelStyles,
50
+ hasTimer: () => hasTimer,
51
+ isGroupingEnabled: () => isGroupingEnabled,
52
+ isLevelEnabled: () => isLevelEnabled,
53
+ levelPlugin: () => levelPlugin,
54
+ now: () => now,
55
+ nowIso: () => nowIso,
56
+ parseLevel: () => parseLevel,
57
+ redactEntry: () => redactEntry,
58
+ redactPlugin: () => redactPlugin,
59
+ setCorrelationId: () => setCorrelationId,
60
+ setLevel: () => setLevel,
61
+ sourcePlugin: () => sourcePlugin,
62
+ startGroup: () => startGroup,
63
+ startTimer: () => startTimer,
64
+ stopFlushInterval: () => stopFlushInterval,
65
+ timestampPlugin: () => timestampPlugin,
66
+ timingPlugin: () => timingPlugin,
67
+ toIso: () => toIso,
68
+ writeToBrowserConsole: () => writeToBrowserConsole
69
+ });
70
+ module.exports = __toCommonJS(plugins_exports);
71
+
72
+ // src/constants.ts
73
+ var LOG_LEVELS = {
74
+ trace: 10 /* Trace */,
75
+ debug: 20 /* Debug */,
76
+ info: 30 /* Info */,
77
+ warn: 40 /* Warn */,
78
+ error: 50 /* Error */,
79
+ fatal: 60 /* Fatal */
80
+ };
81
+ var LEVEL_NAMES = {
82
+ [10 /* Trace */]: "trace",
83
+ [20 /* Debug */]: "debug",
84
+ [30 /* Info */]: "info",
85
+ [40 /* Warn */]: "warn",
86
+ [50 /* Error */]: "error",
87
+ [60 /* Fatal */]: "fatal"
88
+ };
89
+ var DEFAULT_BUFFER_OPTIONS = {
90
+ size: 100,
91
+ flushInterval: 1e3
92
+ };
93
+ var LEVEL_COLORS = {
94
+ trace: "gray",
95
+ debug: "cyan",
96
+ info: "blue",
97
+ warn: "yellow",
98
+ error: "red",
99
+ fatal: "magenta"
100
+ };
101
+ var LEVEL_LABELS = {
102
+ trace: "TRACE",
103
+ debug: "DEBUG",
104
+ info: "INFO ",
105
+ warn: "WARN ",
106
+ error: "ERROR",
107
+ fatal: "FATAL"
108
+ };
109
+ var REDACTED_VALUE = "[REDACTED]";
110
+ var IS_NODE = typeof process !== "undefined" && process.versions != null && process.versions.node != null;
111
+ var IS_BROWSER = typeof window !== "undefined" && typeof window.document !== "undefined";
112
+ var IS_DEV = IS_NODE && process.env["NODE_ENV"] !== "production";
113
+ var IS_TTY = IS_NODE && process.stdout?.isTTY === true;
114
+
115
+ // src/plugins/core/level.ts
116
+ function levelPlugin() {
117
+ return {
118
+ name: "level",
119
+ version: "1.0.0",
120
+ install(kernel) {
121
+ const ctx = kernel.getContext();
122
+ if (ctx.level === void 0) {
123
+ ctx.level = LOG_LEVELS.info;
124
+ }
125
+ }
126
+ };
127
+ }
128
+ function isLevelEnabled(ctx, levelName) {
129
+ const levelValue = LOG_LEVELS[levelName];
130
+ return levelValue >= ctx.level;
131
+ }
132
+ function getLevelName(ctx) {
133
+ return LEVEL_NAMES[ctx.level] ?? "info";
134
+ }
135
+ function setLevel(ctx, levelName) {
136
+ const levelValue = LOG_LEVELS[levelName];
137
+ if (levelValue !== void 0) {
138
+ ctx.level = levelValue;
139
+ }
140
+ }
141
+ function parseLevel(level) {
142
+ if (typeof level === "number") {
143
+ return level;
144
+ }
145
+ return LOG_LEVELS[level] ?? LOG_LEVELS.info;
146
+ }
147
+
148
+ // src/utils/format.ts
149
+ function formatJson(entry) {
150
+ return JSON.stringify(entry, jsonReplacer);
151
+ }
152
+ function jsonReplacer(_key, value) {
153
+ if (typeof value === "bigint") {
154
+ return value.toString();
155
+ }
156
+ if (value instanceof Error) {
157
+ const errorObj = value;
158
+ const result = {
159
+ name: value.name,
160
+ message: value.message,
161
+ stack: value.stack
162
+ };
163
+ for (const key of Object.keys(errorObj)) {
164
+ if (!(key in result)) {
165
+ result[key] = errorObj[key];
166
+ }
167
+ }
168
+ return result;
169
+ }
170
+ if (typeof value === "object" && value !== null) {
171
+ if (value instanceof RegExp) {
172
+ return value.toString();
173
+ }
174
+ if (value instanceof Date) {
175
+ return value.toISOString();
176
+ }
177
+ if (value instanceof Map) {
178
+ return Object.fromEntries(value);
179
+ }
180
+ if (value instanceof Set) {
181
+ return Array.from(value);
182
+ }
183
+ }
184
+ return value;
185
+ }
186
+ function safeStringify(value, indent) {
187
+ const seen = /* @__PURE__ */ new WeakSet();
188
+ return JSON.stringify(
189
+ value,
190
+ (_key, val) => {
191
+ const replaced = jsonReplacer(_key, val);
192
+ if (typeof replaced === "object" && replaced !== null) {
193
+ if (seen.has(replaced)) {
194
+ return "[Circular]";
195
+ }
196
+ seen.add(replaced);
197
+ }
198
+ return replaced;
199
+ },
200
+ indent
201
+ );
202
+ }
203
+ function formatPretty(entry, pigment, options = {}) {
204
+ const parts = [];
205
+ const { timestamp = true, source = true } = options;
206
+ if (timestamp && entry.time) {
207
+ const time = formatTime(entry.time);
208
+ parts.push(pigment ? pigment.gray(`[${time}]`) : `[${time}]`);
209
+ }
210
+ const levelLabel = LEVEL_LABELS[entry.levelName] || entry.levelName.toUpperCase();
211
+ const levelColor = LEVEL_COLORS[entry.levelName] || "white";
212
+ if (pigment) {
213
+ const colorFn = pigment[levelColor];
214
+ parts.push(colorFn ? colorFn(levelLabel) : levelLabel);
215
+ } else {
216
+ parts.push(levelLabel);
217
+ }
218
+ if (source && entry.file) {
219
+ const location = entry.line ? `${entry.file}:${entry.line}` : entry.file;
220
+ parts.push(pigment ? pigment.dim(`(${location})`) : `(${location})`);
221
+ }
222
+ if (entry.msg) {
223
+ parts.push(entry.msg);
224
+ }
225
+ const extra = getExtraFields(entry);
226
+ if (Object.keys(extra).length > 0) {
227
+ const extraStr = safeStringify(extra);
228
+ parts.push(pigment ? pigment.dim(extraStr) : extraStr);
229
+ }
230
+ if (entry.err?.stack) {
231
+ parts.push("\n" + (pigment ? pigment.red(entry.err.stack) : entry.err.stack));
232
+ }
233
+ return parts.join(" ");
234
+ }
235
+ function getExtraFields(entry) {
236
+ const standardFields = /* @__PURE__ */ new Set([
237
+ "level",
238
+ "levelName",
239
+ "time",
240
+ "msg",
241
+ "file",
242
+ "line",
243
+ "column",
244
+ "correlationId",
245
+ "duration",
246
+ "err",
247
+ "name"
248
+ ]);
249
+ const extra = {};
250
+ for (const [key, value] of Object.entries(entry)) {
251
+ if (!standardFields.has(key)) {
252
+ extra[key] = value;
253
+ }
254
+ }
255
+ return extra;
256
+ }
257
+ function formatTime(timestamp) {
258
+ const date = new Date(timestamp);
259
+ const hours = date.getHours().toString().padStart(2, "0");
260
+ const minutes = date.getMinutes().toString().padStart(2, "0");
261
+ const seconds = date.getSeconds().toString().padStart(2, "0");
262
+ return `${hours}:${minutes}:${seconds}`;
263
+ }
264
+
265
+ // src/utils/env.ts
266
+ function isNode() {
267
+ return typeof process !== "undefined" && process.versions != null && process.versions.node != null;
268
+ }
269
+ function isBrowser() {
270
+ return typeof window !== "undefined" && typeof window.document !== "undefined";
271
+ }
272
+ function isDev() {
273
+ if (!isNode()) return false;
274
+ return process.env["NODE_ENV"] !== "production";
275
+ }
276
+ function isTTY() {
277
+ if (!isNode()) return false;
278
+ return process.stdout?.isTTY === true;
279
+ }
280
+
281
+ // src/plugins/core/format.ts
282
+ function formatPlugin() {
283
+ return {
284
+ name: "format",
285
+ version: "1.0.0",
286
+ install(kernel) {
287
+ const ctx = kernel.getContext();
288
+ if (ctx.format === void 0) {
289
+ ctx.format = detectFormat();
290
+ } else if (ctx.format === "auto") {
291
+ ctx.format = detectFormat();
292
+ }
293
+ }
294
+ };
295
+ }
296
+ function detectFormat() {
297
+ if (isDev() && isTTY()) {
298
+ return "pretty";
299
+ }
300
+ return "json";
301
+ }
302
+ function formatEntry(ctx, entry) {
303
+ const format = ctx.format === "auto" ? detectFormat() : ctx.format;
304
+ if (format === "pretty") {
305
+ return formatPretty(entry, ctx.pigment, {
306
+ timestamp: ctx.timestamp,
307
+ source: ctx.source
308
+ });
309
+ }
310
+ return formatJson(entry);
311
+ }
312
+ function getEffectiveFormat(ctx) {
313
+ if (ctx.format === "auto") {
314
+ return detectFormat();
315
+ }
316
+ return ctx.format;
317
+ }
318
+
319
+ // src/plugins/core/timestamp.ts
320
+ function timestampPlugin() {
321
+ return {
322
+ name: "timestamp",
323
+ version: "1.0.0",
324
+ install(kernel) {
325
+ const ctx = kernel.getContext();
326
+ if (ctx.timestamp === void 0) {
327
+ ctx.timestamp = true;
328
+ }
329
+ }
330
+ };
331
+ }
332
+ function now() {
333
+ return Date.now();
334
+ }
335
+ function nowIso() {
336
+ return (/* @__PURE__ */ new Date()).toISOString();
337
+ }
338
+ function toIso(timestamp) {
339
+ return new Date(timestamp).toISOString();
340
+ }
341
+ function fromIso(isoString) {
342
+ return new Date(isoString).getTime();
343
+ }
344
+ function addTimestamp(entry) {
345
+ if (entry.time === void 0) {
346
+ entry.time = Date.now();
347
+ }
348
+ return entry;
349
+ }
350
+
351
+ // src/utils/redact.ts
352
+ function redactFields(obj, paths, placeholder = REDACTED_VALUE) {
353
+ if (!obj || typeof obj !== "object" || paths.length === 0) {
354
+ return obj;
355
+ }
356
+ const result = deepClone(obj);
357
+ for (const path of paths) {
358
+ redactPath(result, path.split("."), placeholder);
359
+ }
360
+ return result;
361
+ }
362
+ function deepClone(obj) {
363
+ if (obj === null || typeof obj !== "object") {
364
+ return obj;
365
+ }
366
+ if (Array.isArray(obj)) {
367
+ return obj.map((item) => deepClone(item));
368
+ }
369
+ if (obj instanceof Date) {
370
+ return new Date(obj.getTime());
371
+ }
372
+ if (obj instanceof RegExp) {
373
+ return new RegExp(obj.source, obj.flags);
374
+ }
375
+ if (obj instanceof Map) {
376
+ const result2 = /* @__PURE__ */ new Map();
377
+ for (const [key, value] of obj) {
378
+ result2.set(key, deepClone(value));
379
+ }
380
+ return result2;
381
+ }
382
+ if (obj instanceof Set) {
383
+ const result2 = /* @__PURE__ */ new Set();
384
+ for (const value of obj) {
385
+ result2.add(deepClone(value));
386
+ }
387
+ return result2;
388
+ }
389
+ const result = {};
390
+ for (const key of Object.keys(obj)) {
391
+ result[key] = deepClone(obj[key]);
392
+ }
393
+ return result;
394
+ }
395
+ function redactPath(obj, pathParts, placeholder) {
396
+ if (pathParts.length === 0 || !obj || typeof obj !== "object") {
397
+ return;
398
+ }
399
+ const [current, ...rest] = pathParts;
400
+ if (!current) return;
401
+ if (current === "*") {
402
+ for (const key of Object.keys(obj)) {
403
+ if (rest.length === 0) {
404
+ obj[key] = placeholder;
405
+ } else {
406
+ const value = obj[key];
407
+ if (value && typeof value === "object") {
408
+ redactPath(value, rest, placeholder);
409
+ }
410
+ }
411
+ }
412
+ return;
413
+ }
414
+ if (current === "[*]" && Array.isArray(obj)) {
415
+ for (let i = 0; i < obj.length; i++) {
416
+ if (rest.length === 0) {
417
+ obj[i] = placeholder;
418
+ } else {
419
+ const value = obj[i];
420
+ if (value && typeof value === "object") {
421
+ redactPath(value, rest, placeholder);
422
+ }
423
+ }
424
+ }
425
+ return;
426
+ }
427
+ if (!(current in obj)) {
428
+ return;
429
+ }
430
+ if (rest.length === 0) {
431
+ obj[current] = placeholder;
432
+ } else {
433
+ const value = obj[current];
434
+ if (value && typeof value === "object") {
435
+ redactPath(value, rest, placeholder);
436
+ }
437
+ }
438
+ }
439
+
440
+ // src/plugins/optional/redact.ts
441
+ function redactPlugin(options = {}) {
442
+ return {
443
+ name: "redact",
444
+ version: "1.0.0",
445
+ install(kernel) {
446
+ const ctx = kernel.getContext();
447
+ const paths = options.paths || ctx.redactPaths || [];
448
+ const placeholder = options.placeholder || REDACTED_VALUE;
449
+ ctx.redactPaths = paths;
450
+ kernel.redact = (entry) => {
451
+ return redactEntry(entry, paths, placeholder);
452
+ };
453
+ }
454
+ };
455
+ }
456
+ function redactEntry(entry, paths, placeholder = REDACTED_VALUE) {
457
+ if (paths.length === 0) {
458
+ return entry;
459
+ }
460
+ return redactFields(entry, paths, placeholder);
461
+ }
462
+
463
+ // src/utils/source.ts
464
+ function getSourceLocation(depth = 0) {
465
+ const err = new Error();
466
+ const stack = err.stack;
467
+ if (!stack) {
468
+ return void 0;
469
+ }
470
+ const frames = parseStack(stack);
471
+ const targetIndex = 2 + depth;
472
+ if (targetIndex >= frames.length) {
473
+ return void 0;
474
+ }
475
+ return frames[targetIndex];
476
+ }
477
+ function parseStack(stack) {
478
+ const lines = stack.split("\n");
479
+ const frames = [];
480
+ for (const line of lines) {
481
+ const trimmed = line.trim();
482
+ if (!trimmed.startsWith("at ")) {
483
+ continue;
484
+ }
485
+ const frame = parseV8Frame(trimmed);
486
+ if (frame) {
487
+ frames.push(frame);
488
+ }
489
+ }
490
+ return frames;
491
+ }
492
+ function parseV8Frame(line) {
493
+ let content = line.slice(3);
494
+ if (content.startsWith("async ")) {
495
+ content = content.slice(6);
496
+ }
497
+ const parenMatch = content.match(/^(.+?)\s+\((.+):(\d+):(\d+)\)$/);
498
+ if (parenMatch) {
499
+ const [, fn, path, lineStr, colStr] = parenMatch;
500
+ return {
501
+ fn: fn?.trim() || void 0,
502
+ path,
503
+ file: extractFileName(path || ""),
504
+ line: parseInt(lineStr || "0", 10),
505
+ column: parseInt(colStr || "0", 10)
506
+ };
507
+ }
508
+ const simpleMatch = content.match(/^(.+):(\d+):(\d+)$/);
509
+ if (simpleMatch) {
510
+ const [, path, lineStr, colStr] = simpleMatch;
511
+ return {
512
+ path,
513
+ file: extractFileName(path || ""),
514
+ line: parseInt(lineStr || "0", 10),
515
+ column: parseInt(colStr || "0", 10)
516
+ };
517
+ }
518
+ const noColMatch = content.match(/^(.+):(\d+)$/);
519
+ if (noColMatch) {
520
+ const [, path, lineStr] = noColMatch;
521
+ return {
522
+ path,
523
+ file: extractFileName(path || ""),
524
+ line: parseInt(lineStr || "0", 10)
525
+ };
526
+ }
527
+ return void 0;
528
+ }
529
+ function extractFileName(path) {
530
+ let cleanPath = path.replace(/^file:\/\//, "");
531
+ cleanPath = cleanPath.replace(/\\/g, "/");
532
+ cleanPath = cleanPath.split("?")[0]?.split("#")[0] || cleanPath;
533
+ const parts = cleanPath.split("/");
534
+ return parts[parts.length - 1] || cleanPath;
535
+ }
536
+
537
+ // src/plugins/optional/source.ts
538
+ function sourcePlugin(options = {}) {
539
+ return {
540
+ name: "source",
541
+ version: "1.0.0",
542
+ install(kernel) {
543
+ const ctx = kernel.getContext();
544
+ ctx.source = true;
545
+ const depth = options.depth ?? 4;
546
+ kernel.getSource = () => {
547
+ return getSourceLocation(depth);
548
+ };
549
+ }
550
+ };
551
+ }
552
+ function addSourceLocation(entry, depth = 4, options = {}) {
553
+ const location = getSourceLocation(depth);
554
+ if (location) {
555
+ entry.file = options.includeFullPath ? location.path : location.file;
556
+ entry.line = location.line;
557
+ if (options.includeColumn && location.column) {
558
+ entry.column = location.column;
559
+ }
560
+ }
561
+ return entry;
562
+ }
563
+
564
+ // src/plugins/optional/correlation.ts
565
+ var correlationOptions = {};
566
+ function correlationPlugin(options = {}) {
567
+ return {
568
+ name: "correlation",
569
+ version: "1.0.0",
570
+ install(_kernel) {
571
+ correlationOptions = options;
572
+ }
573
+ };
574
+ }
575
+ function generateCorrelationId() {
576
+ if (correlationOptions.generator) {
577
+ return correlationOptions.generator();
578
+ }
579
+ const prefix = correlationOptions.prefix ?? "cid";
580
+ const random = Math.random().toString(36).substring(2, 10);
581
+ const timestamp = Date.now().toString(36);
582
+ return `${prefix}_${timestamp}${random}`;
583
+ }
584
+ function addCorrelationId(entry, correlationId) {
585
+ entry.correlationId = correlationId;
586
+ return entry;
587
+ }
588
+ function getCorrelationId(ctx) {
589
+ return ctx.correlationId;
590
+ }
591
+ function setCorrelationId(ctx, correlationId) {
592
+ ctx.correlationId = correlationId;
593
+ }
594
+ function ensureCorrelationId(ctx) {
595
+ if (!ctx.correlationId) {
596
+ ctx.correlationId = generateCorrelationId();
597
+ }
598
+ return ctx.correlationId;
599
+ }
600
+
601
+ // src/plugins/optional/timing.ts
602
+ function timingPlugin() {
603
+ return {
604
+ name: "timing",
605
+ version: "1.0.0",
606
+ install(kernel) {
607
+ const ctx = kernel.getContext();
608
+ if (!ctx.timers) {
609
+ ctx.timers = /* @__PURE__ */ new Map();
610
+ }
611
+ },
612
+ onDestroy() {
613
+ }
614
+ };
615
+ }
616
+ function startTimer(ctx, label) {
617
+ ctx.timers.set(label, performance.now());
618
+ }
619
+ function endTimer(ctx, label) {
620
+ const start = ctx.timers.get(label);
621
+ if (start === void 0) {
622
+ return void 0;
623
+ }
624
+ const duration = performance.now() - start;
625
+ ctx.timers.delete(label);
626
+ return Math.round(duration * 100) / 100;
627
+ }
628
+ function hasTimer(ctx, label) {
629
+ return ctx.timers.has(label);
630
+ }
631
+ function getActiveTimers(ctx) {
632
+ return Array.from(ctx.timers.keys());
633
+ }
634
+ function clearTimers(ctx) {
635
+ ctx.timers.clear();
636
+ }
637
+ function createTimer(ctx, label) {
638
+ startTimer(ctx, label);
639
+ return () => endTimer(ctx, label);
640
+ }
641
+
642
+ // src/plugins/optional/buffer.ts
643
+ function bufferPlugin(options = {}) {
644
+ return {
645
+ name: "buffer",
646
+ version: "1.0.0",
647
+ install(kernel) {
648
+ const ctx = kernel.getContext();
649
+ ctx.buffer = [];
650
+ ctx.bufferOptions = {
651
+ size: options.size ?? DEFAULT_BUFFER_OPTIONS.size,
652
+ flushInterval: options.flushInterval ?? DEFAULT_BUFFER_OPTIONS.flushInterval
653
+ };
654
+ const flushInterval = ctx.bufferOptions.flushInterval ?? DEFAULT_BUFFER_OPTIONS.flushInterval;
655
+ if (flushInterval > 0) {
656
+ ctx.flushTimerId = setInterval(() => {
657
+ flushBuffer(ctx).catch(() => {
658
+ });
659
+ }, flushInterval);
660
+ }
661
+ },
662
+ async onDestroy() {
663
+ }
664
+ };
665
+ }
666
+ function bufferEntry(ctx, entry) {
667
+ ctx.buffer.push(entry);
668
+ if (ctx.buffer.length >= (ctx.bufferOptions.size ?? DEFAULT_BUFFER_OPTIONS.size)) {
669
+ flushBufferSync(ctx);
670
+ return true;
671
+ }
672
+ return false;
673
+ }
674
+ async function flushBuffer(ctx) {
675
+ if (ctx.buffer.length === 0) {
676
+ return [];
677
+ }
678
+ const entries = ctx.buffer;
679
+ ctx.buffer = [];
680
+ await Promise.all(
681
+ entries.flatMap(
682
+ (entry) => ctx.transports.map(
683
+ (transport) => Promise.resolve(transport.write(entry)).catch(() => {
684
+ })
685
+ )
686
+ )
687
+ );
688
+ ctx.emitter.emit("flush", void 0);
689
+ return entries;
690
+ }
691
+ function flushBufferSync(ctx) {
692
+ if (ctx.buffer.length === 0) {
693
+ return [];
694
+ }
695
+ const entries = ctx.buffer;
696
+ ctx.buffer = [];
697
+ for (const entry of entries) {
698
+ for (const transport of ctx.transports) {
699
+ try {
700
+ transport.write(entry);
701
+ } catch {
702
+ }
703
+ }
704
+ }
705
+ return entries;
706
+ }
707
+ function getBufferSize(ctx) {
708
+ return ctx.buffer.length;
709
+ }
710
+ function clearBuffer(ctx) {
711
+ const entries = ctx.buffer;
712
+ ctx.buffer = [];
713
+ return entries;
714
+ }
715
+ function stopFlushInterval(ctx) {
716
+ if (ctx.flushTimerId) {
717
+ clearInterval(ctx.flushTimerId);
718
+ ctx.flushTimerId = void 0;
719
+ }
720
+ }
721
+
722
+ // src/plugins/optional/browser.ts
723
+ var browserOptions = {};
724
+ function browserPlugin(options = {}) {
725
+ return {
726
+ name: "browser",
727
+ version: "1.0.0",
728
+ install(_kernel) {
729
+ browserOptions = options;
730
+ }
731
+ };
732
+ }
733
+ function getConsoleMethod(levelName) {
734
+ switch (levelName) {
735
+ case "trace":
736
+ case "debug":
737
+ return "debug";
738
+ case "info":
739
+ return "info";
740
+ case "warn":
741
+ return "warn";
742
+ case "error":
743
+ case "fatal":
744
+ return "error";
745
+ default:
746
+ return "log";
747
+ }
748
+ }
749
+ function getLevelStyles(levelName) {
750
+ if (browserOptions.styles?.[levelName]) {
751
+ return browserOptions.styles[levelName];
752
+ }
753
+ const colors = {
754
+ trace: "#888888",
755
+ debug: "#00bcd4",
756
+ info: "#2196f3",
757
+ warn: "#ff9800",
758
+ error: "#f44336",
759
+ fatal: "#9c27b0"
760
+ };
761
+ return `color: ${colors[levelName] ?? "#000000"}; font-weight: bold;`;
762
+ }
763
+ function writeToBrowserConsole(entry, styled = true) {
764
+ if (!isBrowser()) return;
765
+ const method = getConsoleMethod(entry.levelName);
766
+ const consoleObj = console;
767
+ if (styled) {
768
+ const styles = getLevelStyles(entry.levelName);
769
+ const label = `%c[${entry.levelName.toUpperCase()}]`;
770
+ const {
771
+ level: _level,
772
+ levelName: _levelName,
773
+ time: _time,
774
+ msg,
775
+ file: _file,
776
+ line: _line,
777
+ column: _column,
778
+ correlationId: _correlationId,
779
+ ...extra
780
+ } = entry;
781
+ if (Object.keys(extra).length > 0) {
782
+ consoleObj[method](label, styles, msg, extra);
783
+ } else {
784
+ consoleObj[method](label, styles, msg);
785
+ }
786
+ } else {
787
+ consoleObj[method](`[${entry.levelName.toUpperCase()}]`, entry.msg, entry);
788
+ }
789
+ }
790
+ function startGroup(label, collapsed = false) {
791
+ if (!isBrowser()) return;
792
+ if (collapsed) {
793
+ console.groupCollapsed(label);
794
+ } else {
795
+ console.group(label);
796
+ }
797
+ }
798
+ function endGroup() {
799
+ if (!isBrowser()) return;
800
+ console.groupEnd();
801
+ }
802
+ function isGroupingEnabled() {
803
+ return browserOptions.grouping ?? true;
804
+ }
805
+ // Annotate the CommonJS export names for ESM import in node:
806
+ 0 && (module.exports = {
807
+ addCorrelationId,
808
+ addSourceLocation,
809
+ addTimestamp,
810
+ browserPlugin,
811
+ bufferEntry,
812
+ bufferPlugin,
813
+ clearBuffer,
814
+ clearTimers,
815
+ correlationPlugin,
816
+ createTimer,
817
+ detectFormat,
818
+ endGroup,
819
+ endTimer,
820
+ ensureCorrelationId,
821
+ flushBuffer,
822
+ flushBufferSync,
823
+ formatEntry,
824
+ formatPlugin,
825
+ fromIso,
826
+ generateCorrelationId,
827
+ getActiveTimers,
828
+ getBufferSize,
829
+ getConsoleMethod,
830
+ getCorrelationId,
831
+ getEffectiveFormat,
832
+ getLevelName,
833
+ getLevelStyles,
834
+ hasTimer,
835
+ isGroupingEnabled,
836
+ isLevelEnabled,
837
+ levelPlugin,
838
+ now,
839
+ nowIso,
840
+ parseLevel,
841
+ redactEntry,
842
+ redactPlugin,
843
+ setCorrelationId,
844
+ setLevel,
845
+ sourcePlugin,
846
+ startGroup,
847
+ startTimer,
848
+ stopFlushInterval,
849
+ timestampPlugin,
850
+ timingPlugin,
851
+ toIso,
852
+ writeToBrowserConsole
853
+ });
854
+ //# sourceMappingURL=index.cjs.map