Skip to content

Commit

Permalink
feat: add Fatal level
Browse files Browse the repository at this point in the history
  • Loading branch information
qloog committed Jul 27, 2024
1 parent 8f865df commit 8066606
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
23 changes: 19 additions & 4 deletions pkg/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

// log is A global variable so that log functions can be directly accessed
var log Logger
var zl *zap.Logger
var logger *zap.Logger

// Fields Type to pass when we want to call WithFields for structured logging
type Fields map[string]interface{}
Expand All @@ -32,6 +32,11 @@ type Logger interface {
Error(args ...interface{})
Errorf(format string, args ...interface{})

// Fatal logs a message at Fatal level
// and process will exit with status set to 1.
Fatal(args ...interface{})
Fatalf(format string, args ...interface{})

WithFields(keyValues Fields) Logger
}

Expand All @@ -53,7 +58,7 @@ func Init(opts ...Option) Logger {
}

// new zap logger
zl, err = newZapLogger(cfg, opts...)
logger, err = newZapLogger(cfg, opts...)
if err != nil {
_ = fmt.Errorf("init newZapLogger err: %v", err)
}
Expand All @@ -74,15 +79,15 @@ func GetLogger() Logger {

// GetZapLogger return raw zap logger
func GetZapLogger() *zap.Logger {
return zl
return logger
}

// WithContext is a logger that can log msg and log span for trace
func WithContext(ctx context.Context) Logger {
//return zap logger

if span := trace.SpanFromContext(ctx); span != nil {
logger := spanLogger{span: span, logger: zl}
logger := spanLogger{span: span, logger: logger}

spanCtx := span.SpanContext()
logger.spanFields = []zapcore.Field{
Expand Down Expand Up @@ -115,6 +120,11 @@ func Error(args ...interface{}) {
log.Error(args...)
}

// Fatal logger
func Fatal(args ...interface{}) {
log.Fatal(args...)
}

// Debugf logger
func Debugf(format string, args ...interface{}) {
log.Debugf(format, args...)
Expand All @@ -135,6 +145,11 @@ func Errorf(format string, args ...interface{}) {
log.Errorf(format, args...)
}

// Fatalf logger
func Fatalf(format string, args ...interface{}) {
log.Fatalf(format, args...)
}

// WithFields logger
// output more field, eg:
//
Expand Down
16 changes: 16 additions & 0 deletions pkg/log/span_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ func (sl spanLogger) Errorf(format string, args ...interface{}) {
sl.logger.Error(msg, append(sl.spanFields, fields...)...)
}

func (sl spanLogger) Fatal(args ...interface{}) {
msg := fmt.Sprint(args...)
var fields []zap.Field
sl.logToSpan("error", msg)
sl.span.RecordError(errors.New(msg))
sl.logger.Fatal(msg, append(sl.spanFields, fields...)...)
}

func (sl spanLogger) Fatalf(format string, args ...interface{}) {
msg := fmt.Sprint(format, args)
var fields []zap.Field
sl.logToSpan("Errorf", msg)
sl.span.RecordError(errors.New(msg))
sl.logger.Fatal(msg, append(sl.spanFields, fields...)...)
}

func (sl spanLogger) WithFields(keyValues Fields) Logger {
panic("implement me")
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/log/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const (
logSuffix = ".log"
warnLogSuffix = "_warn.log"
errorLogSuffix = "_error.log"

// defaultSkip zapLogger 包装了一层 zap.Logger,默认要跳过一层
defaultSkip = 1
)

const (
Expand All @@ -32,8 +35,6 @@ const (
RotateTimeHourly = "hourly"
)

const defaultSkip = 1 // zapLogger 包装了一层 zap.Logger,默认要跳过一层

var (
hostname string
logDir string
Expand Down

0 comments on commit 8066606

Please sign in to comment.