Skip to content

Commit

Permalink
added support for providing custom logger name encoders
Browse files Browse the repository at this point in the history
  • Loading branch information
pavius committed Jul 3, 2017
1 parent acea907 commit 751edf2
Show file tree
Hide file tree
Showing 4 changed files with 315 additions and 204 deletions.
4 changes: 2 additions & 2 deletions zapcore/console_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ func (c consoleEncoder) EncodeEntry(ent Entry, fields []Field) (*buffer.Buffer,
if c.LevelKey != "" && c.EncodeLevel != nil {
c.EncodeLevel(ent.Level, arr)
}
if ent.LoggerName != "" && c.NameKey != "" {
arr.AppendString(ent.LoggerName)
if ent.LoggerName != "" && c.NameKey != "" && c.EncodeLoggerName != nil {
c.EncodeLoggerName(ent.LoggerName, arr)
}
if ent.Caller.Defined && c.CallerKey != "" && c.EncodeCaller != nil {
c.EncodeCaller(ent.Caller, arr)
Expand Down
35 changes: 31 additions & 4 deletions zapcore/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package zapcore

import (
"strings"
"time"

"go.uber.org/zap/buffer"
Expand Down Expand Up @@ -196,6 +197,31 @@ func (e *CallerEncoder) UnmarshalText(text []byte) error {
return nil
}

// A LoggerNameEncoder serializes a LoggerName to a primitive type.
type LoggerNameEncoder func(string, PrimitiveArrayEncoder)

// FullLoggerNameEncoder serializes a logger name as is
func FullLoggerNameEncoder(loggerName string, enc PrimitiveArrayEncoder) {
enc.AppendString(loggerName)
}

// CapitalLoggerNameEncoder serializes a logger name in all caps
func CapitalLoggerNameEncoder(loggerName string, enc PrimitiveArrayEncoder) {
enc.AppendString(strings.ToUpper(loggerName))
}

// UnmarshalText unmarshals text to a LoggerNameEncoder. "capital" is unmarshaled to
// CapitalLoggerNameEncoder and anything else is unmarshaled to FullLoggerNameEncoder.
func (e *LoggerNameEncoder) UnmarshalText(text []byte) error {
switch string(text) {
case "capital":
*e = CapitalLoggerNameEncoder
default:
*e = FullLoggerNameEncoder
}
return nil
}

// An EncoderConfig allows users to configure the concrete encoders supplied by
// zapcore.
type EncoderConfig struct {
Expand All @@ -211,10 +237,11 @@ type EncoderConfig struct {
// Configure the primitive representations of common complex types. For
// example, some users may want all time.Times serialized as floating-point
// seconds since epoch, while others may prefer ISO8601 strings.
EncodeLevel LevelEncoder `json:"levelEncoder" yaml:"levelEncoder"`
EncodeTime TimeEncoder `json:"timeEncoder" yaml:"timeEncoder"`
EncodeDuration DurationEncoder `json:"durationEncoder" yaml:"durationEncoder"`
EncodeCaller CallerEncoder `json:"callerEncoder" yaml:"callerEncoder"`
EncodeLevel LevelEncoder `json:"levelEncoder" yaml:"levelEncoder"`
EncodeTime TimeEncoder `json:"timeEncoder" yaml:"timeEncoder"`
EncodeDuration DurationEncoder `json:"durationEncoder" yaml:"durationEncoder"`
EncodeCaller CallerEncoder `json:"callerEncoder" yaml:"callerEncoder"`
EncodeLoggerName LoggerNameEncoder `json:"nameEncoder" yaml:"nameEncoder"`
}

// ObjectEncoder is a strongly-typed, encoding-agnostic interface for adding a
Expand Down
Loading

0 comments on commit 751edf2

Please sign in to comment.