@likec4/log 1.46.1 → 1.48.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.
package/dist/index.mjs CHANGED
@@ -1,1458 +1,224 @@
1
- import { getAnsiColorFormatter as getAnsiColorFormatter$1, getConsoleSink as getConsoleSink$1, getTextFormatter as getTextFormatter$1, getLogger, configureSync } from '@logtape/logtape';
2
- export { withFilter } from '@logtape/logtape';
3
-
4
- const normalizeDescriptors=(error)=>{
5
- CORE_ERROR_PROPS.forEach((propName)=>{
6
- normalizeDescriptor$1(error,propName);
7
- });
8
- };
9
-
10
- const CORE_ERROR_PROPS=["name","message","stack","cause","errors"];
11
-
12
- const normalizeDescriptor$1=(error,propName)=>{
13
- const descriptor=getDescriptor(error,propName);
14
-
15
- if(descriptor===undefined){
16
- return
17
- }
18
-
19
- if(isReadonlyGetter(descriptor)){
20
- setErrorProperty$1(error,propName,error[propName]);
21
- return
22
- }
23
-
24
- if(isInvalidDescriptor(descriptor)){
25
- setErrorDescriptor(error,propName,descriptor);
26
- }
27
- };
28
-
29
-
30
- const getDescriptor=(value,propName)=>{
31
- const descriptor=Object.getOwnPropertyDescriptor(value,propName);
32
-
33
- if(descriptor!==undefined){
34
- return descriptor
35
- }
36
-
37
- const prototype=Object.getPrototypeOf(value);
38
- return prototype===null?undefined:getDescriptor(prototype,propName)
39
- };
40
-
41
-
42
- const isReadonlyGetter=({get,set})=>
43
- get!==undefined&&set===undefined;
44
-
45
- const isInvalidDescriptor=({enumerable,writable})=>
46
- enumerable||!writable;
47
-
48
-
49
- const setErrorProperty$1=(error,propName,value)=>{
50
- setErrorDescriptor(error,propName,{value});
51
- };
52
-
53
-
54
- const setErrorDescriptor=(error,propName,descriptor)=>{
55
-
56
- Object.defineProperty(error,propName,{
57
- ...descriptor,
58
- ...("get"in descriptor||"set"in descriptor?{}:{writable:true}),
59
- enumerable:false,
60
- configurable:true
61
- });
62
- };
63
-
64
- const normalizeAggregate=(error,recurse)=>{
65
- if(Array.isArray(error.errors)){
66
- const aggregateErrors=error.errors.
67
- filter(isDefined).
68
- map(recurse).
69
- filter(Boolean);
70
- setErrorProperty$1(error,"errors",aggregateErrors);
71
- }else if(isAggregateError(error)){
72
- setErrorProperty$1(error,"errors",[]);
73
- }else if(error.errors!==undefined){
74
- deleteAggregateErrors(error);
75
- }
76
- };
77
-
78
- const isDefined=(error)=>error!==undefined;
79
-
80
- const isAggregateError=(error)=>
81
- "AggregateError"in globalThis&&(
82
- error.name==="AggregateError"||error instanceof AggregateError);
83
-
84
- const deleteAggregateErrors=(error)=>{
85
-
86
- delete error.errors;
87
-
88
- if(error.errors!==undefined){
89
- setErrorProperty$1(error,"errors",[]);
90
- }
91
- };
92
-
93
- const normalizeCause=(error,recurse)=>{
94
- if(!("cause"in error)){
95
- return
96
- }
97
-
98
- const cause=error.cause===undefined?error.cause:recurse(error.cause);
99
-
100
- if(cause===undefined){
101
-
102
- delete error.cause;
103
- }else {
104
- setErrorProperty$1(error,"cause",cause);
105
- }
106
- };
107
-
108
- const isErrorInstance=(value)=>
109
- isInstanceOfError(value)||hasErrorTag(value);
110
-
111
-
112
-
113
- const isInstanceOfError=(value)=>{
114
- try{
115
- return value instanceof Error
116
- }catch{
117
- return false
118
- }
119
- };
120
-
121
- const hasErrorTag=(value)=>{
122
- try{
123
- return ERROR_TAGS.has(Object.prototype.toString.call(value))
124
- }catch{
125
- return false
126
- }
127
- };
128
-
129
- const ERROR_TAGS=new Set([
130
-
131
- "[object Error]",
132
-
133
- "[object DOMException]",
134
-
135
- "[object DOMError]",
136
-
137
- "[object Exception]"]
138
- );
139
-
140
- function isPlainObject(value) {
141
- if (typeof value !== 'object' || value === null) {
142
- return false;
143
- }
144
-
145
- const prototype = Object.getPrototypeOf(value);
146
- return (prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) && !(Symbol.toStringTag in value) && !(Symbol.iterator in value);
147
- }
148
-
149
- const isNonModifiableError=(error)=>
150
- !Object.isExtensible(error)||
151
- CORE_ERROR_PROPS.some(
152
- (propName)=>
153
- isNonConfigurableProp(error,propName)||isThrowingProp(error,propName)
154
- );
155
-
156
-
157
-
158
- const isNonConfigurableProp=(error,propName)=>{
159
- const descriptor=Object.getOwnPropertyDescriptor(error,propName);
160
- return descriptor!==undefined&&!descriptor.configurable
161
- };
162
-
163
-
164
- const isThrowingProp=(error,propName)=>{
165
- try{
166
-
167
- error[propName];
168
- return false
169
- }catch{
170
- return true
171
- }
172
- };
173
-
174
- const setStack=(error)=>{
175
- const stack=getStack$3(error.message,error.name);
176
- setErrorProperty$1(error,"stack",stack);
177
- };
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
- const getStack$3=(message="",name="Error")=>{
186
- const StackError=getErrorClass(name);
187
- const{stack}=new StackError(message);
188
- return typeof stack==="string"&&stack!==""?
189
- stack:
190
- `${name}: ${message}`
191
- };
192
-
193
-
194
-
195
-
196
-
197
- const getErrorClass=(name)=>{
198
- const descriptor={
199
- value:name,
200
- enumerable:false,
201
- writable:true,
202
- configurable:true
203
- };
204
-
205
- const StackError=Object.defineProperty(
206
- class extends Error{},
207
- "name",
208
- descriptor
209
- );
210
-
211
- Object.defineProperty(StackError.prototype,"name",descriptor);
212
- return StackError
213
- };
214
-
215
- const copyObject=(object)=>{
216
- const objectCopy={};
217
-
218
-
219
- for(const propName of getPropsToCopy(object)){
220
-
221
- try{
222
- const value=object[propName];
223
- const{
224
- enumerable,
225
- configurable,
226
- writable=true
227
- }=getDescriptor(object,propName);
228
-
229
- Object.defineProperty(objectCopy,propName,{
230
- value,
231
- enumerable,
232
- configurable,
233
- writable
234
- });
235
- }catch{}
236
- }
237
-
238
- return objectCopy
239
- };
240
-
241
-
242
-
243
-
244
-
245
- const getPropsToCopy=(object)=>{
246
- const propNames=getOwnKeys(object);
247
-
248
-
249
- for(const propName of CORE_ERROR_PROPS){
250
-
251
- if(isInheritedProp(object,propName)){
252
-
253
- propNames.push(propName);
254
- }
255
- }
256
-
257
- return propNames
258
- };
259
-
260
-
261
- const getOwnKeys=(object)=>{
262
- try{
263
- return Reflect.ownKeys(object)
264
- }catch{
265
- return []
266
- }
267
- };
268
-
269
-
270
- const isInheritedProp=(object,propName)=>{
271
- try{
272
- return propName in object&&!Object.hasOwn(object,propName)
273
- }catch{
274
- return false
275
- }
276
- };
277
-
278
- const objectifyError=(object)=>{
279
- const{name,message,stack,cause,errors,...objectA}=copyObject(object);
280
- const messageA=getMessage$1(message,objectA);
281
- const error=newError(name,messageA);
282
-
283
- if(message===messageA){
284
- assignObjectProps(error,objectA);
285
- }
286
-
287
- Object.entries({name,stack,cause,errors}).forEach(
288
- ([propName,propValue])=>{
289
- setNewErrorProperty(error,propName,propValue);
290
- }
291
- );
292
-
293
- if(stack===undefined){
294
- setStack(error);
295
- }
296
-
297
- return error
298
- };
299
-
300
-
301
- const getMessage$1=(message,object)=>
302
- typeof message==="string"&&message!==""?
303
- message:
304
- truncateMessage(safeJsonStringify(object));
305
-
306
- const safeJsonStringify=(object)=>{
307
- try{
308
- return JSON.stringify(object)
309
- }catch{
310
- return safeStringify$1(object)
311
- }
312
- };
313
-
314
- const safeStringify$1=(object)=>{
315
- try{
316
- return String(object)
317
- }catch{
318
- return "Invalid error"
319
- }
320
- };
321
-
322
- const truncateMessage=(message)=>
323
- message.length<MESSAGE_MAX_SIZE?
324
- message:
325
- `${message.slice(0,MESSAGE_MAX_SIZE)}...`;
326
-
327
- const MESSAGE_MAX_SIZE=1e3;
328
-
329
- const newError=(name,message)=>{
330
- if(name==="AggregateError"&&"AggregateError"in globalThis){
331
- return new AggregateError([],message)
332
- }
333
-
334
- if(name in NATIVE_ERRORS){
335
- return new NATIVE_ERRORS[name](message)
336
- }
337
-
338
- return new Error(message)
339
- };
340
-
341
- const NATIVE_ERRORS={
342
- Error,
343
- ReferenceError,
344
- TypeError,
345
- SyntaxError,
346
- RangeError,
347
- URIError,
348
- EvalError
349
- };
350
-
351
-
352
- const assignObjectProps=(error,object)=>{
353
-
354
- for(const propName in object){
355
-
356
- if(!(propName in error)){
357
- error[propName]=object[propName];
358
- }
359
- }
360
- };
361
-
362
- const setNewErrorProperty=(error,propName,propValue)=>{
363
- if(propValue!==undefined){
364
- setErrorProperty$1(error,propName,propValue);
365
- }
366
- };
367
-
368
- const stringifyError=(value)=>{
369
- try{
370
- const error=new Error(String(value));
371
- setStack(error);
372
- return error
373
- }catch(error_){
374
- return error_
375
- }
376
- };
377
-
378
- const{toString:objectToString}=Object.prototype;
379
-
380
-
381
- const createError=(value)=>{
382
- if(isErrorPlainObj(value)){
383
- return objectifyError(value)
384
- }
385
-
386
- if(!isErrorInstance(value)){
387
- return stringifyError(value)
388
- }
389
-
390
- if(isInvalidError(value)){
391
- return objectifyError(value)
392
- }
393
-
394
- return value
395
- };
396
-
397
-
398
- const isErrorPlainObj=(value)=>{
399
- try{
400
- return isPlainObject(value)
401
- }catch{
402
- return false
403
- }
404
- };
405
-
406
- const isInvalidError=(value)=>
407
- isProxy(value)||isNonModifiableError(value)||hasInvalidConstructor(value);
408
-
409
-
410
-
411
-
412
- const isProxy=(value)=>{
413
- try{
414
- return objectToString.call(value)==="[object Object]"
415
- }catch{
416
- return true
417
- }
418
- };
419
-
420
-
421
-
422
- const hasInvalidConstructor=(error)=>
423
- typeof error.constructor!=="function"||
424
- typeof error.constructor.name!=="string"||
425
- error.constructor.name===""||
426
- error.constructor.prototype!==Object.getPrototypeOf(error);
427
-
428
- const normalizeException=(error,{shallow=false}={})=>
429
- recurseException(error,[],shallow);
430
-
431
- const recurseException=(error,parents,shallow)=>{
432
- if(parents.includes(error)){
433
- return
434
- }
435
-
436
- const recurse=shallow?
437
- identity:
438
- (innerError)=>recurseException(innerError,[...parents,error],shallow);
439
-
440
- const errorA=createError(error);
441
- normalizeProps(errorA,recurse);
442
- return errorA
443
- };
444
-
445
- const identity=(error)=>error;
446
-
447
- const normalizeProps=(error,recurse)=>{
448
- normalizeName(error);
449
- normalizeMessage(error);
450
- normalizeStack(error);
451
- normalizeCause(error,recurse);
452
- normalizeAggregate(error,recurse);
453
- normalizeDescriptors(error);
454
- };
455
-
456
-
457
-
458
-
459
-
460
-
461
- const normalizeName=(error)=>{
462
- if(isDefinedString$1(error.name)){
463
- return
464
- }
465
-
466
- const prototypeName=Object.getPrototypeOf(error).name;
467
- const name=isDefinedString$1(prototypeName)?
468
- prototypeName:
469
- error.constructor.name;
470
- setErrorProperty$1(error,"name",name);
471
- };
472
-
473
-
474
- const normalizeMessage=(error)=>{
475
- if(!isDefinedString$1(error.message)){
476
- setErrorProperty$1(error,"message","");
477
- }
478
- };
479
-
480
-
481
- const normalizeStack=(error)=>{
482
- if(!isDefinedString$1(error.stack)){
483
- setStack(error);
484
- }
485
- };
486
-
487
- const isDefinedString$1=(value)=>typeof value==="string"&&value!=="";
488
-
489
- const normalizeArgs$1=(error,ErrorClass,currentName=error.name)=>{
490
- validateErrorClass(ErrorClass);
491
-
492
- if(typeof currentName!=="string"){
493
- throw new TypeError(`currentName must be a string: ${currentName}`)
494
- }
495
-
496
- return currentName
497
- };
498
-
499
- const validateErrorClass=(ErrorClass)=>{
500
- if(!isClass(ErrorClass)){
501
- throw new TypeError(`ErrorClass must be a class: ${ErrorClass}`)
502
- }
503
-
504
- if(!isErrorClass(ErrorClass.prototype)){
505
- throw new TypeError(`ErrorClass must inherit from Error: ${ErrorClass}`)
506
- }
507
-
508
- if(!hasConstructor(ErrorClass)){
509
- throw new TypeError(
510
- `ErrorClass must be have a valid constructor: ${ErrorClass}`
511
- )
512
- }
513
- };
514
-
515
- const isClass=(ErrorClass)=>
516
- typeof ErrorClass==="function"&&
517
- typeof ErrorClass.prototype==="object"&&
518
- ErrorClass.prototype!==null;
519
-
520
-
521
- const isErrorClass=(prototype)=>
522
- prototype!==null&&(
523
- prototype.name==="Error"||isErrorClass(Object.getPrototypeOf(prototype)));
524
-
525
- const hasConstructor=(ErrorClass)=>
526
- typeof ErrorClass.prototype.constructor==="function";
527
-
528
- const setNonEnumProp$1=(error,propName,value)=>{
529
-
530
- Object.defineProperty(error,propName,{
531
- value,
532
- enumerable:false,
533
- writable:true,
534
- configurable:true
535
- });
536
- };
537
-
538
- const updatePrototype=(error,ErrorClass)=>{
539
- if(Object.getPrototypeOf(error)===ErrorClass.prototype){
540
- return
541
- }
542
-
543
- setPrototype(error,ErrorClass);
544
- deleteOwnProperty(error,"constructor");
545
- fixName(error,ErrorClass);
546
- };
547
-
548
-
549
-
550
-
551
-
552
- const setPrototype=(error,ErrorClass)=>{
553
-
554
- Object.setPrototypeOf(error,ErrorClass.prototype);
555
- };
556
-
557
-
558
-
559
-
560
-
561
-
562
-
563
-
564
-
565
-
566
- const fixName=(error,ErrorClass)=>{
567
- deleteOwnProperty(error,"name");
568
-
569
- const prototypeName=getClassName(ErrorClass.prototype);
570
-
571
- if(error.name!==prototypeName){
572
- setNonEnumProp$1(error,"name",prototypeName);
573
- }
574
- };
575
-
576
- const getClassName=(prototype)=>
577
- getPrototypeName(prototype)??
578
- getConstructorName(prototype)??
579
- getClassName(Object.getPrototypeOf(prototype));
580
-
581
- const getPrototypeName=(prototype)=>
582
- Object.hasOwn(prototype,"name")&&isDefinedString(prototype.name)?
583
- prototype.name:
584
- undefined;
585
-
586
- const getConstructorName=(prototype)=>
587
- typeof prototype.constructor==="function"&&
588
- isDefinedString(prototype.constructor.name)?
589
- prototype.constructor.name:
590
- undefined;
591
-
592
- const isDefinedString=(value)=>typeof value==="string"&&value!=="";
593
-
594
-
595
-
596
- const deleteOwnProperty=(error,propName)=>{
597
- if(Object.hasOwn(error,propName)){
598
-
599
- delete error[propName];
600
- }
601
- };
602
-
603
- const updateStack$1=(error,currentName)=>{
604
- if(!shouldUpdateStack(error,currentName)){
605
- return
606
- }
607
-
608
- const stack=getStack$2(error,currentName);
609
- setNonEnumProp$1(error,"stack",stack);
610
- };
611
-
612
- const shouldUpdateStack=(error,currentName)=>
613
- currentName!==error.name&&
614
- currentName!==""&&
615
- error.stack.includes(currentName)&&
616
- stackIncludesName();
617
-
618
-
619
- const stackIncludesName=()=>{
620
-
621
- class StackError extends Error{}
622
- const descriptor={
623
- value:EXAMPLE_NAME,
624
- enumerable:false,
625
- writable:true,
626
- configurable:true
627
- };
628
-
629
- Object.defineProperty(StackError,"name",descriptor);
630
-
631
- Object.defineProperty(StackError.prototype,"name",descriptor);
632
- const{stack}=new StackError("");
633
- return typeof stack==="string"&&stack.includes(EXAMPLE_NAME)
634
- };
635
-
636
- const EXAMPLE_NAME="SetErrorClassError";
637
-
638
-
639
-
640
-
641
-
642
-
643
-
644
-
645
- const getStack$2=({name,stack},currentName)=>{
646
- if(stack.startsWith(`${currentName}: `)){
647
- return stack.replace(currentName,name)
648
- }
649
-
650
- const replacers=getReplacers$1(currentName,name);
651
- const[fromA,to]=replacers.find(([from])=>stack.includes(from));
652
- return stack.replace(fromA,to)
653
- };
654
-
655
-
656
-
657
- const getReplacers$1=(currentName,newName)=>[
658
- [`\n${currentName}: `,`\n${newName}: `],
659
- [`${currentName}: `,`${newName}: `],
660
- [`${currentName} `,`${newName} `],
661
- [currentName,newName]];
662
-
663
- const setErrorClass=(error,ErrorClass,currentName)=>{
664
- const errorA=normalizeException(error);
665
- const currentNameA=normalizeArgs$1(errorA,ErrorClass,currentName);
666
- updatePrototype(errorA,ErrorClass);
667
- updateStack$1(errorA,currentNameA);
668
- return errorA
669
- };
670
-
671
- const mergeDescriptors=(newDescriptor,currentDescriptor)=>
672
- currentDescriptor.configurable===false?
673
- mergeNonConfig(newDescriptor,currentDescriptor):
674
- mergeConfig(newDescriptor,currentDescriptor);
675
-
676
-
677
-
678
-
679
- const mergeNonConfig=(newDescriptor,currentDescriptor)=>({
680
- ...currentDescriptor,
681
- ...getNonConfigWritable(newDescriptor,currentDescriptor),
682
- ...getNonConfigValue(newDescriptor,currentDescriptor)
683
- });
684
-
685
- const getNonConfigWritable=(newDescriptor,currentDescriptor)=>
686
- currentDescriptor.writable===true&&newDescriptor.writable===false?
687
- {writable:false}:
688
- {};
689
-
690
- const getNonConfigValue=(newDescriptor,currentDescriptor)=>
691
- newDescriptor.hasValue&&
692
- "value"in currentDescriptor&&
693
- currentDescriptor.writable===true?
694
- {value:newDescriptor.value}:
695
- {};
696
-
697
- const mergeConfig=(newDescriptor,currentDescriptor)=>{
698
- const enumerable=mergeDescriptor(
699
- newDescriptor.enumerable,
700
- currentDescriptor.enumerable,
701
- true
702
- );
703
- const writable=mergeDescriptor(
704
- newDescriptor.writable,
705
- currentDescriptor.writable,
706
- true
707
- );
708
- const configurable=mergeDescriptor(
709
- newDescriptor.configurable,
710
- currentDescriptor.configurable,
711
- true
712
- );
713
- const valueProps=mergeValue(newDescriptor,currentDescriptor,writable);
714
- return {...valueProps,enumerable,configurable}
715
- };
716
-
717
- const mergeValue=(newDescriptor,currentDescriptor,writable)=>{
718
- if(newDescriptor.hasValue){
719
- return {value:newDescriptor.value,writable}
720
- }
721
-
722
- if(!hasGetSet(newDescriptor)&&!hasGetSet(currentDescriptor)){
723
- return {value:currentDescriptor.value,writable}
724
- }
725
-
726
- return {
727
- get:mergeDescriptor(newDescriptor.get,currentDescriptor.get),
728
- set:mergeDescriptor(newDescriptor.set,currentDescriptor.set)
729
- }
730
- };
731
-
732
- const hasGetSet=({get,set})=>get!==undefined||set!==undefined;
733
-
734
- const mergeDescriptor=(newValue,currentValue,defaultValue)=>
735
- newValue??currentValue??defaultValue;
736
-
737
- const normalizeInput=(input,key,newDescriptor)=>{
738
- if(!isAnyObj(input)){
739
- throw new TypeError(`Argument must be an object: ${input}`)
740
- }
741
-
742
- if(!isValidKey(key)){
743
- throw new TypeError(
744
- `Property key must be a string, a symbol or an integer: ${key}`
745
- )
746
- }
747
-
748
- return normalizeDescriptor(newDescriptor)
749
- };
750
-
751
- const isAnyObj=(value)=>typeof value==="object"&&value!==null;
752
-
753
- const isValidKey=(key)=>{
754
- const type=typeof key;
755
- return type==="string"||type==="symbol"||type==="number"
756
- };
757
-
758
- const normalizeDescriptor=(newDescriptor)=>{
759
- if(!isPlainObject(newDescriptor)){
760
- throw new TypeError(`Descriptor must be a plain object: ${newDescriptor}`)
761
- }
762
-
763
- const{
764
- enumerable,
765
- writable,
766
- configurable,
767
- value,
768
- get,
769
- set,
770
- ...unknownProps
771
- }=newDescriptor;
772
- const hasValue="value"in newDescriptor;
773
- validateDescriptor({
774
- enumerable,
775
- writable,
776
- configurable,
777
- get,
778
- set,
779
- unknownProps,
780
- hasValue
781
- });
782
- return {enumerable,writable,configurable,value,get,set,hasValue}
783
- };
784
-
785
- const validateDescriptor=({
786
- enumerable,
787
- writable,
788
- configurable,
789
- get,
790
- set,
791
- unknownProps,
792
- hasValue
793
- })=>{
794
- validateGetSet(hasValue,get,"get");
795
- validateGetSet(hasValue,set,"set");
796
- validateBoolean(enumerable,"enumerable");
797
- validateBoolean(writable,"writable");
798
- validateBoolean(configurable,"configurable");
799
- validateUnknownProps(unknownProps);
800
- };
801
-
802
- const validateGetSet=(hasValue,getSet,propName)=>{
803
- validateFunction(getSet,propName);
804
-
805
- if(hasValue&&getSet!==undefined){
806
- throw new TypeError(
807
- `Descriptor property "value" and "${propName}" must not both be defined: ${getSet}`
808
- )
809
- }
810
- };
811
-
812
- const validateFunction=(propValue,propName)=>{
813
- if(propValue!==undefined&&typeof propValue!=="function"){
814
- throw new TypeError(
815
- `Descriptor property "${propName}" must be a function: ${propValue}`
816
- )
817
- }
818
- };
819
-
820
- const validateBoolean=(propValue,propName)=>{
821
- if(propValue!==undefined&&typeof propValue!=="boolean"){
822
- throw new TypeError(
823
- `Descriptor property "${propName}" must be a boolean: ${propValue}`
824
- )
825
- }
826
- };
827
-
828
- const validateUnknownProps=(unknownProps)=>{
829
- const[unknownProp]=Object.keys(unknownProps);
830
-
831
- if(unknownProp!==undefined){
832
- throw new TypeError(
833
- `Unknown descriptor property "${unknownProp}": ${unknownProps[unknownProp]}`
834
- )
835
- }
836
- };
837
-
838
- const redefineProperty=(input,key,newDescriptor)=>{
839
- const newDescriptorA=normalizeInput(input,key,newDescriptor);
840
- const currentDescriptor=getCurrentDescriptor(input,key);
841
- const finalDescriptor=mergeDescriptors(newDescriptorA,currentDescriptor);
842
- setProperty(input,key,finalDescriptor);
843
- return input
844
- };
845
-
846
-
847
- const getCurrentDescriptor=(input,key)=>{
848
- const descriptor=Object.getOwnPropertyDescriptor(input,key);
849
-
850
- if(descriptor!==undefined){
851
- return descriptor
852
- }
853
-
854
- const prototype=Object.getPrototypeOf(input);
855
- return prototype===null?{}:getCurrentDescriptor(prototype,key)
856
- };
857
-
858
-
859
- const setProperty=(input,key,finalDescriptor)=>{
860
- try{
861
-
862
- Object.defineProperty(input,key,finalDescriptor);
863
- }catch{}
864
- };
865
-
866
- const assignProp=(error,propName,propValue)=>{
867
- if(propValue!==undefined){
868
- return setProp(error,propName,propValue)
869
- }
870
-
871
- try{
872
-
873
- delete error[propName];
874
- }catch{}
875
-
876
- if(error[propName]!==undefined){
877
- return setProp(error,propName)
878
- }
879
- };
880
-
881
- const setProp=(error,propName,propValue)=>{
882
- const nonEnum=getNonEnum(propName);
883
- redefineProperty(error,propName,{value:propValue,...nonEnum});
884
- };
885
-
886
-
887
- const getNonEnum=(propName)=>
888
- typeof propName==="string"&&propName.startsWith("_")?
889
- {enumerable:false}:
890
- {};
891
-
892
- const normalizeOptions=(error,props,opts={})=>{
893
- validateErrorOrObject(error,"First argument");
894
- validateErrorOrObject(props,"Second argument");
895
-
896
- if(!isPlainObject(opts)){
897
- throw new TypeError(`Options must be a plain object: ${opts}`)
898
- }
899
-
900
- const{soft=false}=opts;
901
-
902
- if(typeof soft!=="boolean"){
903
- throw new TypeError(`Option "soft" must be a boolean: ${soft}`)
904
- }
905
-
906
- return {soft}
907
- };
908
-
909
- const validateErrorOrObject=(value,prefix)=>{
910
- if(value===undefined){
911
- throw new TypeError(`${prefix} is required.`)
912
- }
913
-
914
- if(!isErrorOrObject(value)){
915
- throw new TypeError(
916
- `${prefix} must be a plain object or an error: ${value}`
917
- )
918
- }
919
- };
920
-
921
- const isErrorOrObject=(value)=>isPlainObject(value)||isErrorInstance(value);
922
-
923
- const shouldSkipProp=({error,props,propName,soft})=>
924
- isIgnoredPropName(propName)||
925
- !isEnum.call(props,propName)||
926
- soft&&error[propName]!==undefined;
927
-
928
- const isIgnoredPropName=(propName)=>
929
- propName in CHECK_ERROR||IGNORED_PROPS.has(propName);
930
-
931
-
932
-
933
-
934
- const CHECK_ERROR=new Error("check");
935
-
936
-
937
-
938
- const IGNORED_PROPS=new Set(["prototype","errors","cause"]);
939
-
940
- const{propertyIsEnumerable:isEnum}=Object.prototype;
941
-
942
- const setErrorProps=(error,props,opts)=>{
943
- const{soft}=normalizeOptions(error,props,opts);
944
-
945
-
946
- for(const propName of Reflect.ownKeys(props)){
947
- setErrorProp({error,props,propName,soft});
948
- }
949
-
950
- return error
951
- };
952
-
953
-
954
-
955
-
956
-
957
- const setErrorProp=({error,props,propName,soft})=>{
958
- if(!shouldSkipProp({error,props,propName,soft})){
959
- assignProp(error,propName,props[propName]);
960
- }
961
- };
962
-
963
- const setErrorProperty=(error,propName,value)=>{
964
-
965
- Object.defineProperty(error,propName,{
966
- value,
967
- writable:true,
968
- enumerable:false,
969
- configurable:true
970
- });
971
- };
972
-
973
- const mergeAggregateCauses=(parent,recurse)=>{
974
- if(parent.errors===undefined){
975
- return
976
- }
977
-
978
- const errors=parent.errors.
979
- map((error)=>recurse(error).error).
980
- filter(Boolean);
981
- setErrorProperty(parent,"errors",errors);
982
- };
983
-
984
- const mergeAggregateErrors=({target,source,parent,child})=>{
985
- if(!hasErrors(target)){
986
- mergeSourceErrors(target,source);
987
- return
988
- }
989
-
990
- if(hasErrors(source)){
991
- setErrorProperty(target,"errors",[...child.errors,...parent.errors]);
992
- }
993
- };
994
-
995
- const mergeSourceErrors=(target,source)=>{
996
- if(source.errors!==undefined){
997
- setErrorProperty(target,"errors",source.errors);
998
- }
999
- };
1000
-
1001
- const hasErrors=(targetOrSource)=>
1002
- targetOrSource.errors!==undefined&&targetOrSource.errors.length!==0;
1003
-
1004
- const normalizeArgs=(
1005
- error,
1006
- newMessage,
1007
- currentMessage=error.message)=>
1008
- {
1009
- if(typeof newMessage!=="string"){
1010
- throw new TypeError(`newMessage must be a string: ${newMessage}`)
1011
- }
1012
-
1013
- if(typeof currentMessage!=="string"){
1014
- throw new TypeError(`currentMessage must be a string: ${currentMessage}`)
1015
- }
1016
-
1017
- return currentMessage
1018
- };
1019
-
1020
- const getStack$1=({name,stack},newMessage,currentMessage)=>
1021
- currentMessage!==""&&stack.includes(currentMessage)?
1022
- replaceMessage({name,stack,newMessage,currentMessage}):
1023
- insertMessage(name,stack,newMessage);
1024
-
1025
-
1026
-
1027
- const replaceMessage=({name,stack,newMessage,currentMessage})=>{
1028
- const replacers=getReplacers(name,newMessage,currentMessage);
1029
- const[fromA,to]=replacers.find(([from])=>stack.includes(from));
1030
- return stack.replace(fromA,to)
1031
- };
1032
-
1033
-
1034
-
1035
- const getReplacers=(name,newMessage,currentMessage)=>[
1036
- [`${name}: ${currentMessage}`,`${name}: ${newMessage}`],
1037
- [`: ${currentMessage}`,`: ${newMessage}`],
1038
- [`\n${currentMessage}`,`\n${newMessage}`],
1039
- [` ${currentMessage}`,` ${newMessage}`],
1040
- [currentMessage,newMessage]];
1041
-
1042
-
1043
- const insertMessage=(name,stack,newMessage)=>{
1044
- const nameAndColon=`${name}: `;
1045
- const newMessageA=newMessage.trimEnd();
1046
-
1047
- if(stack===name||stack.startsWith(`${name}\n`)){
1048
- return stack.replace(name,`${nameAndColon}${newMessageA}`)
1049
- }
1050
-
1051
- return stack.startsWith(nameAndColon)?
1052
- stack.replace(nameAndColon,`${nameAndColon}${newMessageA}\n`):
1053
- `${nameAndColon}${newMessageA}\n${stack}`
1054
- };
1055
-
1056
- const setErrorMessage=(error,newMessage,currentMessage)=>{
1057
- const errorA=normalizeException(error);
1058
- const currentMessageA=normalizeArgs(errorA,newMessage,currentMessage);
1059
- setNonEnumProp(errorA,"message",newMessage);
1060
- updateStack(errorA,newMessage,currentMessageA);
1061
- return errorA
1062
- };
1063
-
1064
-
1065
-
1066
- const updateStack=(error,newMessage,currentMessage)=>{
1067
- if(newMessage===currentMessage||!stackIncludesMessage()){
1068
- return
1069
- }
1070
-
1071
- const stack=getStack$1(error,newMessage,currentMessage);
1072
- setNonEnumProp(error,"stack",stack);
1073
- };
1074
-
1075
-
1076
- const stackIncludesMessage=()=>{
1077
- const{stack}=new Error(EXAMPLE_MESSAGE);
1078
- return typeof stack==="string"&&stack.includes(EXAMPLE_MESSAGE)
1079
- };
1080
-
1081
- const EXAMPLE_MESSAGE="set-error-message test message";
1082
-
1083
- const setNonEnumProp=(error,propName,value)=>{
1084
-
1085
- Object.defineProperty(error,propName,{
1086
- value,
1087
- enumerable:false,
1088
- writable:true,
1089
- configurable:true
1090
- });
1091
- };
1092
-
1093
- const wrapErrorMessage=(error,newMessage,oldMessage)=>{
1094
- if(typeof newMessage!=="string"){
1095
- throw new TypeError(
1096
- `Second argument must be a message string: ${newMessage}`
1097
- )
1098
- }
1099
-
1100
- const errorA=normalizeException(error);
1101
- const message=getMessage(newMessage,errorA.message);
1102
- return setErrorMessage(errorA,message,oldMessage)
1103
- };
1104
-
1105
-
1106
-
1107
-
1108
-
1109
-
1110
-
1111
-
1112
-
1113
- const getMessage=(rawNewMessage,rawCurrentMessage)=>{
1114
- const newMessage=rawNewMessage.trim();
1115
- const currentMessage=rawCurrentMessage.trim();
1116
-
1117
- if(newMessage===""){
1118
- return currentMessage
1119
- }
1120
-
1121
- if(currentMessage===""){
1122
- return newMessage
1123
- }
1124
-
1125
- return concatMessages(newMessage,currentMessage,rawNewMessage)
1126
- };
1127
-
1128
- const concatMessages=(newMessage,currentMessage,rawNewMessage)=>{
1129
- if(!newMessage.endsWith(PREPEND_CHAR)){
1130
- return `${currentMessage}\n${newMessage}`
1131
- }
1132
-
1133
- return rawNewMessage.endsWith(PREPEND_NEWLINE_CHAR)?
1134
- `${newMessage}\n${currentMessage}`:
1135
- `${newMessage} ${currentMessage}`
1136
- };
1137
-
1138
- const PREPEND_CHAR=":";
1139
- const PREPEND_NEWLINE_CHAR="\n";
1140
-
1141
- const mergeMessage=({parent,child,target,stackError})=>{
1142
- const parentMessage=parent.message;
1143
- const stackErrorMessage=stackError.message;
1144
-
1145
- target.message=child.message;
1146
- return wrapErrorMessage(target,parentMessage,stackErrorMessage)
1147
- };
1148
-
1149
- const hasStack=(error,stack)=>getStack(error)===stack;
1150
-
1151
-
1152
-
1153
-
1154
- const getStack=(error)=>
1155
- typeof error==="object"&&error!==null?error.stack:undefined;
1156
-
1157
-
1158
-
1159
-
1160
-
1161
-
1162
-
1163
-
1164
-
1165
-
1166
- const mergeStack=({wrap,target,source,childHasStack})=>{
1167
- if(wrap===childHasStack){
1168
- return target
1169
- }
1170
-
1171
- setErrorProperty(target,"stack",source.stack);
1172
- return source
1173
- };
1174
-
1175
- const getWrap=(parent)=>{
1176
- const{wrap,name}=parent;
1177
-
1178
- if(typeof wrap!=="boolean"){
1179
- return name==="Error"
1180
- }
1181
-
1182
- if(Object.hasOwn(parent,"wrap")){
1183
-
1184
- delete parent.wrap;
1185
- }
1186
-
1187
- return wrap
1188
- };
1189
-
1190
- const mergeErrorCause=(error)=>mergeError(error,[]).error;
1191
-
1192
-
1193
-
1194
-
1195
-
1196
-
1197
- const mergeError=(error,parents)=>{
1198
- if(parents.includes(error)){
1199
- return {}
1200
- }
1201
-
1202
- const recurse=(innerError)=>mergeError(innerError,[...parents,error]);
1203
- const stack=getStack(error);
1204
- const errorA=normalizeException(error,{shallow:true});
1205
- const parentHasStack=hasStack(errorA,stack);
1206
-
1207
- mergeAggregateCauses(errorA,recurse);
1208
- const{parent:errorB,childHasStack}=mergeCause(errorA,recurse);
1209
- const errorHasStack=parentHasStack||childHasStack;
1210
- return {error:errorB,errorHasStack}
1211
- };
1212
-
1213
-
1214
-
1215
- const mergeCause=(parent,recurse)=>{
1216
- const wrap=getWrap(parent);
1217
-
1218
- if(parent.cause===undefined){
1219
- return {parent,childHasStack:false}
1220
- }
1221
-
1222
- const{error:child,errorHasStack:childHasStack}=recurse(parent.cause);
1223
-
1224
- delete parent.cause;
1225
- const parentA=mergeChild({parent,child,childHasStack,wrap});
1226
- return {parent:parentA,childHasStack}
1227
- };
1228
-
1229
- const mergeChild=({parent,child,childHasStack,wrap})=>{
1230
- if(child===undefined){
1231
- return parent
1232
- }
1233
-
1234
- const[target,source]=wrap?[child,parent]:[parent,child];
1235
- const stackError=mergeStack({wrap,target,source,childHasStack});
1236
- const targetA=setErrorClass(target,target.constructor,stackError.name);
1237
- const targetB=mergeMessage({parent,child,target:targetA,stackError});
1238
- mergeAggregateErrors({target:targetB,source,parent,child});
1239
- const targetC=setErrorProps(targetB,source,{soft:!wrap});
1240
- return targetC
1241
- };
1242
-
1243
- function safeStringifyReplacer(seen) {
1244
- const replacer = function (key, value) {
1245
- // Handle objects with a custom `.toJSON()` method.
1246
- if (typeof value?.toJSON === 'function') {
1247
- value = value.toJSON();
1248
- }
1249
-
1250
- if (!(value !== null && typeof value === 'object')) {
1251
- return value;
1252
- }
1253
-
1254
- if (seen.has(value)) {
1255
- return '[Circular]';
1256
- }
1257
-
1258
- seen.add(value);
1259
-
1260
- const newValue = Array.isArray(value) ? [] : {};
1261
-
1262
- for (const [key2, value2] of Object.entries(value)) {
1263
- newValue[key2] = replacer(key2, value2);
1264
- }
1265
-
1266
- seen.delete(value);
1267
-
1268
- return newValue;
1269
- };
1270
-
1271
- return replacer;
1272
- }
1273
-
1274
- function safeStringify(object, {indentation} = {}) {
1275
- const seen = new WeakSet();
1276
- return JSON.stringify(object, safeStringifyReplacer(seen), indentation);
1277
- }
1
+ import { n as main_default$1, t as main_default } from "./_chunks/libs/merge-error-cause.mjs";
2
+ import { t as safeStringify } from "./_chunks/libs/safe-stringify.mjs";
3
+ import { configureSync, getAnsiColorFormatter as getAnsiColorFormatter$1, getConsoleSink as getConsoleSink$1, getLogger, getTextFormatter as getTextFormatter$1, withFilter } from "@logtape/logtape";
1278
4
 
