Skip to content
Snippets Groups Projects
Commit 29d881cf authored by Semyon Krestyaninov's avatar Semyon Krestyaninov
Browse files

refactor

parent 2e4998de
No related branches found
No related tags found
No related merge requests found
...@@ -57,11 +57,6 @@ func (core *Core) getEntry(entry zapcore.Entry, fields []zapcore.Field) *log.Ent ...@@ -57,11 +57,6 @@ func (core *Core) getEntry(entry zapcore.Entry, fields []zapcore.Field) *log.Ent
fields = append(fields, core.fields...) fields = append(fields, core.fields...)
} }
enc := zapcore.NewMapObjectEncoder()
for _, field := range fields {
field.AddTo(enc)
}
ent := &log.Entry{ ent := &log.Entry{
ID: id.GenerateNewID(), ID: id.GenerateNewID(),
Timestamp: entry.Time, Timestamp: entry.Time,
...@@ -69,18 +64,22 @@ func (core *Core) getEntry(entry zapcore.Entry, fields []zapcore.Field) *log.Ent ...@@ -69,18 +64,22 @@ func (core *Core) getEntry(entry zapcore.Entry, fields []zapcore.Field) *log.Ent
Message: entry.Message, Message: entry.Message,
} }
ent.Category, _ = enc.Fields["category"].(string) for _, field := range fields {
ent.Component, _ = enc.Fields["component"].(string) switch field.Key {
ent.Event, _ = enc.Fields["event"].(string) case "category":
ent.ObjectID, _ = enc.Fields["object"].(*oid.ObjectId) ent.Category = field.String
ent.CallerID, _ = enc.Fields["caller"].(*oid.ObjectId) case "component":
ent.Attr = enc.Fields["attr"] ent.Component = field.String
case "event":
if tags, ok := enc.Fields["tags"].([]any); ok { ent.Event = field.String
for _, item := range tags { case "object":
if tag, ok := item.(string); ok { ent.ObjectID = field.Interface.(*oid.ObjectId)
ent.Tags = append(ent.Tags, tag) case "caller":
} ent.CallerID = field.Interface.(*oid.ObjectId)
case "attr":
ent.Attr = field.Interface
case "tags":
ent.Tags, _ = field.Interface.([]string)
} }
} }
......
...@@ -62,12 +62,12 @@ func Attr(attr any) zap.Field { ...@@ -62,12 +62,12 @@ func Attr(attr any) zap.Field {
if attr == nil { if attr == nil {
return zap.Skip() return zap.Skip()
} }
return zap.Any("attr", attr) return zap.Reflect("attr", attr)
} }
func Tags(tags ...string) zap.Field { func Tags(tags ...string) zap.Field {
if len(tags) == 0 { if len(tags) == 0 {
return zap.Skip() return zap.Skip()
} }
return zap.Strings("tags", tags) return zap.Reflect("tags", tags)
} }
...@@ -173,7 +173,7 @@ func TestTags(t *testing.T) { ...@@ -173,7 +173,7 @@ func TestTags(t *testing.T) {
field zap.Field field zap.Field
want zap.Field want zap.Field
}{ }{
{name: "ok", field: Tags("a", "b", "c"), want: zap.Strings("tags", []string{"a", "b", "c"})}, {name: "ok", field: Tags("a", "b", "c"), want: zap.Reflect("tags", []string{"a", "b", "c"})},
{name: "invalid", field: Tags(nil...), want: zap.Skip()}, {name: "invalid", field: Tags(nil...), want: zap.Skip()},
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment