Skip to content

Commit

Permalink
fix: update log event
Browse files Browse the repository at this point in the history
  • Loading branch information
kooksee committed Jan 19, 2024
1 parent 2c86b58 commit f9786f6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 81 deletions.
11 changes: 5 additions & 6 deletions log/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ func CreateEventCtx(ctx context.Context, evt *Event) context.Context {
return context.WithValue(ctx, ctxEventKey{}, evt)
}

func withEventCtx(ctx context.Context) func(e *Event) {
return func(e *Event) {
var evt, ok = ctx.Value(ctxEventKey{}).(*Event)
if ok {
WithEvent(evt)(e)
}
func getEventFromCtx(ctx context.Context) *Event {
var evt, ok = ctx.Value(ctxEventKey{}).(*Event)
if ok {
return evt
}
return nil
}
27 changes: 27 additions & 0 deletions log/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,30 @@ func convertEvent(event2 *Event) *event {
func NewEvent() *Event {
return zerolog.Dict()
}

func mergeEvent(to *Event, from ...*Event) *Event {
if len(from) == 0 {
return to
}

if to == nil {
to = zerolog.Dict()
}

to1 := convertEvent(to)
to1.buf = bytes.TrimRight(to1.buf, ",")
for i := range from {
if from[i] == nil {
continue
}

from1 := convertEvent(from[i])
from1.buf = bytes.TrimLeft(from1.buf, "{")
from1.buf = bytes.Trim(from1.buf, ",")
if len(from1.buf) > 0 {
to1.buf = append(to1.buf, ',')
to1.buf = append(to1.buf, from1.buf...)
}
}
return to
}
91 changes: 16 additions & 75 deletions log/impl.log.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package log

import (
"bytes"
"context"
"fmt"
"github.com/rs/zerolog"
Expand All @@ -13,7 +12,7 @@ type loggerImpl struct {
name string
log *zerolog.Logger
fields Map
content []byte
content *Event
callerSkip int
lvl Level
}
Expand All @@ -29,20 +28,8 @@ func (l *loggerImpl) WithEvent(evt *Event) Logger {
return l
}

putEvent(evt)
evt1 := convertEvent(evt)
if len(evt1.buf) > 0 {
evt1.buf = bytes.TrimLeft(bytes.TrimSpace(evt1.buf), "{")
evt1.buf = bytes.TrimRight(evt1.buf, ",")
}

var log = l.copy()
logContent := make([]byte, 0, len(evt1.buf)+len(l.content)+1)
logContent = append(logContent, log.content...)
if len(logContent) > 0 && len(evt1.buf) > 0 {
logContent = append(logContent, ',')
}
log.content = append(logContent, evt1.buf...)
log.content = mergeEvent(l.content, evt)

return log
}
Expand Down Expand Up @@ -95,90 +82,55 @@ func (l *loggerImpl) Debug(ctxL ...context.Context) *zerolog.Event {
return nil
}

var ctx = context.Background()
if len(ctxL) > 0 {
ctx = ctxL[0]
}

return l.newEvent(func(log *zerolog.Logger) *zerolog.Event { return log.Debug().Func(withEventCtx(ctx)) })
return l.newEvent(ctxL, l.getLog().Debug())
}

func (l *loggerImpl) Info(ctxL ...context.Context) *zerolog.Event {
if !l.enabled(zerolog.InfoLevel) {
return nil
}

var ctx = context.Background()
if len(ctxL) > 0 {
ctx = ctxL[0]
}

return l.newEvent(func(log *zerolog.Logger) *zerolog.Event { return log.Info().Func(withEventCtx(ctx)) })
return l.newEvent(ctxL, l.getLog().Info())
}

func (l *loggerImpl) Warn(ctxL ...context.Context) *zerolog.Event {
if !l.enabled(zerolog.WarnLevel) {
return nil
}

var ctx = context.Background()
if len(ctxL) > 0 {
ctx = ctxL[0]
}

return l.newEvent(func(log *zerolog.Logger) *zerolog.Event { return log.Warn().Func(withEventCtx(ctx)) })
return l.newEvent(ctxL, l.getLog().Warn())
}

func (l *loggerImpl) Error(ctxL ...context.Context) *zerolog.Event {
if !l.enabled(zerolog.ErrorLevel) {
return nil
}

var ctx = context.Background()
if len(ctxL) > 0 {
ctx = ctxL[0]
}

return l.newEvent(func(log *zerolog.Logger) *zerolog.Event { return log.Error().Func(withEventCtx(ctx)) })
return l.newEvent(ctxL, l.getLog().Error())
}

func (l *loggerImpl) Err(err error, ctxL ...context.Context) *zerolog.Event {
if !l.enabled(zerolog.ErrorLevel) {
return nil
}

var ctx = context.Background()
if len(ctxL) > 0 {
ctx = ctxL[0]
}

return l.newEvent(func(log *zerolog.Logger) *zerolog.Event { return log.Err(err).Func(withEventCtx(ctx)) })
return l.newEvent(ctxL, l.getLog().Err(err))
}

func (l *loggerImpl) Panic(ctxL ...context.Context) *zerolog.Event {
if !l.enabled(zerolog.PanicLevel) {
return nil
}

var ctx = context.Background()
if len(ctxL) > 0 {
ctx = ctxL[0]
}

return l.newEvent(func(log *zerolog.Logger) *zerolog.Event { return log.Panic().Func(withEventCtx(ctx)) })
return l.newEvent(ctxL, l.getLog().Panic())
}

func (l *loggerImpl) Fatal(ctxL ...context.Context) *zerolog.Event {
if !l.enabled(zerolog.FatalLevel) {
return nil
}

var ctx = context.Background()
if len(ctxL) > 0 {
ctx = ctxL[0]
}

return l.newEvent(func(log *zerolog.Logger) *zerolog.Event { return log.Fatal().Func(withEventCtx(ctx)) })
return l.newEvent(ctxL, l.getLog().Fatal())
}

func (l *loggerImpl) enabled(lvl zerolog.Level) bool {
Expand All @@ -197,9 +149,12 @@ func (l *loggerImpl) getLog() *zerolog.Logger {
return stdZerolog
}

func (l *loggerImpl) newEvent(fn func(log *zerolog.Logger) *zerolog.Event) *zerolog.Event {
var log = l.getLog()
e := fn(log)
func (l *loggerImpl) newEvent(ctxL []context.Context, e *zerolog.Event) *zerolog.Event {
var ctx = context.Background()
if len(ctxL) > 0 {
ctx = ctxL[0]
}

if l.name != "" {
e = e.Str("logger", l.name)
}
Expand All @@ -212,19 +167,5 @@ func (l *loggerImpl) newEvent(fn func(log *zerolog.Logger) *zerolog.Event) *zero
e = e.Fields(l.fields)
}

e1 := convertEvent(e)
if len(e1.buf) > 0 {
e1.buf = bytes.TrimLeft(e1.buf, "{")
e1.buf = bytes.TrimRight(e1.buf, ",")
}

if len(l.content) > 0 && len(e1.buf) > 0 {
e1.buf = append(e1.buf, ',')
}
e1.buf = append(e1.buf, l.content...)

if len(e1.buf) > 0 && e1.buf[0] != '{' {
e1.buf = append([]byte{'{'}, e1.buf...)
}
return e
return mergeEvent(e, getEventFromCtx(ctx), l.content)
}

0 comments on commit f9786f6

Please sign in to comment.