5
+ //#region src/utils.ts
1279
6
  const parseStack = (stack) => {
1280
- const lines = stack.split("\n").map((l) => {
1281
- let replaced = l.trim().replace("file://", "");
1282
- return replaced;
1283
- });
1284
- return lines;
7
+ return stack.split("\n").map((l) => {
8
+ return l.trim().replace("file://", "");
9
+ });
1285
10
  };
1286
11
  function indent(value, indentation = 2) {
1287
- value = Array.isArray(value) ? value : value.split("\n");
1288
- const prefix = " ".repeat(indentation);
1289
- return value.map((l) => `${prefix}${l}`).join("\n");
12
+ value = Array.isArray(value) ? value : value.split("\n");
13
+ const prefix = " ".repeat(indentation);
14
+ return value.map((l) => `${prefix}${l}`).join("\n");
1290
15
  }
1291
16
  function loggable(error) {
1292
- if (typeof error === "string") {
1293
- return error;
1294
- }
1295
- if (error instanceof Error) {
1296
- const mergedErr = mergeErrorCause(error);
1297
- if (mergedErr.stack) {
1298
- const stack = parseStack(mergedErr.stack);
1299
- return mergedErr.message + "\n" + indent(stack.slice(1));
1300
- }
1301
- return mergedErr.message;
1302
- }
1303
- return safeStringify(error, { indentation: " " });
1304
- }
17
+ if (typeof error === "string") return error;
18
+ if (error instanceof Error) {
19
+ const mergedErr = main_default(error);
20
+ if (mergedErr.stack) {
21
+ const stack = parseStack(mergedErr.stack);
22
+ return mergedErr.message + "\n" + indent(stack.slice(1));
23
+ }
24
+ return mergedErr.message;
25
+ }
26
+ return safeStringify(error, { indentation: " " });
27
+ }
28
+ /**
29
+ * Appends `message` to `error.message`. If `message` ends with `:` or `:\n`,
30
+ * prepends it instead.
31
+ *
32
+ * Returns `error`. If `error` is not an `Error` instance, it is converted to
33
+ * one.
34
+ *
35
+ * @example
36
+ * ```js
37
+ * wrapErrorMessage(new Error('Message.'), 'Additional message.')
38
+ * // Error: Message.
39
+ * // Additional message.
40
+ *
41
+ * wrapErrorMessage(new Error('Message.'), 'Additional message:')
42
+ * // Error: Additional message: Message.
43
+ *
44
+ * wrapErrorMessage(new Error('Message.'), 'Additional message:\n')
45
+ * // Error: Additional message:
46
+ * // Message.
47
+ *
48
+ * wrapErrorMessage(new Error('Message.'), '')
49
+ * // Error: Message.
50
+ *
51
+ * const invalidError = 'Message.'
52
+ * wrapErrorMessage(invalidError, 'Additional message.')
53
+ * // Error: Message.
54
+ * // Additional message.
55
+ *
56
+ * wrapErrorMessage(new Error(' Message with spaces '), ' Additional message ')
57
+ * // Error: Message with spaces
58
+ * // Additional message
59
+ * ```
60
+ */
1305
61
  function wrapError(error, newMessage) {
1306
- return wrapErrorMessage(error, newMessage);
62
+ return main_default$1(error, newMessage);
1307
63
  }
1308
64
 
65
+ //#endregion
66
+ //#region src/formatters.ts
1309
67
  function gerErrorFromLogRecord(record) {
1310
- const errors = Object.entries(record.properties).flatMap(([k, err]) => {
1311
- if (err instanceof Error) {
1312
- const mergedErr = mergeErrorCause(err);
1313
- if (mergedErr.stack) {
1314
- mergedErr.stack = parseStack(mergedErr.stack).join("\n");
1315
- }
1316
- return [mergedErr];
1317
- }
1318
- if (k === "error" || k === "err") {
1319
- return [new Error(`${err}`)];
1320
- }
1321
- return [];
1322
- });
1323
- if (errors.length === 0) {
1324
- return null;
1325
- }
1326
- return errors.length === 1 ? errors[0] : new AggregateError(errors);
68
+ const errors = Object.entries(record.properties).flatMap(([k, err]) => {
69
+ if (err instanceof Error) {
70
+ const mergedErr = main_default(err);
71
+ if (mergedErr.stack) mergedErr.stack = parseStack(mergedErr.stack).join("\n");
72
+ return [mergedErr];
73
+ }
74
+ if (k === "error" || k === "err") return [/* @__PURE__ */ new Error(`${err}`)];
75
+ return [];
76
+ });
77
+ if (errors.length === 0) return null;
78
+ return errors.length === 1 ? errors[0] : new AggregateError(errors);
1327
79
  }
