@@ -66,6 +66,9 @@ type intLogger struct {
66
66
implied []interface {}
67
67
68
68
exclude func (level Level , msg string , args ... interface {}) bool
69
+
70
+ // create subloggers with their own level setting
71
+ independentLevels bool
69
72
}
70
73
71
74
// New returns a configured logger.
@@ -100,14 +103,15 @@ func newLogger(opts *LoggerOptions) *intLogger {
100
103
}
101
104
102
105
l := & intLogger {
103
- json : opts .JSONFormat ,
104
- caller : opts .IncludeLocation ,
105
- name : opts .Name ,
106
- timeFormat : TimeFormat ,
107
- mutex : mutex ,
108
- writer : newWriter (output , opts .Color ),
109
- level : new (int32 ),
110
- exclude : opts .Exclude ,
106
+ json : opts .JSONFormat ,
107
+ caller : opts .IncludeLocation ,
108
+ name : opts .Name ,
109
+ timeFormat : TimeFormat ,
110
+ mutex : mutex ,
111
+ writer : newWriter (output , opts .Color ),
112
+ level : new (int32 ),
113
+ exclude : opts .Exclude ,
114
+ independentLevels : opts .IndependentLevels ,
111
115
}
112
116
113
117
l .setColorization (opts )
@@ -514,7 +518,7 @@ func (l *intLogger) With(args ...interface{}) Logger {
514
518
args = args [:len (args )- 1 ]
515
519
}
516
520
517
- sl := * l
521
+ sl := l . copy ()
518
522
519
523
result := make (map [string ]interface {}, len (l .implied )+ len (args ))
520
524
keys := make ([]string , 0 , len (l .implied )+ len (args ))
@@ -548,32 +552,32 @@ func (l *intLogger) With(args ...interface{}) Logger {
548
552
sl .implied = append (sl .implied , MissingKey , extra )
549
553
}
550
554
551
- return & sl
555
+ return sl
552
556
}
553
557
554
558
// Create a new sub-Logger that a name decending from the current name.
555
559
// This is used to create a subsystem specific Logger.
556
560
func (l * intLogger ) Named (name string ) Logger {
557
- sl := * l
561
+ sl := l . copy ()
558
562
559
563
if sl .name != "" {
560
564
sl .name = sl .name + "." + name
561
565
} else {
562
566
sl .name = name
563
567
}
564
568
565
- return & sl
569
+ return sl
566
570
}
567
571
568
572
// Create a new sub-Logger with an explicit name. This ignores the current
569
573
// name. This is used to create a standalone logger that doesn't fall
570
574
// within the normal hierarchy.
571
575
func (l * intLogger ) ResetNamed (name string ) Logger {
572
- sl := * l
576
+ sl := l . copy ()
573
577
574
578
sl .name = name
575
579
576
- return & sl
580
+ return sl
577
581
}
578
582
579
583
func (l * intLogger ) ResetOutput (opts * LoggerOptions ) error {
@@ -660,3 +664,16 @@ func (i *intLogger) ImpliedArgs() []interface{} {
660
664
func (i * intLogger ) Name () string {
661
665
return i .name
662
666
}
667
+
668
+ // copy returns a shallow copy of the intLogger, replacing the level pointer
669
+ // when necessary
670
+ func (l * intLogger ) copy () * intLogger {
671
+ sl := * l
672
+
673
+ if l .independentLevels {
674
+ sl .level = new (int32 )
675
+ * sl .level = * l .level
676
+ }
677
+
678
+ return & sl
679
+ }
0 commit comments