Skip to content

Commit

Permalink
fix: 2024-04-01 17:03:02
Browse files Browse the repository at this point in the history
  • Loading branch information
kooksee committed Apr 1, 2024
1 parent 3f37758 commit 261f737
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 24 deletions.
13 changes: 8 additions & 5 deletions log/aaa.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ import (
"github.com/rs/zerolog"
)

type Map = map[string]any
type Hook = zerolog.Hook
type Event = zerolog.Event
type Level = zerolog.Level
type (
Map = map[string]any
Hook = zerolog.Hook
Event = zerolog.Event
Level = zerolog.Level
LogEnableChecker = func(lvl Level, name string, fields Map) bool
)

type Logger interface {
WithName(name string) Logger
WithFields(m Map) Logger
WithCallerSkip(skip int) Logger
WithEvent(evt *Event) Logger
WithLevel(lvl Level) Logger
WithEnableChecker(cb func(lvl Level, name string, fields Map) bool) Logger
WithEnableChecker(cb LogEnableChecker) Logger

Debug(ctx ...context.Context) *Event
Info(ctx ...context.Context) *Event
Expand Down
2 changes: 1 addition & 1 deletion log/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func CreateEventCtx(ctx context.Context, evt *Event) context.Context {
}

func getEventFromCtx(ctx context.Context) *Event {
var evt, ok = ctx.Value(ctxEventKey{}).(*Event)
evt, ok := ctx.Value(ctxEventKey{}).(*Event)
if ok {
return evt
}
Expand Down
24 changes: 12 additions & 12 deletions log/impl.log.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ type loggerImpl struct {
content *Event
callerSkip int
lvl Level
enableChecker func(lvl Level, name string, fields Map) bool
enableChecker LogEnableChecker
}

func (l *loggerImpl) WithEnableChecker(cb func(lvl Level, name string, fields Map) bool) Logger {
var log = l.copy()
func (l *loggerImpl) WithEnableChecker(cb LogEnableChecker) Logger {
log := l.copy()
log.enableChecker = cb
return log
}

func (l *loggerImpl) WithLevel(lvl Level) Logger {
var log = l.copy()
log := l.copy()
log.lvl = lvl
return log
}
Expand All @@ -37,7 +37,7 @@ func (l *loggerImpl) WithEvent(evt *Event) Logger {
return l
}

var log = l.copy()
log := l.copy()
log.content = mergeEvent(l.content, evt)

return log
Expand All @@ -48,7 +48,7 @@ func (l *loggerImpl) WithCallerSkip(skip int) Logger {
return l
}

var log = l.copy()
log := l.copy()
log.callerSkip += skip
return log
}
Expand All @@ -58,7 +58,7 @@ func (l *loggerImpl) WithName(name string) Logger {
return l
}

var log = l.copy()
log := l.copy()
if log.name == "" {
log.name = name
} else {
Expand All @@ -72,8 +72,8 @@ func (l *loggerImpl) WithFields(m Map) Logger {
return l
}

var log = l.copy()
var logFields = make(Map, len(m)+len(log.fields))
log := l.copy()
logFields := make(Map, len(m)+len(log.fields))
for k, v := range m {
logFields[k] = v
}
Expand Down Expand Up @@ -125,7 +125,7 @@ func (l *loggerImpl) Err(err error, ctxL ...context.Context) *zerolog.Event {

if err != nil {
if errJson, ok := err.(json.Marshaler); ok {
var errJsonBytes, _ = errJson.MarshalJSON()
errJsonBytes, _ := errJson.MarshalJSON()
if errJsonBytes != nil && len(errJsonBytes) > 0 {
return l.newEvent(ctxL, l.getLog().Error().Str("error", err.Error()).RawJSON("error_detail", errJsonBytes))
}
Expand Down Expand Up @@ -164,7 +164,7 @@ func (l *loggerImpl) enabled(lvl zerolog.Level) bool {
}

func (l *loggerImpl) copy() *loggerImpl {
var log = *l
log := *l
return &log
}

Expand All @@ -176,7 +176,7 @@ func (l *loggerImpl) getLog() *zerolog.Logger {
}

func (l *loggerImpl) newEvent(ctxL []context.Context, e *zerolog.Event) *zerolog.Event {
var ctx = context.Background()
ctx := context.Background()
if len(ctxL) > 0 {
ctx = ctxL[0]
}
Expand Down
2 changes: 1 addition & 1 deletion log/z_log_std_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import (
)

func TestStdLog(t *testing.T) {
var evt = log.NewEvent().Str("hello", "world").Int("int", 100)
evt := log.NewEvent().Str("hello", "world").Int("int", 100)
log.NewStd(log.GetLogger("with_event").WithEvent(evt)).Print("dddd")
}
10 changes: 5 additions & 5 deletions log/z_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestName(t *testing.T) {
log.Info().Str("hello", "world world").Msg("ok ok")
log.Warn().Str("hello", "world world").Msg("ok ok")

var err = errors.WrapCaller(fmt.Errorf("test error"))
err := errors.WrapCaller(fmt.Errorf("test error"))
err = errors.Wrap(err, "next error")
err = errors.WrapTag(err, errors.T("event", "test event"), errors.T("test123", 123), errors.T("test", "hello"))
err = errors.Wrapf(err, "next error name=%s", "wrapf")
Expand All @@ -35,20 +35,20 @@ func TestName(t *testing.T) {
}

func TestEvent(t *testing.T) {
var evt = log.NewEvent().Str("hello", "world").Int("int", 100).Dict("ddd", log.NewEvent())
evt := log.NewEvent().Str("hello", "world").Int("int", 100).Dict("ddd", log.NewEvent())
ctx := log.CreateEventCtx(context.Background(), evt)
ee := log.Info(ctx).Str("info", "abcd").Func(log.WithEvent(evt))
ee.Msg("dddd")
}

func TestWithEvent(t *testing.T) {
var evt = log.NewEvent().Str("hello", "hello world").Int("int", 100)
evt := log.NewEvent().Str("hello", "hello world").Int("int", 100)
ee := log.GetLogger("with_event").WithEvent(evt).Info().Str("info", "abcd")
ee.Msg("dddd")
}

func TestSetLog(t *testing.T) {
//zerolog.SetGlobalLevel(zerolog.ErrorLevel)
// zerolog.SetGlobalLevel(zerolog.ErrorLevel)
log.SetLogger(generic.Ptr(zl.Output(zerolog.NewConsoleWriter())))
log.Debug().Msg("test")
log.Info().Msg("test")
Expand All @@ -57,7 +57,7 @@ func TestSetLog(t *testing.T) {
}

func TestChecker(t *testing.T) {
var l = log.GetLogger("test-checker")
l := log.GetLogger("test-checker")
l.Info().Msg("hello")

l = l.WithEnableChecker(func(lvl log.Level, name string, fields log.Map) bool {
Expand Down

0 comments on commit 261f737

Please sign in to comment.