1328
80
  function errorFromLogRecord(record) {
1329
- const error = gerErrorFromLogRecord(record);
1330
- if (error && typeof record.rawMessage === "string") {
1331
- return wrapErrorMessage(error, record.rawMessage + "\n");
1332
- }
1333
- return error;
81
+ const error = gerErrorFromLogRecord(record);
82
+ if (error && typeof record.rawMessage === "string") return main_default$1(error, record.rawMessage + "\n");
83
+ return error;
1334
84
  }
1335
85
  function appendErrorToMessage(values, color = false) {
1336
- const error = gerErrorFromLogRecord(values.record);
1337
- if (error) {
1338
- let errorMessge = error.message;
1339
- if (error.stack) {
1340
- errorMessge = errorMessge + "\n" + indent(error.stack.split("\n").slice(1));
1341
- }
1342
- if (color) {
1343
- errorMessge = `${ansiColors.red}${errorMessge}${RESET}`;
1344
- }
1345
- return {
1346
- ...values,
1347
- message: values.message + "\n" + indent(errorMessge)
1348
- };
1349
- }
1350
- return values;
86
+ const error = gerErrorFromLogRecord(values.record);
87
+ if (error) {
88
+ let errorMessge = error.message;
89
+ if (error.stack) errorMessge = errorMessge + "\n" + indent(error.stack.split("\n").slice(1));
90
+ if (color) errorMessge = `${ansiColors.red}${errorMessge}${RESET}`;
91
+ return {
92
+ ...values,
93
+ message: values.message + "\n" + indent(errorMessge)
94
+ };
95
+ }
96
+ return values;
1351
97
  }
