Skip to content

Commit

Permalink
Merge pull request #48 from lovung/main
Browse files Browse the repository at this point in the history
feat: remove custom hook for zerolog
  • Loading branch information
rogerogers authored Jan 28, 2024
2 parents 8af1031 + d0e396a commit 8239b48
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 107 deletions.
85 changes: 5 additions & 80 deletions logging/zerolog/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@
package zerolog

import (
"context"
"errors"

"github.com/cloudwego/hertz/pkg/common/hlog"
hertzzerolog "github.com/hertz-contrib/logger/zerolog"
"github.com/rs/zerolog"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)

type Logger struct {
Expand All @@ -32,13 +25,11 @@ type Logger struct {

// Ref to https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/README.md#json-formats
const (
TraceIDKey = "trace_id"
SpanIDKey = "span_id"
TraceFlagsKey = "trace_flags"
traceIDKey = "trace_id"
spanIDKey = "span_id"
traceFlagsKey = "trace_flags"
)

type ExtraKey string

func NewLogger(opts ...Option) *Logger {
cfg := defaultConfig()

Expand All @@ -47,77 +38,11 @@ func NewLogger(opts ...Option) *Logger {
opt.apply(cfg)
}
logger := *cfg.logger
zerologLogger := logger.Unwrap().Hook(cfg.getZerologHookFn())
zerologLogger := logger.Unwrap().
Hook(cfg.defaultZerologHookFn())

return &Logger{
Logger: hertzzerolog.From(zerologLogger),
config: cfg,
}
}

func (l *Logger) CtxLogf(level hlog.Level, ctx context.Context, format string, kvs ...any) {
var zlevel zerolog.Level

switch level {
case hlog.LevelDebug, hlog.LevelTrace:
zlevel = zerolog.DebugLevel
l.Logger.CtxDebugf(ctx, format, kvs...)
case hlog.LevelInfo:
zlevel = zerolog.InfoLevel
l.Logger.CtxInfof(ctx, format, kvs...)
case hlog.LevelNotice, hlog.LevelWarn:
zlevel = zerolog.WarnLevel
l.Logger.CtxWarnf(ctx, format, kvs...)
case hlog.LevelError:
zlevel = zerolog.ErrorLevel
l.Logger.CtxErrorf(ctx, format, kvs...)
case hlog.LevelFatal:
zlevel = zerolog.FatalLevel
l.Logger.CtxFatalf(ctx, format, kvs...)
default:
zlevel = zerolog.WarnLevel
l.Logger.CtxWarnf(ctx, format, kvs...)
}

span := trace.SpanFromContext(ctx)
if !span.IsRecording() {
l.Logger.Logf(level, format, kvs...)
return
}

// set span status
if zlevel >= l.config.traceConfig.errorSpanLevel {
msg := getMessage(format, kvs)
span.SetStatus(codes.Error, "")
span.RecordError(errors.New(msg), trace.WithStackTrace(l.config.traceConfig.recordStackTraceInSpan))
}
}

func (l *Logger) CtxTracef(ctx context.Context, format string, v ...any) {
l.CtxLogf(hlog.LevelDebug, ctx, format, v...)
}

func (l *Logger) CtxDebugf(ctx context.Context, format string, v ...any) {
l.CtxLogf(hlog.LevelDebug, ctx, format, v...)
}

func (l *Logger) CtxInfof(ctx context.Context, format string, v ...any) {
l.CtxLogf(hlog.LevelInfo, ctx, format, v...)
}

func (l *Logger) CtxNoticef(ctx context.Context, format string, v ...any) {
l.CtxLogf(hlog.LevelWarn, ctx, format, v...)
}

func (l *Logger) CtxWarnf(ctx context.Context, format string, v ...any) {
l.CtxLogf(hlog.LevelWarn, ctx, format, v...)
}

func (l *Logger) CtxErrorf(ctx context.Context, format string, v ...any) {
l.CtxLogf(hlog.LevelError, ctx, format, v...)
}

func (l *Logger) CtxFatalf(ctx context.Context, format string, v ...any) {
// l.CtxLogf(hlog.LevelFatal, ctx, format, v...)
l.Logger.CtxFatalf(ctx, format, v...)
}
12 changes: 4 additions & 8 deletions logging/zerolog/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ type traceConfig struct {
type config struct {
logger *hertzzerolog.Logger
traceConfig *traceConfig
hookFunc zerolog.HookFunc
}

// defaultConfig default config
Expand Down Expand Up @@ -76,10 +75,7 @@ func WithRecordStackTraceInSpan(recordStackTraceInSpan bool) Option {
})
}

func (cfg config) getZerologHookFn() zerolog.HookFunc {
if cfg.hookFunc != nil {
return cfg.hookFunc
}
func (cfg config) defaultZerologHookFn() zerolog.HookFunc {
return func(e *zerolog.Event, level zerolog.Level, message string) {
ctx := e.GetCtx()
span := trace.SpanFromContext(ctx)
Expand All @@ -89,9 +85,9 @@ func (cfg config) getZerologHookFn() zerolog.HookFunc {
return
}

e.Any(SpanIDKey, spanCtx.SpanID())
e.Any(TraceIDKey, spanCtx.TraceID())
e.Any(TraceFlagsKey, spanCtx.TraceFlags())
e.Any(spanIDKey, spanCtx.SpanID())
e.Any(traceIDKey, spanCtx.TraceID())
e.Any(traceFlagsKey, spanCtx.TraceFlags())

if !span.IsRecording() {
return
Expand Down
19 changes: 0 additions & 19 deletions logging/zerolog/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,11 @@
package zerolog

import (
"fmt"
"strings"

"github.com/rs/zerolog"
)

// getMessage format with Sprint, Sprintf, or neither.
func getMessage(template string, fmtArgs []any) string {
if len(fmtArgs) == 0 {
return template
}

if template != "" {
return fmt.Sprintf(template, fmtArgs...)
}

if len(fmtArgs) == 1 {
if str, ok := fmtArgs[0].(string); ok {
return str
}
}
return fmt.Sprint(fmtArgs...)
}

// OtelSeverityText convert zerolog level to otel severityText
// ref to https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#severity-fields
func OtelSeverityText(lv zerolog.Level) string {
Expand Down

0 comments on commit 8239b48

Please sign in to comment.