logan-logger 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,642 @@
1
+ var n = /* @__PURE__ */ ((t) => (t[t.DEBUG = 0] = "DEBUG", t[t.INFO = 1] = "INFO", t[t.WARN = 2] = "WARN", t[t.ERROR = 3] = "ERROR", t[t.SILENT = 4] = "SILENT", t))(n || {});
2
+ function d() {
3
+ const t = u(), e = O(t), r = b(t);
4
+ return {
5
+ name: t,
6
+ version: e,
7
+ capabilities: r
8
+ };
9
+ }
10
+ function u() {
11
+ return typeof globalThis.Deno < "u" ? "deno" : typeof globalThis.Bun < "u" ? "bun" : typeof window < "u" && typeof document < "u" ? "browser" : typeof globalThis.importScripts == "function" && typeof window > "u" ? "webworker" : typeof process < "u" && process.versions && process.versions.node ? "node" : "unknown";
12
+ }
13
+ function O(t) {
14
+ switch (t) {
15
+ case "node":
16
+ return typeof process < "u" ? process.version : void 0;
17
+ case "deno":
18
+ return typeof globalThis.Deno < "u" ? globalThis.Deno.version?.deno : void 0;
19
+ case "bun":
20
+ return typeof globalThis.Bun < "u" ? globalThis.Bun.version : void 0;
21
+ case "browser":
22
+ return typeof navigator < "u" ? navigator.userAgent : void 0;
23
+ default:
24
+ return;
25
+ }
26
+ }
27
+ function b(t) {
28
+ switch (t) {
29
+ case "node":
30
+ return {
31
+ fileSystem: !0,
32
+ colorSupport: !0,
33
+ processInfo: !0,
34
+ streams: !0
35
+ };
36
+ case "deno":
37
+ return {
38
+ fileSystem: !0,
39
+ colorSupport: !0,
40
+ processInfo: !0,
41
+ streams: !0
42
+ };
43
+ case "bun":
44
+ return {
45
+ fileSystem: !0,
46
+ colorSupport: !0,
47
+ processInfo: !0,
48
+ streams: !0
49
+ };
50
+ case "browser":
51
+ return {
52
+ fileSystem: !1,
53
+ colorSupport: !0,
54
+ // CSS styling in console
55
+ processInfo: !1,
56
+ streams: !1
57
+ };
58
+ case "webworker":
59
+ return {
60
+ fileSystem: !1,
61
+ colorSupport: !1,
62
+ processInfo: !1,
63
+ streams: !1
64
+ };
65
+ default:
66
+ return {
67
+ fileSystem: !1,
68
+ colorSupport: !1,
69
+ processInfo: !1,
70
+ streams: !1
71
+ };
72
+ }
73
+ }
74
+ function T() {
75
+ return u() === "node";
76
+ }
77
+ function D() {
78
+ return u() === "browser";
79
+ }
80
+ function M() {
81
+ return u() === "deno";
82
+ }
83
+ function $() {
84
+ return u() === "bun";
85
+ }
86
+ function f(t, e) {
87
+ const r = /* @__PURE__ */ new WeakSet();
88
+ return JSON.stringify(t, (s, o) => {
89
+ if (typeof o == "object" && o !== null) {
90
+ if (r.has(o))
91
+ return "[Circular]";
92
+ r.add(o);
93
+ }
94
+ return o instanceof Error ? {
95
+ name: o.name,
96
+ message: o.message,
97
+ stack: o.stack,
98
+ ...Object.getOwnPropertyNames(o).reduce((a, i) => (i !== "name" && i !== "message" && i !== "stack" && (a[i] = o[i]), a), {})
99
+ } : typeof o == "function" ? `[Function: ${o.name || "anonymous"}]` : o === void 0 ? "[undefined]" : typeof o == "bigint" ? `[BigInt: ${o.toString()}]` : typeof o == "symbol" ? `[Symbol: ${o.toString()}]` : o;
100
+ }, e);
101
+ }
102
+ function N(t, e = ["password", "token", "secret", "key", "auth"]) {
103
+ if (typeof t != "object" || t === null)
104
+ return t;
105
+ const r = Array.isArray(t) ? [] : {};
106
+ for (const [s, o] of Object.entries(t))
107
+ e.some(
108
+ (i) => s.toLowerCase().includes(i.toLowerCase())
109
+ ) ? r[s] = "[REDACTED]" : typeof o == "object" && o !== null ? r[s] = N(o, e) : r[s] = o;
110
+ return r;
111
+ }
112
+ class p {
113
+ constructor(e = {}) {
114
+ this.childMetadata = {}, this.config = e, this.level = e.level ?? n.INFO, this.runtime = d().name;
115
+ }
116
+ debug(e, r) {
117
+ this.log(n.DEBUG, e, r);
118
+ }
119
+ info(e, r) {
120
+ this.log(n.INFO, e, r);
121
+ }
122
+ warn(e, r) {
123
+ this.log(n.WARN, e, r);
124
+ }
125
+ error(e, r) {
126
+ this.log(n.ERROR, e, r);
127
+ }
128
+ log(e, r, s) {
129
+ if (!this.shouldLog(e))
130
+ return;
131
+ const o = typeof r == "function" ? r() : r, a = { ...this.childMetadata, ...s }, i = {
132
+ timestamp: /* @__PURE__ */ new Date(),
133
+ level: e,
134
+ message: o,
135
+ metadata: Object.keys(a).length > 0 ? a : void 0,
136
+ runtime: this.runtime
137
+ };
138
+ this.writeLog(i);
139
+ }
140
+ setLevel(e) {
141
+ this.level = e;
142
+ }
143
+ getLevel() {
144
+ return this.level;
145
+ }
146
+ child(e) {
147
+ const r = this.createChild();
148
+ return r.childMetadata = { ...this.childMetadata, ...e }, r;
149
+ }
150
+ shouldLog(e) {
151
+ return e >= this.level;
152
+ }
153
+ }
154
+ function W(t) {
155
+ return t instanceof Error ? {
156
+ name: t.name,
157
+ message: t.message,
158
+ stack: t.stack,
159
+ ...t
160
+ // Include any additional properties
161
+ } : t;
162
+ }
163
+ function A(t, e = "text") {
164
+ if (e === "json")
165
+ return f({
166
+ timestamp: t.timestamp.toISOString(),
167
+ level: n[t.level].toLowerCase(),
168
+ message: t.message,
169
+ metadata: t.metadata,
170
+ runtime: t.runtime
171
+ });
172
+ const r = t.timestamp.toISOString(), s = n[t.level].toUpperCase(), o = t.metadata ? ` ${f(t.metadata)}` : "";
173
+ return `[${r}] ${s}: ${t.message}${o}`;
174
+ }
175
+ class m extends p {
176
+ constructor(e = {}) {
177
+ super(e), this.initializeWinston();
178
+ }
179
+ async initializeWinston() {
180
+ try {
181
+ const e = await import("winston");
182
+ this.winston = this.createWinstonLogger(e);
183
+ } catch {
184
+ console.warn("[logan-logger] Winston not found, falling back to console logging");
185
+ }
186
+ }
187
+ createWinstonLogger(e) {
188
+ const r = e.format.combine(
189
+ e.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
190
+ e.format.errors({ stack: !0 }),
191
+ e.format.json(),
192
+ e.format.prettyPrint()
193
+ ), s = e.format.combine(
194
+ e.format.colorize(),
195
+ e.format.timestamp({ format: "HH:mm:ss" }),
196
+ e.format.printf(({ timestamp: a, level: i, message: E, ...g }) => {
197
+ const R = Object.keys(g).length ? JSON.stringify(g, null, 2) : "";
198
+ return `${a} [${i}]: ${E} ${R}`;
199
+ })
200
+ ), o = e.createLogger({
201
+ level: this.getWinstonLevel(this.level),
202
+ format: r,
203
+ transports: [
204
+ new e.transports.Console({
205
+ format: process.env.NODE_ENV === "production" ? r : s
206
+ })
207
+ ]
208
+ });
209
+ return process.env.NODE_ENV === "production" && (o.add(
210
+ new e.transports.File({
211
+ filename: "logs/error.log",
212
+ level: "error",
213
+ maxsize: 5242880,
214
+ // 5MB
215
+ maxFiles: 5
216
+ })
217
+ ), o.add(
218
+ new e.transports.File({
219
+ filename: "logs/combined.log",
220
+ maxsize: 5242880,
221
+ // 5MB
222
+ maxFiles: 10
223
+ })
224
+ )), o;
225
+ }
226
+ writeLog(e) {
227
+ this.winston ? this.winston.log({
228
+ level: this.getWinstonLevel(e.level),
229
+ message: e.message,
230
+ timestamp: e.timestamp,
231
+ ...e.metadata
232
+ }) : this.writeToConsole(e);
233
+ }
234
+ createChild() {
235
+ return new m(this.config);
236
+ }
237
+ writeToConsole(e) {
238
+ const r = e.timestamp.toISOString(), s = n[e.level].toLowerCase(), o = e.metadata ? ` ${f(e.metadata)}` : "", a = `[${r}] ${s.toUpperCase()}: ${e.message}${o}`;
239
+ switch (e.level) {
240
+ case n.DEBUG:
241
+ console.debug(a);
242
+ break;
243
+ case n.INFO:
244
+ console.info(a);
245
+ break;
246
+ case n.WARN:
247
+ console.warn(a);
248
+ break;
249
+ case n.ERROR:
250
+ console.error(a);
251
+ break;
252
+ }
253
+ }
254
+ getWinstonLevel(e) {
255
+ switch (e) {
256
+ case n.DEBUG:
257
+ return "debug";
258
+ case n.INFO:
259
+ return "info";
260
+ case n.WARN:
261
+ return "warn";
262
+ case n.ERROR:
263
+ return "error";
264
+ default:
265
+ return "info";
266
+ }
267
+ }
268
+ setLevel(e) {
269
+ super.setLevel(e), this.winston && (this.winston.level = this.getWinstonLevel(e));
270
+ }
271
+ }
272
+ function B(t) {
273
+ return {
274
+ write: (e) => {
275
+ t.info(e.trim());
276
+ }
277
+ };
278
+ }
279
+ class c extends p {
280
+ constructor(e = {}) {
281
+ super(e);
282
+ }
283
+ writeLog(e) {
284
+ const r = this.formatMessage(e), s = this.getConsoleStyle(e.level), o = e.metadata ? ` ${f(e.metadata)}` : "", a = `%c${r}${o}`;
285
+ switch (e.level) {
286
+ case n.DEBUG:
287
+ console.debug ? console.debug(a, s) : console.log(a, s);
288
+ break;
289
+ case n.INFO:
290
+ console.info(a, s);
291
+ break;
292
+ case n.WARN:
293
+ console.warn(a, s);
294
+ break;
295
+ case n.ERROR:
296
+ console.error(a, s);
297
+ break;
298
+ }
299
+ }
300
+ createChild() {
301
+ return new c(this.config);
302
+ }
303
+ formatMessage(e) {
304
+ const r = e.timestamp.toISOString(), s = n[e.level].toUpperCase();
305
+ return `[${r}] ${s}: ${e.message}`;
306
+ }
307
+ getConsoleStyle(e) {
308
+ if (!this.config.colorize)
309
+ return "";
310
+ switch (e) {
311
+ case n.DEBUG:
312
+ return "color: #888; font-weight: normal;";
313
+ case n.INFO:
314
+ return "color: #007acc; font-weight: normal;";
315
+ case n.WARN:
316
+ return "color: #ff8c00; font-weight: bold;";
317
+ case n.ERROR:
318
+ return "color: #dc3545; font-weight: bold;";
319
+ default:
320
+ return "";
321
+ }
322
+ }
323
+ shouldLogInProduction() {
324
+ return (globalThis.process?.env?.NODE_ENV || globalThis.process?.env?.NEXT_PUBLIC_APP_ENV || "development") !== "production" || this.level <= n.ERROR;
325
+ }
326
+ shouldLog(e) {
327
+ return !this.shouldLogInProduction() && e < n.ERROR ? !1 : super.shouldLog(e);
328
+ }
329
+ }
330
+ class G extends c {
331
+ constructor() {
332
+ super(...arguments), this.groupStack = [];
333
+ }
334
+ group(e) {
335
+ console.group(e), this.groupStack.push(e);
336
+ }
337
+ groupCollapsed(e) {
338
+ console.groupCollapsed(e), this.groupStack.push(e);
339
+ }
340
+ groupEnd() {
341
+ console.groupEnd(), this.groupStack.pop();
342
+ }
343
+ time(e) {
344
+ console.time(e);
345
+ }
346
+ timeEnd(e) {
347
+ console.timeEnd(e);
348
+ }
349
+ trace(e, r) {
350
+ console.trace(e, r);
351
+ }
352
+ count(e) {
353
+ console.count(e);
354
+ }
355
+ countReset(e) {
356
+ console.countReset(e);
357
+ }
358
+ table(e) {
359
+ console.table(e);
360
+ }
361
+ }
362
+ class _ extends c {
363
+ mark(e) {
364
+ typeof performance < "u" && performance.mark && performance.mark(e);
365
+ }
366
+ measure(e, r, s) {
367
+ if (typeof performance < "u" && performance.measure)
368
+ try {
369
+ performance.measure(e, r, s);
370
+ const o = performance.getEntriesByName(e, "measure");
371
+ if (o.length > 0) {
372
+ const a = o[o.length - 1];
373
+ this.info(`Performance: ${e}`, {
374
+ duration: a.duration,
375
+ startTime: a.startTime
376
+ });
377
+ }
378
+ } catch (o) {
379
+ this.warn("Failed to measure performance", { name: e, error: o });
380
+ }
381
+ }
382
+ clearMarks(e) {
383
+ typeof performance < "u" && performance.clearMarks && performance.clearMarks(e);
384
+ }
385
+ clearMeasures(e) {
386
+ typeof performance < "u" && performance.clearMeasures && performance.clearMeasures(e);
387
+ }
388
+ }
389
+ function h() {
390
+ const t = d();
391
+ return {
392
+ level: n.INFO,
393
+ format: "text",
394
+ timestamp: !0,
395
+ colorize: t.capabilities.colorSupport,
396
+ metadata: {},
397
+ transports: [
398
+ {
399
+ type: "console",
400
+ options: {}
401
+ }
402
+ ]
403
+ };
404
+ }
405
+ function j() {
406
+ const t = {};
407
+ if (typeof process < "u" && process.env) {
408
+ const e = process.env;
409
+ e.LOG_LEVEL && (t.level = L(e.LOG_LEVEL)), e.LOG_FORMAT && ["json", "text"].includes(e.LOG_FORMAT) && (t.format = e.LOG_FORMAT), e.LOG_TIMESTAMP && (t.timestamp = e.LOG_TIMESTAMP.toLowerCase() === "true"), e.LOG_COLOR && (t.colorize = e.LOG_COLOR.toLowerCase() === "true");
410
+ }
411
+ return t;
412
+ }
413
+ async function U(t) {
414
+ const e = d();
415
+ if (!e.capabilities.fileSystem)
416
+ return {};
417
+ const r = t ? [t] : [
418
+ "logan.config.json",
419
+ "logan.config.js",
420
+ ".loganrc.json",
421
+ "package.json"
422
+ // Check for logan config in package.json
423
+ ];
424
+ for (const s of r)
425
+ try {
426
+ if (e.name === "node")
427
+ return await w(s);
428
+ if (e.name === "deno")
429
+ return await v(s);
430
+ if (e.name === "bun")
431
+ return await S(s);
432
+ } catch {
433
+ }
434
+ return {};
435
+ }
436
+ async function w(t) {
437
+ try {
438
+ const e = await import("./__vite-browser-external-DYxpcVy9.mjs"), r = await import("./__vite-browser-external-DYxpcVy9.mjs");
439
+ if (t.endsWith(".json")) {
440
+ const s = await e.readFile(t, "utf-8"), o = JSON.parse(s);
441
+ return t === "package.json" ? o.logan || {} : o;
442
+ } else if (t.endsWith(".js")) {
443
+ const s = r.resolve(t);
444
+ delete require.cache[s];
445
+ const o = require(s);
446
+ return o.default || o;
447
+ }
448
+ } catch {
449
+ }
450
+ return {};
451
+ }
452
+ async function v(t) {
453
+ try {
454
+ if (t.endsWith(".json")) {
455
+ const e = await globalThis.Deno.readTextFile(t), r = JSON.parse(e);
456
+ return t === "package.json" ? r.logan || {} : r;
457
+ } else if (t.endsWith(".js")) {
458
+ const e = await import(
459
+ /* @vite-ignore */
460
+ `./${t}`
461
+ );
462
+ return e.default || e;
463
+ }
464
+ } catch {
465
+ }
466
+ return {};
467
+ }
468
+ async function S(t) {
469
+ return w(t);
470
+ }
471
+ function L(t) {
472
+ switch (t.toLowerCase()) {
473
+ case "debug":
474
+ return n.DEBUG;
475
+ case "info":
476
+ return n.INFO;
477
+ case "warn":
478
+ case "warning":
479
+ return n.WARN;
480
+ case "error":
481
+ return n.ERROR;
482
+ case "silent":
483
+ case "none":
484
+ return n.SILENT;
485
+ default:
486
+ return n.INFO;
487
+ }
488
+ }
489
+ function P(...t) {
490
+ const e = h();
491
+ return t.reduce((r, s) => ({
492
+ ...r,
493
+ ...s,
494
+ metadata: {
495
+ ...r.metadata,
496
+ ...s.metadata
497
+ },
498
+ transports: s.transports || r.transports
499
+ }), e);
500
+ }
501
+ class y {
502
+ /**
503
+ * Create a logger instance appropriate for the current runtime.
504
+ * @param config - Optional configuration for the logger
505
+ * @returns A logger instance
506
+ */
507
+ static create(e = {}) {
508
+ const r = d(), s = this.mergeConfig(e);
509
+ switch (r.name) {
510
+ case "node":
511
+ return new m(s);
512
+ case "deno":
513
+ return new c(s);
514
+ case "bun":
515
+ return new m(s);
516
+ case "browser":
517
+ case "webworker":
518
+ return new c(s);
519
+ default:
520
+ return new c(s);
521
+ }
522
+ }
523
+ /**
524
+ * Create a child logger with additional metadata.
525
+ * @param parent - The parent logger instance
526
+ * @param metadata - Additional metadata to include in all child log messages
527
+ * @returns A new logger instance with the additional metadata
528
+ */
529
+ static createChild(e, r) {
530
+ return e.child(r);
531
+ }
532
+ static mergeConfig(e) {
533
+ const r = h();
534
+ return {
535
+ ...r,
536
+ ...e,
537
+ metadata: {
538
+ ...r.metadata,
539
+ ...e.metadata
540
+ }
541
+ };
542
+ }
543
+ }
544
+ function C(t) {
545
+ return y.create(t);
546
+ }
547
+ function k() {
548
+ const t = I(), e = {
549
+ level: F(t),
550
+ colorize: t !== "production",
551
+ timestamp: !0,
552
+ format: t === "production" ? "json" : "text"
553
+ };
554
+ return C(e);
555
+ }
556
+ function I() {
557
+ return typeof process < "u" && process.env ? process.env.NODE_ENV || process.env.NEXT_PUBLIC_APP_ENV || process.env.ENVIRONMENT || "development" : typeof window < "u" && globalThis.__ENV__ || "development";
558
+ }
559
+ function F(t) {
560
+ switch (t) {
561
+ case "production":
562
+ return n.ERROR;
563
+ case "staging":
564
+ case "test":
565
+ return n.WARN;
566
+ case "development":
567
+ case "dev":
568
+ return n.DEBUG;
569
+ default:
570
+ return n.INFO;
571
+ }
572
+ }
573
+ function x(t) {
574
+ switch (t.toLowerCase()) {
575
+ case "debug":
576
+ return n.DEBUG;
577
+ case "info":
578
+ return n.INFO;
579
+ case "warn":
580
+ case "warning":
581
+ return n.WARN;
582
+ case "error":
583
+ return n.ERROR;
584
+ case "silent":
585
+ case "none":
586
+ return n.SILENT;
587
+ default:
588
+ return n.INFO;
589
+ }
590
+ }
591
+ function V(t) {
592
+ switch (t) {
593
+ case n.DEBUG:
594
+ return "debug";
595
+ case n.INFO:
596
+ return "info";
597
+ case n.WARN:
598
+ return "warn";
599
+ case n.ERROR:
600
+ return "error";
601
+ case n.SILENT:
602
+ return "silent";
603
+ default:
604
+ return "info";
605
+ }
606
+ }
607
+ const l = k(), z = {
608
+ debug: (t, e) => l.debug(t, e),
609
+ info: (t, e) => l.info(t, e),
610
+ warn: (t, e) => l.warn(t, e),
611
+ error: (t, e) => l.error(t, e)
612
+ };
613
+ export {
614
+ p as BaseLogger,
615
+ c as BrowserLogger,
616
+ G as ConsoleGroupLogger,
617
+ n as LogLevel,
618
+ y as LoggerFactory,
619
+ m as NodeLogger,
620
+ _ as PerformanceLogger,
621
+ C as createLogger,
622
+ k as createLoggerForEnvironment,
623
+ B as createMorganStream,
624
+ d as detectRuntime,
625
+ N as filterSensitiveData,
626
+ A as formatLogEntry,
627
+ h as getDefaultConfig,
628
+ D as isBrowser,
629
+ $ as isBun,
630
+ M as isDeno,
631
+ T as isNode,
632
+ j as loadConfigFromEnvironment,
633
+ U as loadConfigFromFile,
634
+ z as log,
635
+ V as logLevelToString,
636
+ l as logger,
637
+ P as mergeConfigs,
638
+ f as safeStringify,
639
+ W as serializeError,
640
+ x as stringToLogLevel
641
+ };
642
+ //# sourceMappingURL=index.esm.js.map