1352
98
  const levelAbbreviations = {
1353
- "trace": "TRACE",
1354
- "debug": "DEBUG",
1355
- "info": "INFO ",
1356
- "warning": "WARN ",
1357
- "error": "ERROR",
1358
- "fatal": "FATAL"
99
+ "trace": "TRACE",
100
+ "debug": "DEBUG",
101
+ "info": "INFO ",
102
+ "warning": "WARN ",
103
+ "error": "ERROR",
104
+ "fatal": "FATAL"
1359
105
  };
1360
106
  function getMessageOnlyFormatter() {
1361
- return getTextFormatter({
1362
- format: ({ message }) => {
1363
- return message;
1364
- }
1365
- });
107
+ return getTextFormatter({ format: ({ message }) => {
108
+ return message;
109
+ } });
1366
110
  }
1367
111
  const level = (l) => levelAbbreviations[l];
1368
112
  function getTextFormatter(options) {
1369
- const _format = options?.format ?? (({ timestamp, level: level2, category, message }) => {
1370
- return `${timestamp} ${level2} ${category} ${message}`;
1371
- });
1372
- return getTextFormatter$1({
1373
- timestamp: "time",
1374
- level,
1375
- category: ".",
1376
- ...options,
1377
- format: (values) => {
1378
- return _format(appendErrorToMessage(values));
1379
- }
1380
- });
113
+ const _format = options?.format ?? (({ timestamp, level, category, message }) => {
114
+ return `${timestamp} ${level} ${category} ${message}`;
115
+ });
116
+ return getTextFormatter$1({
117
+ timestamp: "time",
118
+ level,
119
+ category: ".",
120
+ ...options,
121
+ format: (values) => {
122
+ return _format(appendErrorToMessage(values));
123
+ }
124
+ });
1381
125
  }
