-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Better name for some functions/variables - Allowing the typedLogger to have any output, its the caller's responsibility to restric output usage - Moved `typedLogger` to its own file - Updated tests accoringly
- Loading branch information
Showing
4 changed files
with
84 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package logp | ||
|
||
import ( | ||
"fmt" | ||
|
||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
// typedLoggerCore takes two cores and directs logs entries to one of them | ||
// with the value of the field defined by the pair `key` and `value` | ||
// | ||
// If `entry[key] == value` the typedCore is used, otherwise the | ||
// defaultCore is used. | ||
// WARNING: The level of both cores must always be the same! | ||
// typedLoggerCore will only use the defaultCore level to decide | ||
// whether to log an entry or not | ||
type typedLoggerCore struct { | ||
typedCore zapcore.Core | ||
defaultCore zapcore.Core | ||
value string | ||
key string | ||
} | ||
|
||
func (t *typedLoggerCore) Enabled(l zapcore.Level) bool { | ||
return t.defaultCore.Enabled(l) | ||
} | ||
|
||
func (t *typedLoggerCore) With(fields []zapcore.Field) zapcore.Core { | ||
t.defaultCore = t.defaultCore.With(fields) | ||
t.typedCore = t.typedCore.With(fields) | ||
return t | ||
} | ||
|
||
func (t *typedLoggerCore) Check(e zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry { | ||
if t.defaultCore.Enabled(e.Level) { | ||
return ce.AddCore(e, t) | ||
} | ||
|
||
return ce | ||
} | ||
|
||
func (t *typedLoggerCore) Sync() error { | ||
defaultErr := t.defaultCore.Sync() | ||
typedErr := t.typedCore.Sync() | ||
|
||
if defaultErr != nil || typedErr != nil { | ||
return fmt.Errorf("error syncing logger. DefaultCore: '%w', typedCore: '%w'", defaultErr, typedErr) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (t *typedLoggerCore) Write(e zapcore.Entry, fields []zapcore.Field) error { | ||
for _, f := range fields { | ||
if f.Key == t.key { | ||
if f.String == t.value { | ||
return t.typedCore.Write(e, fields) | ||
} | ||
} | ||
} | ||
|
||
return t.defaultCore.Write(e, fields) | ||
} |