-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
log.go
1046 lines (860 loc) · 31.2 KB
/
log.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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
// Package log implements logging for the datadog agent. It wraps seelog, and
// supports logging to multiple destinations, buffering messages logged before
// setup, and scrubbing secrets from log messages.
//
// # Compatibility
//
// This module is exported and can be used outside of the datadog-agent
// repository, but is not designed as a general-purpose logging system. Its
// API may change incompatibly.
package log
import (
"bytes"
"errors"
"fmt"
"os"
"strings"
"sync"
"github.com/cihub/seelog"
"go.uber.org/atomic"
"github.com/DataDog/datadog-agent/pkg/util/scrubber"
)
type loggerPointer struct {
atomic.Pointer[DatadogLogger]
}
var (
// Logger is the main DatadogLogger
logger loggerPointer
jmxLogger loggerPointer
// This buffer holds log lines sent to the logger before its
// initialization. Even if initializing the logger is one of the first
// things the agent does, we still: load the conf, resolve secrets inside,
// compute the final proxy settings, ...
//
// This buffer should be very short lived.
logsBuffer = []func(){}
bufferMutex sync.Mutex
defaultStackDepth = 3
// for testing purposes
scrubBytesFunc = scrubber.ScrubBytes
)
// DatadogLogger wrapper structure for seelog
type DatadogLogger struct {
inner seelog.LoggerInterface
level seelog.LogLevel
extra map[string]seelog.LoggerInterface
l sync.RWMutex
}
/*
* Setup and initialization of the logger
*/
// SetupLogger setup agent wide logger
func SetupLogger(i seelog.LoggerInterface, level string) {
logger.Store(setupCommonLogger(i, level))
// Flush the log entries logged before initialization now that the logger is initialized
bufferMutex.Lock()
defer bufferMutex.Unlock()
for _, logLine := range logsBuffer {
logLine()
}
logsBuffer = []func(){}
}
func setupCommonLogger(i seelog.LoggerInterface, level string) *DatadogLogger {
l := &DatadogLogger{
inner: i,
extra: make(map[string]seelog.LoggerInterface),
}
lvl, ok := seelog.LogLevelFromString(level)
if !ok {
lvl = seelog.InfoLvl
}
l.level = lvl
// We're not going to call DatadogLogger directly, but using the
// exported functions, that will give us two frames in the stack
// trace that should be skipped to get to the original caller.
//
// The fact we need a constant "additional depth" means some
// theoretical refactor to avoid duplication in the functions
// below cannot be performed.
l.inner.SetAdditionalStackDepth(defaultStackDepth) //nolint:errcheck
return l
}
func addLogToBuffer(logHandle func()) {
bufferMutex.Lock()
defer bufferMutex.Unlock()
logsBuffer = append(logsBuffer, logHandle)
}
func (sw *DatadogLogger) scrub(s string) string {
if scrubbed, err := scrubBytesFunc([]byte(s)); err == nil {
return string(scrubbed)
}
return s
}
/*
* Operation on the **logger level**
*/
// ChangeLogLevel changes the current log level, valid levels are trace, debug,
// info, warn, error, critical and off, it requires a new seelog logger because
// an existing one cannot be updated
func ChangeLogLevel(li seelog.LoggerInterface, level string) error {
if err := logger.changeLogLevel(level); err != nil {
return err
}
// See detailed explanation in SetupLogger(...)
if err := li.SetAdditionalStackDepth(defaultStackDepth); err != nil {
return err
}
logger.replaceInnerLogger(li)
return nil
// need to return something, just set to Info (expected default)
}
func (sw *loggerPointer) changeLogLevel(level string) error {
l := sw.Load()
if l == nil {
return errors.New("cannot change loglevel: logger not initialized")
}
l.l.Lock()
defer l.l.Unlock()
if l.inner == nil {
return errors.New("cannot change loglevel: logger is initialized however logger.inner is nil")
}
lvl, ok := seelog.LogLevelFromString(strings.ToLower(level))
if !ok {
return errors.New("bad log level")
}
l.level = lvl
return nil
}
// GetLogLevel returns a seelog native representation of the current log level
func GetLogLevel() (seelog.LogLevel, error) {
return logger.getLogLevel()
}
func (sw *loggerPointer) getLogLevel() (seelog.LogLevel, error) {
l := sw.Load()
if l == nil {
return seelog.InfoLvl, errors.New("cannot get loglevel: logger not initialized")
}
l.l.RLock()
defer l.l.RUnlock()
if l.inner == nil {
return seelog.InfoLvl, errors.New("cannot get loglevel: logger not initialized")
}
return l.level, nil
}
// ShouldLog returns whether a given log level should be logged by the default logger
func ShouldLog(lvl seelog.LogLevel) bool {
// The lock stay in the exported function due to the use of `shouldLog` in function that already hold the lock
l := logger.Load()
if l != nil {
l.l.RLock()
defer l.l.RUnlock()
return l.shouldLog(lvl)
}
return false
}
// This function should be called with `sw.l` held
func (sw *DatadogLogger) shouldLog(level seelog.LogLevel) bool {
return level >= sw.level
}
// ValidateLogLevel validates the given log level and returns the corresponding Seelog log level.
// If the log level is "warning", it is converted to "warn" to handle a common gotcha when used with agent5.
// If the log level is not recognized, an error is returned.
func ValidateLogLevel(logLevel string) (string, error) {
seelogLogLevel := strings.ToLower(logLevel)
if seelogLogLevel == "warning" { // Common gotcha when used to agent5
seelogLogLevel = "warn"
}
if _, found := seelog.LogLevelFromString(seelogLogLevel); !found {
return "", fmt.Errorf("unknown log level: %s", seelogLogLevel)
}
return seelogLogLevel, nil
}
/*
* Operation on the **logger**
*/
// RegisterAdditionalLogger registers an additional logger for logging
func RegisterAdditionalLogger(n string, li seelog.LoggerInterface) error {
return logger.registerAdditionalLogger(n, li)
}
func (sw *loggerPointer) registerAdditionalLogger(n string, li seelog.LoggerInterface) error {
l := sw.Load()
if l == nil {
return errors.New("cannot register: logger not initialized")
}
l.l.Lock()
defer l.l.Unlock()
if l.inner == nil {
return errors.New("cannot register: logger not initialized")
}
if l.extra == nil {
return errors.New("logger not fully initialized, additional logging unavailable")
}
if _, ok := l.extra[n]; ok {
return errors.New("logger already registered with that name")
}
l.extra[n] = li
return nil
}
// ReplaceLogger allows replacing the internal logger, returns old logger
func ReplaceLogger(li seelog.LoggerInterface) seelog.LoggerInterface {
return logger.replaceInnerLogger(li)
}
func (sw *loggerPointer) replaceInnerLogger(li seelog.LoggerInterface) seelog.LoggerInterface {
l := sw.Load()
if l == nil {
return nil // Return nil if logger is not initialized
}
l.l.Lock()
defer l.l.Unlock()
if l.inner == nil {
return nil // Return nil if logger.inner is not initialized
}
old := l.inner
l.inner = li
return old
}
// Flush flushes the underlying inner log
func Flush() {
logger.flush()
jmxLogger.flush()
}
func (sw *loggerPointer) flush() {
l := sw.Load()
if l == nil {
return
}
l.l.Lock()
defer l.l.Unlock()
if l.inner != nil {
l.inner.Flush()
}
}
/*
* log functions
*/
// log logs a message at the given level, using either bufferFunc (if logging is not yet set up) or
// scrubAndLogFunc, and treating the variadic args as the message.
func log(logLevel seelog.LogLevel, bufferFunc func(), scrubAndLogFunc func(string), v ...interface{}) {
l := logger.Load()
if l == nil {
addLogToBuffer(bufferFunc)
return
}
l.l.Lock()
defer l.l.Unlock()
if l.inner == nil {
addLogToBuffer(bufferFunc)
} else if l.shouldLog(logLevel) {
s := BuildLogEntry(v...)
scrubAndLogFunc(s)
}
}
func logWithError(logLevel seelog.LogLevel, bufferFunc func(), scrubAndLogFunc func(string) error, fallbackStderr bool, v ...interface{}) error {
l := logger.Load()
if l == nil {
addLogToBuffer(bufferFunc)
err := formatError(v...)
if fallbackStderr {
fmt.Fprintf(os.Stderr, "%s: %s\n", logLevel.String(), err.Error())
}
return err
}
l.l.Lock()
isInnerNil := l.inner == nil
if isInnerNil {
if !fallbackStderr {
addLogToBuffer(bufferFunc)
}
} else if l.shouldLog(logLevel) {
defer l.l.Unlock()
s := BuildLogEntry(v...)
return scrubAndLogFunc(s)
}
l.l.Unlock()
err := formatError(v...)
// Originally (PR 6436) fallbackStderr check had been added to handle a small window
// where error messages had been lost before Logger had been initialized. Adjusting
// just for that case because if the error log should not be logged - because it has
// been suppressed then it should be taken into account.
if fallbackStderr && isInnerNil {
fmt.Fprintf(os.Stderr, "%s: %s\n", logLevel.String(), err.Error())
}
return err
}
/*
* logFormat functions
*/
func logFormat(logLevel seelog.LogLevel, bufferFunc func(), scrubAndLogFunc func(string, ...interface{}), format string, params ...interface{}) {
l := logger.Load()
if l == nil {
addLogToBuffer(bufferFunc)
return
}
l.l.Lock()
defer l.l.Unlock()
if l.inner == nil {
addLogToBuffer(bufferFunc)
} else if l.shouldLog(logLevel) {
scrubAndLogFunc(format, params...)
}
}
func logFormatWithError(logLevel seelog.LogLevel, bufferFunc func(), scrubAndLogFunc func(string, ...interface{}) error, format string, fallbackStderr bool, params ...interface{}) error {
l := logger.Load()
if l == nil {
addLogToBuffer(bufferFunc)
err := formatErrorf(format, params...)
if fallbackStderr {
fmt.Fprintf(os.Stderr, "%s: %s\n", logLevel.String(), err.Error())
}
return err
}
l.l.Lock()
isInnerNil := l.inner == nil
if isInnerNil {
if !fallbackStderr {
addLogToBuffer(bufferFunc)
}
} else if l.shouldLog(logLevel) {
defer l.l.Unlock()
return scrubAndLogFunc(format, params...)
}
l.l.Unlock()
err := formatErrorf(format, params...)
// Originally (PR 6436) fallbackStderr check had been added to handle a small window
// where error messages had been lost before Logger had been initialized. Adjusting
// just for that case because if the error log should not be logged - because it has
// been suppressed then it should be taken into account.
if fallbackStderr && isInnerNil {
fmt.Fprintf(os.Stderr, "%s: %s\n", logLevel.String(), err.Error())
}
return err
}
/*
* logContext functions
*/
func logContext(logLevel seelog.LogLevel, bufferFunc func(), scrubAndLogFunc func(string), message string, depth int, context ...interface{}) {
l := logger.Load()
if l == nil {
addLogToBuffer(bufferFunc)
return
}
l.l.Lock()
defer l.l.Unlock()
if l.inner == nil {
addLogToBuffer(bufferFunc)
} else if l.shouldLog(logLevel) {
l.inner.SetContext(context)
l.inner.SetAdditionalStackDepth(defaultStackDepth + depth) //nolint:errcheck
scrubAndLogFunc(message)
l.inner.SetContext(nil)
l.inner.SetAdditionalStackDepth(defaultStackDepth) //nolint:errcheck
}
}
func logContextWithError(logLevel seelog.LogLevel, bufferFunc func(), scrubAndLogFunc func(string) error, message string, fallbackStderr bool, depth int, context ...interface{}) error {
l := logger.Load()
if l == nil {
addLogToBuffer(bufferFunc)
err := formatErrorc(message, context...)
if fallbackStderr {
fmt.Fprintf(os.Stderr, "%s: %s\n", logLevel.String(), err.Error())
}
return err
}
l.l.Lock()
isInnerNil := l.inner == nil
if isInnerNil {
if !fallbackStderr {
addLogToBuffer(bufferFunc)
}
} else if l.shouldLog(logLevel) {
l.inner.SetContext(context)
l.inner.SetAdditionalStackDepth(defaultStackDepth + depth) //nolint:errcheck
err := scrubAndLogFunc(message)
l.inner.SetContext(nil)
l.inner.SetAdditionalStackDepth(defaultStackDepth) //nolint:errcheck
defer l.l.Unlock()
return err
}
l.l.Unlock()
err := formatErrorc(message, context...)
if fallbackStderr && isInnerNil {
fmt.Fprintf(os.Stderr, "%s: %s\n", logLevel.String(), err.Error())
}
return err
}
// trace logs at the trace level, called with sw.l held
func (sw *loggerPointer) trace(s string) {
l := sw.Load()
if l == nil {
return
}
scrubbed := l.scrub(s)
l.inner.Trace(scrubbed)
for _, l := range l.extra {
l.Trace(scrubbed)
}
}
// trace logs at the trace level and the current stack depth plus the
// additional given one, called with sw.l held
func (sw *loggerPointer) traceStackDepth(s string, depth int) {
l := sw.Load()
scrubbed := l.scrub(s)
l.inner.SetAdditionalStackDepth(defaultStackDepth + depth) //nolint:errcheck
l.inner.Trace(scrubbed)
l.inner.SetAdditionalStackDepth(defaultStackDepth) //nolint:errcheck
for _, l := range l.extra {
l.Trace(scrubbed)
}
}
// debug logs at the debug level, called with sw.l held
func (sw *loggerPointer) debug(s string) {
l := sw.Load()
scrubbed := l.scrub(s)
l.inner.Debug(scrubbed)
for _, l := range l.extra {
l.Debug(scrubbed)
}
}
// debug logs at the debug level and the current stack depth plus the additional given one, called with sw.l held
func (sw *loggerPointer) debugStackDepth(s string, depth int) {
l := sw.Load()
scrubbed := l.scrub(s)
l.inner.SetAdditionalStackDepth(defaultStackDepth + depth) //nolint:errcheck
l.inner.Debug(scrubbed)
l.inner.SetAdditionalStackDepth(defaultStackDepth) //nolint:errcheck
for _, l := range l.extra {
l.Debug(scrubbed) //nolint:errcheck
}
}
// info logs at the info level, called with sw.l held
func (sw *loggerPointer) info(s string) {
l := sw.Load()
scrubbed := l.scrub(s)
l.inner.Info(scrubbed)
for _, l := range l.extra {
l.Info(scrubbed)
}
}
// info logs at the info level and the current stack depth plus the additional given one, called with sw.l held
func (sw *loggerPointer) infoStackDepth(s string, depth int) {
l := sw.Load()
scrubbed := l.scrub(s)
l.inner.SetAdditionalStackDepth(defaultStackDepth + depth) //nolint:errcheck
l.inner.Info(scrubbed)
l.inner.SetAdditionalStackDepth(defaultStackDepth) //nolint:errcheck
for _, l := range l.extra {
l.Info(scrubbed) //nolint:errcheck
}
}
// warn logs at the warn level, called with sw.l held
func (sw *loggerPointer) warn(s string) error {
l := sw.Load()
scrubbed := l.scrub(s)
err := l.inner.Warn(scrubbed)
for _, l := range l.extra {
l.Warn(scrubbed) //nolint:errcheck
}
return err
}
// error logs at the error level and the current stack depth plus the additional given one, called with sw.l held
func (sw *loggerPointer) warnStackDepth(s string, depth int) error {
l := sw.Load()
scrubbed := l.scrub(s)
l.inner.SetAdditionalStackDepth(defaultStackDepth + depth) //nolint:errcheck
err := l.inner.Warn(scrubbed)
l.inner.SetAdditionalStackDepth(defaultStackDepth) //nolint:errcheck
for _, l := range l.extra {
l.Warn(scrubbed) //nolint:errcheck
}
return err
}
// error logs at the error level, called with sw.l held
func (sw *loggerPointer) error(s string) error {
l := sw.Load()
scrubbed := l.scrub(s)
err := l.inner.Error(scrubbed)
for _, l := range l.extra {
l.Error(scrubbed) //nolint:errcheck
}
return err
}
// error logs at the error level and the current stack depth plus the additional given one, called with sw.l held
func (sw *loggerPointer) errorStackDepth(s string, depth int) error {
l := sw.Load()
scrubbed := l.scrub(s)
l.inner.SetAdditionalStackDepth(defaultStackDepth + depth) //nolint:errcheck
err := l.inner.Error(scrubbed)
l.inner.SetAdditionalStackDepth(defaultStackDepth) //nolint:errcheck
for _, l := range l.extra {
l.Error(scrubbed) //nolint:errcheck
}
return err
}
// critical logs at the critical level, called with sw.l held
func (sw *loggerPointer) critical(s string) error {
l := sw.Load()
scrubbed := l.scrub(s)
err := l.inner.Critical(scrubbed)
for _, l := range l.extra {
l.Critical(scrubbed) //nolint:errcheck
}
return err
}
// critical logs at the critical level and the current stack depth plus the additional given one, called with sw.l held
func (sw *loggerPointer) criticalStackDepth(s string, depth int) error {
l := sw.Load()
scrubbed := l.scrub(s)
l.inner.SetAdditionalStackDepth(defaultStackDepth + depth) //nolint:errcheck
err := l.inner.Critical(scrubbed)
l.inner.SetAdditionalStackDepth(defaultStackDepth) //nolint:errcheck
for _, l := range l.extra {
l.Critical(scrubbed) //nolint:errcheck
}
return err
}
// tracef logs with format at the trace level, called with sw.l held
func (sw *loggerPointer) tracef(format string, params ...interface{}) {
l := sw.Load()
scrubbed := l.scrub(fmt.Sprintf(format, params...))
l.inner.Trace(scrubbed)
for _, l := range l.extra {
l.Trace(scrubbed)
}
}
// debugf logs with format at the debug level, called with sw.l held
func (sw *loggerPointer) debugf(format string, params ...interface{}) {
l := sw.Load()
scrubbed := l.scrub(fmt.Sprintf(format, params...))
l.inner.Debug(scrubbed)
for _, l := range l.extra {
l.Debug(scrubbed)
}
}
// infof logs with format at the info level, called with sw.l held
func (sw *loggerPointer) infof(format string, params ...interface{}) {
l := sw.Load()
scrubbed := l.scrub(fmt.Sprintf(format, params...))
l.inner.Info(scrubbed)
for _, l := range l.extra {
l.Info(scrubbed)
}
}
// warnf logs with format at the warn level, called with sw.l held
func (sw *loggerPointer) warnf(format string, params ...interface{}) error {
l := sw.Load()
scrubbed := l.scrub(fmt.Sprintf(format, params...))
err := l.inner.Warn(scrubbed)
for _, l := range l.extra {
l.Warn(scrubbed) //nolint:errcheck
}
return err
}
// errorf logs with format at the error level, called with sw.l held
func (sw *loggerPointer) errorf(format string, params ...interface{}) error {
l := sw.Load()
scrubbed := l.scrub(fmt.Sprintf(format, params...))
err := l.inner.Error(scrubbed)
for _, l := range l.extra {
l.Error(scrubbed) //nolint:errcheck
}
return err
}
// criticalf logs with format at the critical level, called with sw.l held
func (sw *loggerPointer) criticalf(format string, params ...interface{}) error {
l := sw.Load()
scrubbed := l.scrub(fmt.Sprintf(format, params...))
err := l.inner.Critical(scrubbed)
for _, l := range l.extra {
l.Critical(scrubbed) //nolint:errcheck
}
return err
}
// BuildLogEntry concatenates all inputs with spaces
func BuildLogEntry(v ...interface{}) string {
var fmtBuffer bytes.Buffer
for i := 0; i < len(v)-1; i++ {
fmtBuffer.WriteString("%v ")
}
fmtBuffer.WriteString("%v")
return fmt.Sprintf(fmtBuffer.String(), v...)
}
func scrubMessage(message string) string {
msgScrubbed, err := scrubBytesFunc([]byte(message))
if err == nil {
return string(msgScrubbed)
}
return "[REDACTED] - failure to clean the message"
}
func formatErrorf(format string, params ...interface{}) error {
msg := scrubMessage(fmt.Sprintf(format, params...))
return errors.New(msg)
}
func formatError(v ...interface{}) error {
msg := scrubMessage(fmt.Sprint(v...))
return errors.New(msg)
}
func formatErrorc(message string, context ...interface{}) error {
// Build a format string like this:
// message (%s:%v, %s:%v, ... %s:%v)
var fmtBuffer bytes.Buffer
fmtBuffer.WriteString(message)
if len(context) > 0 && len(context)%2 == 0 {
fmtBuffer.WriteString(" (")
for i := 0; i < len(context); i += 2 {
fmtBuffer.WriteString("%s:%v")
if i != len(context)-2 {
fmtBuffer.WriteString(", ")
}
}
fmtBuffer.WriteString(")")
}
msg := fmt.Sprintf(fmtBuffer.String(), context...)
return errors.New(scrubMessage(msg))
}
// Trace logs at the trace level
func Trace(v ...interface{}) {
log(seelog.TraceLvl, func() { Trace(v...) }, logger.trace, v...)
}
// Tracef logs with format at the trace level
func Tracef(format string, params ...interface{}) {
logFormat(seelog.TraceLvl, func() { Tracef(format, params...) }, logger.tracef, format, params...)
}
// TracefStackDepth logs with format at the trace level and the current stack depth plus the given depth
func TracefStackDepth(depth int, format string, params ...interface{}) {
currentLevel, _ := GetLogLevel()
if currentLevel > seelog.TraceLvl {
return
}
msg := fmt.Sprintf(format, params...)
log(seelog.TraceLvl, func() { TraceStackDepth(depth, msg) }, func(s string) {
logger.traceStackDepth(s, depth)
}, msg)
}
// TracecStackDepth logs at the trace level with context and the current stack depth plus the additional given one
func TracecStackDepth(message string, depth int, context ...interface{}) {
logContext(seelog.TraceLvl, func() { Tracec(message, context...) }, logger.trace, message, depth, context...)
}
// Tracec logs at the trace level with context
func Tracec(message string, context ...interface{}) {
TracecStackDepth(message, 1, context...)
}
// TraceFunc calls and logs the result of 'logFunc' if and only if Trace (or more verbose) logs are enabled
func TraceFunc(logFunc func() string) {
currentLevel, _ := GetLogLevel()
if currentLevel <= seelog.TraceLvl {
TraceStackDepth(2, logFunc())
}
}
// Debug logs at the debug level
func Debug(v ...interface{}) {
log(seelog.DebugLvl, func() { Debug(v...) }, logger.debug, v...)
}
// Debugf logs with format at the debug level
func Debugf(format string, params ...interface{}) {
logFormat(seelog.DebugLvl, func() { Debugf(format, params...) }, logger.debugf, format, params...)
}
// DebugfStackDepth logs with format at the debug level and the current stack depth plus the given depth
func DebugfStackDepth(depth int, format string, params ...interface{}) {
currentLevel, _ := GetLogLevel()
if currentLevel > seelog.DebugLvl {
return
}
msg := fmt.Sprintf(format, params...)
log(seelog.DebugLvl, func() { DebugStackDepth(depth, msg) }, func(s string) {
logger.debugStackDepth(s, depth)
}, msg)
}
// DebugcStackDepth logs at the debug level with context and the current stack depth plus the additional given one
func DebugcStackDepth(message string, depth int, context ...interface{}) {
logContext(seelog.DebugLvl, func() { Debugc(message, context...) }, logger.debug, message, depth, context...)
}
// Debugc logs at the debug level with context
func Debugc(message string, context ...interface{}) {
DebugcStackDepth(message, 1, context...)
}
// DebugFunc calls and logs the result of 'logFunc' if and only if Debug (or more verbose) logs are enabled
func DebugFunc(logFunc func() string) {
currentLevel, _ := GetLogLevel()
if currentLevel <= seelog.DebugLvl {
DebugStackDepth(2, logFunc())
}
}
// Info logs at the info level
func Info(v ...interface{}) {
log(seelog.InfoLvl, func() { Info(v...) }, logger.info, v...)
}
// Infof logs with format at the info level
func Infof(format string, params ...interface{}) {
logFormat(seelog.InfoLvl, func() { Infof(format, params...) }, logger.infof, format, params...)
}
// InfofStackDepth logs with format at the info level and the current stack depth plus the given depth
func InfofStackDepth(depth int, format string, params ...interface{}) {
currentLevel, _ := GetLogLevel()
if currentLevel > seelog.InfoLvl {
return
}
msg := fmt.Sprintf(format, params...)
log(seelog.InfoLvl, func() { InfoStackDepth(depth, msg) }, func(s string) {
logger.infoStackDepth(s, depth)
}, msg)
}
// InfocStackDepth logs at the info level with context and the current stack depth plus the additional given one
func InfocStackDepth(message string, depth int, context ...interface{}) {
logContext(seelog.InfoLvl, func() { Infoc(message, context...) }, logger.info, message, depth, context...)
}
// Infoc logs at the info level with context
func Infoc(message string, context ...interface{}) {
InfocStackDepth(message, 1, context...)
}
// InfoFunc calls and logs the result of 'logFunc' if and only if Info (or more verbose) logs are enabled
func InfoFunc(logFunc func() string) {
currentLevel, _ := GetLogLevel()
if currentLevel <= seelog.InfoLvl {
InfoStackDepth(2, logFunc())
}
}
// Warn logs at the warn level and returns an error containing the formated log message
func Warn(v ...interface{}) error {
return logWithError(seelog.WarnLvl, func() { _ = Warn(v...) }, logger.warn, false, v...)
}
// Warnf logs with format at the warn level and returns an error containing the formated log message
func Warnf(format string, params ...interface{}) error {
return logFormatWithError(seelog.WarnLvl, func() { _ = Warnf(format, params...) }, logger.warnf, format, false, params...)
}
// WarnfStackDepth logs with format at the warn level and the current stack depth plus the given depth
func WarnfStackDepth(depth int, format string, params ...interface{}) error {
msg := fmt.Sprintf(format, params...)
return logWithError(seelog.WarnLvl, func() { _ = WarnStackDepth(depth, msg) }, func(s string) error {
return logger.warnStackDepth(s, depth)
}, false, msg)
}
// WarncStackDepth logs at the warn level with context and the current stack depth plus the additional given one and returns an error containing the formated log message
func WarncStackDepth(message string, depth int, context ...interface{}) error {
return logContextWithError(seelog.WarnLvl, func() { _ = Warnc(message, context...) }, logger.warn, message, false, depth, context...)
}
// Warnc logs at the warn level with context and returns an error containing the formated log message
func Warnc(message string, context ...interface{}) error {
return WarncStackDepth(message, 1, context...)
}
// WarnFunc calls and logs the result of 'logFunc' if and only if Warn (or more verbose) logs are enabled
func WarnFunc(logFunc func() string) {
currentLevel, _ := GetLogLevel()
if currentLevel <= seelog.WarnLvl {
_ = WarnStackDepth(2, logFunc())
}
}
// Error logs at the error level and returns an error containing the formated log message
func Error(v ...interface{}) error {
return logWithError(seelog.ErrorLvl, func() { _ = Error(v...) }, logger.error, true, v...)
}
// Errorf logs with format at the error level and returns an error containing the formated log message
func Errorf(format string, params ...interface{}) error {
return logFormatWithError(seelog.ErrorLvl, func() { _ = Errorf(format, params...) }, logger.errorf, format, true, params...)
}
// ErrorfStackDepth logs with format at the error level and the current stack depth plus the given depth
func ErrorfStackDepth(depth int, format string, params ...interface{}) error {
msg := fmt.Sprintf(format, params...)
return logWithError(seelog.ErrorLvl, func() { _ = ErrorStackDepth(depth, msg) }, func(s string) error {
return logger.errorStackDepth(s, depth)
}, true, msg)
}
// ErrorcStackDepth logs at the error level with context and the current stack depth plus the additional given one and returns an error containing the formated log message
func ErrorcStackDepth(message string, depth int, context ...interface{}) error {
return logContextWithError(seelog.ErrorLvl, func() { _ = Errorc(message, context...) }, logger.error, message, true, depth, context...)
}
// Errorc logs at the error level with context and returns an error containing the formated log message
func Errorc(message string, context ...interface{}) error {
return ErrorcStackDepth(message, 1, context...)
}
// ErrorFunc calls and logs the result of 'logFunc' if and only if Error (or more verbose) logs are enabled
func ErrorFunc(logFunc func() string) {
currentLevel, _ := GetLogLevel()
if currentLevel <= seelog.ErrorLvl {
_ = ErrorStackDepth(2, logFunc())
}
}
// Critical logs at the critical level and returns an error containing the formated log message
func Critical(v ...interface{}) error {
return logWithError(seelog.CriticalLvl, func() { _ = Critical(v...) }, logger.critical, true, v...)
}
// Criticalf logs with format at the critical level and returns an error containing the formated log message
func Criticalf(format string, params ...interface{}) error {
return logFormatWithError(seelog.CriticalLvl, func() { _ = Criticalf(format, params...) }, logger.criticalf, format, true, params...)
}
// CriticalfStackDepth logs with format at the critical level and the current stack depth plus the given depth
func CriticalfStackDepth(depth int, format string, params ...interface{}) error {
msg := fmt.Sprintf(format, params...)
return logWithError(seelog.CriticalLvl, func() { _ = CriticalStackDepth(depth, msg) }, func(s string) error {
return logger.criticalStackDepth(s, depth)
}, false, msg)
}
// CriticalcStackDepth logs at the critical level with context and the current stack depth plus the additional given one and returns an error containing the formated log message
func CriticalcStackDepth(message string, depth int, context ...interface{}) error {
return logContextWithError(seelog.CriticalLvl, func() { _ = Criticalc(message, context...) }, logger.critical, message, true, depth, context...)
}
// Criticalc logs at the critical level with context and returns an error containing the formated log message
func Criticalc(message string, context ...interface{}) error {
return CriticalcStackDepth(message, 1, context...)
}
// CriticalFunc calls and logs the result of 'logFunc' if and only if Critical (or more verbose) logs are enabled
func CriticalFunc(logFunc func() string) {
currentLevel, _ := GetLogLevel()
if currentLevel <= seelog.CriticalLvl {
_ = CriticalStackDepth(2, logFunc())
}
}
// InfoStackDepth logs at the info level and the current stack depth plus the additional given one
func InfoStackDepth(depth int, v ...interface{}) {
log(seelog.InfoLvl, func() { InfoStackDepth(depth, v...) }, func(s string) {
logger.infoStackDepth(s, depth)
}, v...)
}
// WarnStackDepth logs at the warn level and the current stack depth plus the additional given one and returns an error containing the formated log message
func WarnStackDepth(depth int, v ...interface{}) error {
return logWithError(seelog.WarnLvl, func() { _ = WarnStackDepth(depth, v...) }, func(s string) error {
return logger.warnStackDepth(s, depth)
}, false, v...)
}