1382
126
  const RESET = "\x1B[0m";
1383
- const ansiColors = {
1384
- // black: "\x1b[30m",
1385
- red: "\x1B[31m"
1386
- // green: "\x1b[32m",
1387
- // yellow: "\x1b[33m",
1388
- // blue: "\x1b[34m",
1389
- // magenta: "\x1b[35m",
1390
- // cyan: "\x1b[36m",
1391
- // white: "\x1b[37m",
1392
- };
127
+ const ansiColors = { red: "\x1B[31m" };
1393
128
  function getAnsiColorFormatter(options) {
1394
- const _format = options?.format ?? (({ timestamp, level: level2, category, message }) => {
1395
- return `${timestamp} ${level2} ${category} ${message}`;
1396
- });
1397
- return getAnsiColorFormatter$1({
1398
- timestamp: "time",
1399
- level,
1400
- categoryStyle: "bold",
1401
- categoryColor: "cyan",
1402
- category: ".",
1403
- ...options,
1404
- format: (values) => {
1405
- return _format(appendErrorToMessage(values, true));
1406
- }
1407
- });
129
+ const _format = options?.format ?? (({ timestamp, level, category, message }) => {
130
+ return `${timestamp} ${level} ${category} ${message}`;
131
+ });
132
+ return getAnsiColorFormatter$1({
133
+ timestamp: "time",
134
+ level,
135
+ categoryStyle: "bold",
136
+ categoryColor: "cyan",
137
+ category: ".",
138
+ ...options,
139
+ format: (values) => {
140
+ return _format(appendErrorToMessage(values, true));
141
+ }
142
+ });
143
+ }
144
+ /**
145
+ * The formatter returns an array where:
146
+ * - First element is the formatted message string
147
+ * - Second element is the record properties object
148
+ */
149
+ function getConsoleFormatter(options) {
150
+ const formatter = options?.messageFormatter;
151
+ if (formatter) return (record) => {
152
+ const { properties } = record;
153
+ if (properties && Object.keys(properties).length > 0) return [formatter(record), properties];
154
+ return [formatter(record)];
155
+ };
156
+ return (record) => {
157
+ const { message, properties } = record;
158
+ if (properties && Object.keys(properties).length > 0) return [...message, properties];
159
+ return message;
160
+ };
1408
161
  }
