Skip to content

Commit

Permalink
feat: add Panic func and export CallerFrameToSkip variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Galiley authored and fsamin committed Apr 6, 2023
1 parent 6991d8b commit 7638ea5
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ loop:
})
}

// CallerFrameToSkip correspond to the number of frame to skip while retrieving the caller stack
var CallerFrameToSkip = 2

func Skip(field Field, value interface{}) {
excludeRulesMutex.Lock()
defer excludeRulesMutex.Unlock()
Expand All @@ -62,23 +65,27 @@ func Skip(field Field, value interface{}) {
}

func Debug(ctx context.Context, format string, args ...interface{}) {
call(ctx, LevelDebug, 2, format, args...)
call(ctx, LevelDebug, format, args...)
}

func Info(ctx context.Context, format string, args ...interface{}) {
call(ctx, LevelInfo, 2, format, args...)
call(ctx, LevelInfo, format, args...)
}

func Warn(ctx context.Context, format string, args ...interface{}) {
call(ctx, LevelWarn, 2, format, args...)
call(ctx, LevelWarn, format, args...)
}

func Error(ctx context.Context, format string, args ...interface{}) {
call(ctx, LevelError, 2, format, args...)
call(ctx, LevelError, format, args...)
}

func Fatal(ctx context.Context, format string, args ...interface{}) {
call(ctx, LevelFatal, 2, format, args...)
call(ctx, LevelFatal, format, args...)
}

func Panic(ctx context.Context, format string, args ...interface{}) {
call(ctx, LevelPanic, format, args...)
}

var (
Expand All @@ -92,14 +99,14 @@ func init() {
RegisterField(FieldSourceFile, FieldSourceLine, FieldCaller, FieldStackTrace)
}

func call(ctx context.Context, level Level, callerOffset int, format string, args ...interface{}) {
func call(ctx context.Context, level Level, format string, args ...interface{}) {
entry := Factory()

if level < entry.GetLevel() {
return
}

pc, file, line, ok := runtime.Caller(callerOffset)
pc, file, line, ok := runtime.Caller(CallerFrameToSkip)
if ok {
ctx = context.WithValue(ctx, FieldSourceFile, file)
ctx = context.WithValue(ctx, FieldSourceLine, line)
Expand Down Expand Up @@ -156,7 +163,7 @@ func ContextWithStackTrace(ctx context.Context, err error) context.Context {

func ErrorWithStackTrace(ctx context.Context, err error) {
ctx = ContextWithStackTrace(ctx, err)
call(ctx, LevelError, 2, err.Error())
call(ctx, LevelError, err.Error())
}

func FieldValues(ctx context.Context) map[Field]interface{} {
Expand Down

0 comments on commit 7638ea5

Please sign in to comment.