logan-logger 1.1.2 → 1.1.3

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