162
+
163
+ //#endregion
164
+ //#region src/sink.ts
1409
165
  function getConsoleSink(options) {
1410
- return getConsoleSink$1({
1411
- formatter: getAnsiColorFormatter(),
1412
- ...options
1413
- });
1414
- }
166
+ return getConsoleSink$1({
167
+ formatter: getConsoleFormatter(),
168
+ ...options
169
+ });
170
+ }
171
+ /**
172
+ * Creates a console sink that writes to stderr.
173
+ * (MCP protocol requires stderr to be used for logging)
174
+ */
1415
175
  function getConsoleStderrSink(options) {
1416
- const formatter = options?.formatter ?? getTextFormatter();
1417
- return (record) => {
1418
- const args = formatter(record);
1419
- if (typeof args === "string") {
1420
- const msg = args.replace(/\r?\n$/, "");
1421
- console.error(msg);
1422
- } else {
1423
- console.error(...args);
1424
- }
1425
- };
176
+ const formatter = options?.formatter ?? getConsoleFormatter();
177
+ return (record) => {
178
+ const args = formatter(record);
179
+ if (typeof args === "string") {
180
+ const msg = args.replace(/\r?\n$/, "");
181
+ console.error(msg);
182
+ } else console.error(...args);
183
+ };
1426
184
  }
