-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctx_test.go
783 lines (629 loc) · 22.5 KB
/
ctx_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
/*
* Copyright 2022 Mandelsoft. All rights reserved.
* This file is licensed under the Apache Software License, v. 2 except as noted
* otherwise in the LICENSE file
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package logging_test
import (
"bytes"
"fmt"
"os"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/sirupsen/logrus"
"github.com/tonglil/buflogr"
"github.com/mandelsoft/logging"
"github.com/mandelsoft/logging/logrusl"
"github.com/mandelsoft/logging/logrusl/adapter"
"github.com/mandelsoft/logging/logrusr"
"github.com/mandelsoft/logging/utils"
)
var pkg = logging.Package()
var _ = Describe("context test", func() {
var buf bytes.Buffer
var ctx logging.Context
buflogr.NameSeparator = ":"
BeforeEach(func() {
buf.Reset()
def := buflogr.NewWithBuffer(&buf)
ctx = logging.New(def)
})
It("just default logs", func() {
ctx.Logger().Trace("trace")
ctx.Logger().Debug("debug")
ctx.Logger().Info("info")
ctx.Logger().Warn("warn")
ctx.Logger().Error("error")
ctx.Logger().LogError(fmt.Errorf("message"), "error")
fmt.Printf("%s\n", buf.String())
fmt.Printf("%s\n", buf.String())
Expect("\n" + buf.String()).To(Equal(`
V[3] info
V[2] warn
ERROR <nil> error
ERROR message error
`))
})
It("just default logs with package", func() {
ctx.Logger(pkg).Trace("trace")
ctx.Logger(pkg).Debug("debug")
ctx.Logger(pkg).Info("info")
ctx.Logger(pkg).Warn("warn")
ctx.Logger(pkg).Error("error")
fmt.Printf("%s\n", buf.String())
Expect("\n" + buf.String()).To(Equal(`
V[3] info realm github.com/mandelsoft/logging_test
V[2] warn realm github.com/mandelsoft/logging_test
ERROR <nil> error realm github.com/mandelsoft/logging_test
`))
})
It("just logs", func() {
ctx.SetDefaultLevel(9)
ctx.Logger().Trace("trace")
ctx.Logger().Debug("debug")
ctx.Logger().Info("info")
ctx.Logger().Warn("warn")
ctx.Logger().Error("error")
fmt.Printf("%s\n", buf.String())
Expect("\n" + buf.String()).To(Equal(`
V[5] trace
V[4] debug
V[3] info
V[2] warn
ERROR <nil> error
`))
})
It("just logs with level rule", func() {
ctx.AddRule(logging.NewConditionRule(logging.WarnLevel))
ctx.Logger().Trace("trace")
ctx.Logger().Debug("debug")
ctx.Logger().Info("info")
ctx.Logger().Warn("warn")
ctx.Logger().Error("error")
fmt.Printf("%s\n", buf.String())
Expect("\n" + buf.String()).To(Equal(`
V[2] warn
ERROR <nil> error
`))
})
It("just logs with context realm rule", func() {
realm := logging.NewRealm("realm")
ctx.AddRule(logging.NewConditionRule(logging.WarnLevel))
ctx.AddRule(logging.NewConditionRule(logging.DebugLevel, realm))
ctx.Logger().Trace("trace")
ctx.Logger().Debug("debug")
ctx.Logger().Info("info")
ctx.Logger().Warn("warn")
ctx.Logger().Error("error")
ctx.Logger(realm).Debug("test debug")
fmt.Printf("%s\n", buf.String())
Expect("\n" + buf.String()).To(Equal(`
V[2] warn
ERROR <nil> error
V[4] test debug realm realm
`))
})
It("just logs with context realm prefix rule", func() {
realm := logging.NewRealm("prefix/realm")
prefix := logging.NewRealmPrefix("prefix")
ctx.AddRule(logging.NewConditionRule(logging.WarnLevel))
ctx.AddRule(logging.NewConditionRule(logging.DebugLevel, prefix))
ctx.Logger().Trace("trace")
ctx.Logger().Debug("debug")
ctx.Logger().Info("info")
ctx.Logger().Warn("warn")
ctx.Logger().Error("error")
ctx.Logger(realm).Debug("test prefix debug")
fmt.Printf("%s\n", buf.String())
Expect("\n" + buf.String()).To(Equal(`
V[2] warn
ERROR <nil> error
V[4] test prefix debug realm prefix/realm
`))
})
It("just logs with context value rule", func() {
realm := logging.NewRealm("realm")
attr := logging.NewAttribute("attr", "test")
ctx.AddRule(logging.NewConditionRule(logging.WarnLevel))
ctx.AddRule(logging.NewConditionRule(logging.DebugLevel, realm, attr))
ctx.Logger(realm).Debug("test debug")
ctx.Logger(realm, attr).Debug("test debug value")
fmt.Printf("%s\n", buf.String())
Expect("\n" + buf.String()).To(Equal(`
V[4] test debug value realm realm attr test
`))
})
It("just logs with context NOT value rule", func() {
realm := logging.NewRealm("realm")
attr := logging.NewAttribute("attr", "test")
ctx.AddRule(logging.NewConditionRule(logging.WarnLevel))
ctx.AddRule(logging.NewConditionRule(logging.DebugLevel, realm, logging.Not(attr)))
ctx.Logger(realm).Debug("test debug")
ctx.Logger(realm, attr).Debug("test debug value")
fmt.Printf("%s\n", buf.String())
Expect("\n" + buf.String()).To(Equal(`
V[4] test debug realm realm
`))
})
It("just logs with context OR value rule", func() {
realm := logging.NewRealm("realm")
attr := logging.NewAttribute("attr", "test")
attr2 := logging.NewAttribute("attr", "other")
ctx.AddRule(logging.NewConditionRule(logging.WarnLevel))
ctx.AddRule(logging.NewConditionRule(logging.DebugLevel, realm, logging.Or(attr2, attr)))
ctx.Logger(realm).Debug("test debug")
ctx.Logger(realm, attr).Debug("test debug value")
ctx.Logger(realm, attr2).Debug("test debug value")
fmt.Printf("%s\n", buf.String())
Expect("\n" + buf.String()).To(Equal(`
V[4] test debug value realm realm attr test
V[4] test debug value realm realm attr other
`))
})
It("just logs with context AND value rule", func() {
realm := logging.NewRealm("realm")
attr := logging.NewAttribute("attr", "test")
attr2 := logging.NewAttribute("attr", "other")
ctx.AddRule(logging.NewConditionRule(logging.WarnLevel))
ctx.AddRule(logging.NewConditionRule(logging.DebugLevel, realm, logging.And(attr2, attr)))
ctx.Logger(realm).Debug("test debug")
ctx.Logger(realm, attr).Debug("test debug value")
ctx.Logger(realm, attr2).Debug("test debug value")
ctx.Logger(realm, attr, attr2).Debug("test debug value")
fmt.Printf("%s\n", buf.String())
Expect("\n" + buf.String()).To(Equal(`
V[4] test debug value realm realm attr test attr other
`))
})
It("handles regular error level", func() {
ctx.Logger().V(logging.ErrorLevel).Info("error")
fmt.Printf("%s\n", buf.String())
Expect("\n" + buf.String()).To(Equal(`
V[1] error
`))
})
Context("package realm", func() {
local := "github.com/mandelsoft/logging_test"
It("provides package", func() {
Expect(pkg.Name()).To(Equal(local))
})
It("provides package in function expression", func() {
Expect(logging.Package().Name()).To(Equal("github.com/mandelsoft/logging_test"))
})
It("provides package in struct nested function", func() {
Expect((&X{}).Package().Name()).To(Equal("github.com/mandelsoft/logging_test"))
})
})
Context("with realm and name", func() {
It("adds realm and name", func() {
realm := logging.NewRealm("test")
ctx = ctx.WithContext(logging.NewName("name"))
enriched := ctx.WithContext(realm, logging.NewName("sub"))
ctx.AddRule(logging.NewConditionRule(logging.DebugLevel, realm))
enriched.Logger().Debug("debug pkg")
enriched.Logger().WithName("cmd").Info("info pkg")
ctx.Logger().Debug("debug plain")
ctx.Logger().Info("info plain")
fmt.Printf("\n" + buf.String())
Expect("\n" + buf.String()).To(Equal(`
V[4] name:sub debug pkg realm test
V[3] name:sub:cmd info pkg realm test
V[3] name info plain
`))
})
})
Context("with standard message context", func() {
It("adds standard message context", func() {
realm := logging.NewRealm("test")
enriched := ctx.WithContext(realm)
ctx.AddRule(logging.NewConditionRule(logging.DebugLevel, realm))
enriched.Logger().Debug("debug pkg")
enriched.Logger().Info("info pkg")
ctx.Logger().Debug("debug plain")
ctx.Logger().Info("info plain")
fmt.Printf("\n" + buf.String())
Expect("\n" + buf.String()).To(Equal(`
V[4] debug pkg realm test
V[3] info pkg realm test
V[3] info plain
`))
})
It("adds nested standard message context", func() {
realm := logging.NewRealm("test")
enriched := ctx.WithContext(realm)
enriched2 := enriched.WithContext(logging.NewRealm("detail"))
ctx.AddRule(logging.NewConditionRule(logging.DebugLevel, realm))
enriched2.Logger().Debug("debug detail")
enriched2.Logger().Info("info detail")
enriched.Logger().Debug("debug pkg")
enriched.Logger().Info("info pkg")
ctx.Logger().Debug("debug plain")
ctx.Logger().Info("info plain")
fmt.Printf("\n" + buf.String())
Expect("\n" + buf.String()).To(Equal(`
V[3] info detail realm test realm detail
V[4] debug pkg realm test
V[3] info pkg realm test
V[3] info plain
`))
})
})
Context("nested contexts", func() {
var nested logging.Context
BeforeEach(func() {
nested = logging.NewWithBase(ctx)
})
It("just logs with context realm rule", func() {
realm := logging.NewRealm("realm")
ctx.AddRule(logging.NewConditionRule(logging.WarnLevel))
ctx.AddRule(logging.NewConditionRule(logging.DebugLevel, realm))
nested.Logger().Trace("trace")
nested.Logger().Debug("debug")
nested.Logger().Info("info")
nested.Logger().Warn("warn")
nested.Logger().Error("error")
nested.Logger(realm).Debug("test debug")
fmt.Printf("%s\n", buf.String())
Expect("\n" + buf.String()).To(Equal(`
V[2] warn
ERROR <nil> error
V[4] test debug realm realm
`))
})
It("just logs with nested context realm rule", func() {
realm := logging.NewRealm("realm")
ctx.AddRule(logging.NewConditionRule(logging.WarnLevel))
nested.AddRule(logging.NewConditionRule(logging.DebugLevel, realm))
nested.Logger().Trace("trace")
nested.Logger().Debug("debug")
nested.Logger().Info("info")
nested.Logger().Warn("warn")
nested.Logger().Error("error")
nested.Logger(realm).Debug("test debug")
ctx.Logger(realm).Debug("ctx test debug")
fmt.Printf("%s\n", buf.String())
Expect("\n" + buf.String()).To(Equal(`
V[2] warn
ERROR <nil> error
V[4] test debug realm realm
`))
})
Context("dynamic changes", func() {
It("adapts provided default logger", func() {
logger := nested.Logger()
logger.Info("info before")
logger.Debug("debug before")
ctx.SetDefaultLevel(logging.DebugLevel)
logger.Info("info after")
logger.Debug("debug after")
Expect("\n" + buf.String()).To(Equal(`
V[3] info before
V[3] info after
V[4] debug after
`))
})
It("adapts provided sink", func() {
var buf bytes.Buffer
def := buflogr.NewWithBuffer(&buf)
ctx.SetBaseLogger(def)
logger := nested.Logger()
logger.Info("info before")
logger.Debug("debug before")
ctx.SetDefaultLevel(logging.DebugLevel)
logger.Info("info after")
logger.Debug("debug after")
Expect("\n" + buf.String()).To(Equal(`
V[3] info before
V[3] info after
V[4] debug after
`))
})
It("adapts provided ruke based logger", func() {
tag := logging.NewTag("tag")
nested.AddRule(logging.NewConditionRule(logging.DebugLevel, tag))
logger := nested.Logger(tag)
var buf bytes.Buffer
def := buflogr.NewWithBuffer(&buf)
ctx.SetBaseLogger(def)
logger.Info("info before")
logger.Debug("debug before")
ctx.SetDefaultLevel(logging.DebugLevel)
logger.Info("info after")
logger.Debug("debug after")
Expect("\n" + buf.String()).To(Equal(`
V[3] info before
V[4] debug before
V[3] info after
V[4] debug after
`))
})
})
Context("update nested contexts", func() {
It("plain", func() {
Expect(nested.Tree().Updater().SeenWatermark()).To((Equal(int64(0))))
Expect(nested.Tree().Updater().SeenWatermark()).To(Equal(nested.Tree().Updater().Watermark()))
})
It("plain", func() {
ctx.SetDefaultLevel(9)
Expect(nested.GetDefaultLevel()).To((Equal(9)))
Expect(nested.Tree().Updater().SeenWatermark()).To((Equal(int64(1))))
Expect(nested.Tree().Updater().SeenWatermark()).To(Equal(nested.Tree().Updater().Watermark()))
})
})
})
Context("shifted", func() {
var shifted logging.Context
BeforeEach(func() {
shifted = logging.New(buflogr.NewWithBuffer(&buf).V(2))
})
It("shift initial level", func() {
shifted.Logger().Info("info")
Expect("\n" + buf.String()).To(Equal(`
V[5] info
`))
})
It("keeps error", func() {
shifted.Logger().Error("error")
Expect("\n" + buf.String()).To(Equal(`
ERROR <nil> error
`))
})
It("disables error", func() {
shifted.SetDefaultLevel(0)
shifted.Logger().Error("error")
Expect(buf.String()).To(Equal(""))
})
})
Context("deriving standard message context", func() {
It("inherits context", func() {
name := logging.NewName("test")
enriched := ctx.WithContext(name)
eff := logging.NewWithBase(enriched)
eff.Logger().Error("with realm")
Expect(buf.String()).To(Equal("ERROR <nil> test with realm\n"))
})
It("extended message context", func() {
name := logging.NewName("test")
enriched := ctx.WithContext(name)
eff := logging.NewWithBase(enriched)
ext := logging.NewName("extended")
eff.Logger(ext).Error("with realm")
Expect(buf.String()).To(Equal("ERROR <nil> test:extended with realm\n"))
})
})
Context("logrus human", func() {
BeforeEach(func() {
ctx = logrusl.WithWriter(&buf).Human().New()
ctx.SetDefaultLevel(logging.InfoLevel)
})
It("regular", func() {
ctx.Logger().Info("test", logrus.FieldKeyLevel, 1000)
Expect(buf.String()).To(MatchRegexp(`.{25} info test fields.level=1000\n`))
})
It("substitution", func() {
ctx.Logger().Info("test {{value}}", "value", "test")
Expect(buf.String()).To(MatchRegexp(`.{25} info "test test"`))
buf.Reset()
ctx.Logger().Info("test {{value}}", "value", 4711)
Expect(buf.String()).To(MatchRegexp(`.{25} info "test 4711"`))
buf.Reset()
ctx.Logger().Info("test {{value}}", "value", true)
Expect(buf.String()).To(MatchRegexp(`.{25} info "test true"`))
buf.Reset()
ctx.Logger().Info("test {{value}}", "value", struct {
Field1 string `json:"field1"`
Field2 string `json:"field2"`
}{
Field1: "value1",
Field2: "value2",
})
Expect(buf.String()).To(MatchRegexp(`.{25} info "test {\\"field1\\":\\"value1\\",\\"field2\\":\\"value2\\"}"`))
})
})
Context("name and realm handling", func() {
BeforeEach(func() {
ctx = logrusl.Human().WithWriter(&buf).New()
ctx.SetDefaultLevel(logging.InfoLevel)
})
It("adds name to logger name", func() {
enriched := ctx.WithContext(logging.NewName("test")).WithContext(logging.NewName("nested"))
enriched.Logger().Error("with name")
Expect(buf.String()).To(MatchRegexp(`.{25} error test.nested "with name"\n`))
buf.Reset()
enriched.Logger(logging.NewName("sub")).Error("with name")
Expect(buf.String()).To(MatchRegexp(`.{25} error test.nested.sub "with name"\n`))
buf.Reset()
enriched.Logger(logging.Realm("absolute")).
Error("with name")
Expect(buf.String()).To(MatchRegexp(`.{25} error test.nested \[absolute\] "with name"\n`))
})
It("adds realm as key/value pair", func() {
enriched := ctx.WithContext(logging.NewRealm("test")).WithContext(logging.NewRealm("other"), logging.NewName("test"))
enriched.Logger().Error("with realm")
Expect(buf.String()).To(MatchRegexp(`.{25} error test \[other\] "with realm"\n`))
buf.Reset()
enriched.Logger(logging.NewRealm("sub")).Error("with realm")
Expect(buf.String()).To(MatchRegexp(`.{25} error test \[sub\] "with realm"\n`))
buf.Reset()
enriched.Logger(logging.NewRealm("sub"), logging.NewName("sub")).Error("with realm")
Expect(buf.String()).To(MatchRegexp(`.{25} error test.sub \[sub\] "with realm"\n`))
})
})
Context("fields", func() {
BeforeEach(func() {
ctx = logrusl.Human().WithWriter(&buf).New()
ctx.SetDefaultLevel(logging.InfoLevel)
})
It("logs simple attribute values", func() {
enriched := ctx.WithContext(logging.NewAttribute("attr", "value"))
enriched.Logger().Info("with attribute")
Expect(buf.String()).To(MatchRegexp(`.{25} info "with attribute" attr=value\n`))
})
It("logs value attribute pointer stringer", func() {
enriched := ctx.WithContext(logging.NewAttribute("attr", &Pointer{"value"}))
enriched.Logger().Info("with attribute")
Expect(buf.String()).To(MatchRegexp(`.{25} info "with attribute" attr=value\n`))
})
It("logs value attribute values", func() {
enriched := ctx.WithContext(logging.NewAttribute("attr", Value{"value"}))
enriched.Logger().Info("with attribute")
Expect(buf.String()).To(MatchRegexp(`.{25} info "with attribute" attr=value\n`))
})
It("logs value simple underlying type", func() {
enriched := ctx.WithContext(logging.NewAttribute("attr", Underlying("value")))
enriched.Logger().Info("with attribute")
Expect(buf.String()).To(MatchRegexp(`.{25} info "with attribute" attr=value\n`))
})
It("logs value simple underlying type with substitution", func() {
enriched := ctx.WithContext(logging.NewAttribute("attr", Underlying("value")))
enriched.Logger().Info("with attribute value {{attr}}")
Expect(buf.String()).To(MatchRegexp(`.{25} info "with attribute value value"\n`))
})
It("logs value simple underlying type with substitution", func() {
enriched := ctx.WithContext(logging.NewAttribute("attr", fmt.Errorf("value")))
enriched.Logger().Info("with attribute value {{attr}}")
Expect(buf.String()).To(MatchRegexp(`.{25} info "with attribute value value"\n`))
})
})
Context("fields values", func() {
BeforeEach(func() {
ctx = logrusl.Human().WithWriter(&buf).New()
ctx.SetDefaultLevel(logging.InfoLevel)
})
It("key value", func() {
log := ctx.Logger()
log.Info("with key value", logging.KeyValue("name", "value"))
Expect(buf.String()).To(MatchRegexp(`.{25} info "with key value" name=value\n`))
})
It("key value sequence", func() {
log := ctx.Logger()
log.Info("with key value", logging.KeyValue("name", "value"), logging.KeyValue("other", "othervalue"))
Expect(buf.String()).To(MatchRegexp(`.{25} info "with key value" name=value other=othervalue\n`))
})
It("mixed key value sequence", func() {
log := ctx.Logger()
log.Info("with key value", logging.KeyValue("name", "value"), "other", "othervalue")
log.Info("with key value", "first", "firstvalue", logging.KeyValue("name", "value"), "other", "othervalue")
Expect(buf.String()).To(MatchRegexp(`.{25} info "with key value" name=value other=othervalue\n.{25} info "with key value" first=firstvalue name=value other=othervalue\n`))
})
It("attributes", func() {
log := ctx.Logger()
log.Info("with key value", logging.NewAttribute("name", "value"))
Expect(buf.String()).To(MatchRegexp(`.{25} info "with key value" name=value\n`))
})
})
Context("message context provider", func() {
BeforeEach(func() {
ctx = logrusl.Human().WithWriter(&buf).New()
ctx.SetDefaultLevel(logging.InfoLevel)
})
It("handles slices", func() {
mctx := []logging.MessageContext{
logging.NewAttribute("alice", 25),
logging.NewAttribute("bob", 26),
}
ctx.Logger(mctx, logging.NewAttribute("attr", "value")).Info("3 attrs")
Expect(buf.String()).To(MatchRegexp(`.{25} info "3 attrs" alice=25 attr=value bob=26\n`))
})
It("handles context", func() {
enriched := logrusl.New().WithContext(
logging.NewAttribute("alice", 25),
logging.NewAttribute("bob", 26),
)
ctx.WithContext(enriched, logging.NewAttribute("attr", "value")).Logger().Info("3 attrs")
Expect(buf.String()).To(MatchRegexp(`.{25} info "3 attrs" alice=25 attr=value bob=26\n`))
})
})
Context("special values", func() {
It("ignores NoValue", func() {
ctx.Logger().Info("test", "some", "value", "ignored", utils.Ignore, "next", "value")
Expect(buf.String()).To(Equal("V[3] test some value next value\n"))
})
It("logr preserves NoValue", func() {
ctx.Logger().V(0).Info("test", "some", "value", "ignored", utils.Ignore, "next", "value")
Expect(buf.String()).To(Equal("INFO test some value ignored <unset> next value\n"))
})
It("own formatter handles NoValue", func() {
l := adapter.NewLogger(&buf)
l.Formatter = adapter.NewTextFmtFormatter()
ctx.SetBaseLogger(logrusr.New(l))
ctx.Logger().V(3).Info("test", "some", "value", "ignored", utils.Ignore, "next", "value")
Expect(buf.String()).To(MatchRegexp(`.{25} info test next=value some=value\n`))
})
})
Context("tag based logging", func() {
var tag = logging.NewTag("info")
It("logs nothing if tag not present", func() {
ctx.AddRule(logging.NewConditionRule(logging.TraceLevel, tag))
ctx.Logger().Info("test", "some", "value")
ctx.Logger().Debug("should not be logged")
Expect(buf.String()).To(Equal("V[3] test some value\n"))
})
It("logs if tag is present", func() {
ctx.AddRule(logging.NewConditionRule(logging.TraceLevel, tag))
ctx.Logger(tag).Debug("test", "some", "value")
Expect(buf.String()).To(Equal("V[4] test some value\n"))
})
It("logs if tag is present for complex context", func() {
ctx.AddRule(logging.NewConditionRule(logging.TraceLevel, tag))
ctx.Logger([]logging.MessageContext{tag}).Debug("test", "some", "value")
Expect(buf.String()).To(Equal("V[4] test some value\n"))
})
It("logs for tag", func() {
ctx.AddRule(logging.NewConditionRule(logging.TraceLevel, tag))
log := ctx.LoggerFor(tag)
log.Debug("test", "some", "value")
Expect(buf.String()).To(Equal("V[4] test some value\n"))
})
It("does not log at all if tag is not given", func() {
ctx.Logger(tag).Info("test", "some", "value")
ctx.LoggerFor(tag).Info("should not be logged")
Expect(buf.String()).To(Equal("V[3] test some value\n"))
})
})
Context("log writer support", func() {
It("provides default writer for logrus", func() {
Expect(utils.UnwrapWriter(logrusl.New().Tree().LogWriter())).To(BeIdenticalTo(os.Stderr))
})
It("provides default writer for logrus in base context", func() {
Expect(utils.UnwrapWriter(logrusl.New().WithContext().Tree().LogWriter())).To(BeIdenticalTo(os.Stderr))
})
It("provides explicit writer for logrus in base context", func() {
ctx := logrusl.New().WithContext()
buf := bytes.NewBuffer(nil)
ctx.SetBaseLogger(logrusr.New(adapter.NewLogger(buf)))
Expect(ctx.Tree().LogWriter()).To(BeIdenticalTo(buf))
})
})
})
type Value struct {
msg string
}
type Pointer struct {
msg string
}
type Underlying string
func (v Value) String() string {
return v.msg
}
func (v *Pointer) String() string {
return v.msg
}
type X struct{}
func (x *X) Package() logging.Realm {
return func() logging.Realm {
return logging.Package()
}()
}