1427
185
 
186
+ //#endregion
187
+ //#region src/index.ts
1428
188
  const logger = getLogger("likec4");
189
+ /**
190
+ * Get a child logger with the given subcategory.
191
+ *
192
+ * @param subcategory The subcategory.
193
+ * @returns The child logger.
194
+ */
1429
195
  function createLogger(subcategory) {
1430
- return logger.getChild(subcategory);
196
+ return logger.getChild(subcategory);
1431
197
  }
1432
198
  function configureLogger(config) {
1433
- try {
1434
- const sinks = config?.sinks ?? {};
1435
- configureSync({
1436
- reset: true,
1437
- ...config,
1438
- sinks: {
1439
- console: getConsoleSink(),
1440
- ...sinks
1441
- },
1442
- loggers: [
1443
- { category: ["logtape", "meta"], sinks: ["console"], lowestLevel: "warning" },
1444
- ...config?.loggers ?? [
1445
- {
1446
- category: "likec4",
1447
- sinks: ["console"],
1448
- lowestLevel: "debug"
1449
- }
1450
- ]
1451
- ]
1452
- });
1453
- } catch (e) {
1454
- console.error(e);
1455
- }
199
+ try {
200
+ const sinks = config?.sinks ?? {};
201
+ configureSync({
202
+ reset: true,
203
+ ...config,
204
+ sinks: {
205
+ console: getConsoleSink(),
206
+ ...sinks
207
+ },
208
+ loggers: [{
209
+ category: ["logtape", "meta"],
210
+ sinks: ["console"],
211
+ lowestLevel: "warning"
212
+ }, ...config?.loggers ?? [{
213
+ category: "likec4",
214
+ sinks: ["console"],
215
+ lowestLevel: "debug"
216
+ }]]
217
+ });
218
+ } catch (e) {
219
+ console.error(e);
220
+ }
1456
221
  }
1457
222
 
1458
- export { configureLogger, logger as consola, createLogger, errorFromLogRecord, getAnsiColorFormatter, getConsoleSink, getConsoleStderrSink, getMessageOnlyFormatter, getTextFormatter, loggable, logger, logger as rootLogger, wrapError };
223
+ //#endregion
224
+ export { configureLogger, logger as consola, logger, logger as rootLogger, createLogger, errorFromLogRecord, getAnsiColorFormatter, getConsoleFormatter, getConsoleSink, getConsoleStderrSink, getMessageOnlyFormatter, getTextFormatter, loggable, withFilter, wrapError };