Skip to content

Commit

Permalink
feat: add capability to customize caller format (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
deryrahman committed Jul 1, 2023
1 parent 513ffbb commit 4c4c4c2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
8 changes: 7 additions & 1 deletion formatter_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type JSONFormatter struct {
PrettyPrint bool
// TimeFormat the time format layout. default is DefaultTimeFormat
TimeFormat string
// CallerFormatFunc the caller format layout. default is defined by CallerFlag
CallerFormatFunc CallerFormatFn
}

// NewJSONFormatter create new JSONFormatter
Expand Down Expand Up @@ -89,7 +91,11 @@ func (f *JSONFormatter) Format(r *Record) ([]byte, error) {
case field == FieldKeyTimestamp:
logData[outName] = r.timestamp()
case field == FieldKeyCaller && r.Caller != nil:
logData[outName] = formatCaller(r.Caller, r.CallerFlag)
if f.CallerFormatFunc != nil {
logData[outName] = f.CallerFormatFunc(r.Caller)
} else {
logData[outName] = formatCaller(r.Caller, r.CallerFlag)
}
case field == FieldKeyLevel:
logData[outName] = r.LevelName()
case field == FieldKeyChannel:
Expand Down
10 changes: 9 additions & 1 deletion formatter_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type TextFormatter struct {
// EncodeFunc data encode for Record.Data, Record.Extra, etc.
// Default is encode by EncodeToString()
EncodeFunc func(v any) string
// CallerFormatFunc the caller format layout. default is defined by CallerFlag
CallerFormatFunc CallerFormatFn
}

// NewTextFormatter create new TextFormatter
Expand Down Expand Up @@ -118,7 +120,13 @@ func (f *TextFormatter) Format(r *Record) ([]byte, error) {
case field == FieldKeyTimestamp:
buf.WriteString(r.timestamp())
case field == FieldKeyCaller && r.Caller != nil:
buf.WriteString(formatCaller(r.Caller, r.CallerFlag))
var callerLog string
if f.CallerFormatFunc != nil {
callerLog = f.CallerFormatFunc(r.Caller)
} else {
callerLog = formatCaller(r.Caller, r.CallerFlag)
}
buf.WriteString(callerLog)
case field == FieldKeyLevel:
// output colored logs for console
if f.EnableColor {
Expand Down
2 changes: 2 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
// return nil
// }

type CallerFormatFn func(rf *runtime.Frame) (cs string)

Check warning on line 27 in util.go

View workflow job for this annotation

GitHub Actions / Test on go 1.19 and ubuntu-latest

exported type CallerFormatFn should have comment or be unexported

Check warning on line 27 in util.go

View workflow job for this annotation

GitHub Actions / Test on go 1.18 and ubuntu-latest

exported type CallerFormatFn should have comment or be unexported

Check warning on line 27 in util.go

View workflow job for this annotation

GitHub Actions / Test on go 1.20 and ubuntu-latest

exported type CallerFormatFn should have comment or be unexported

func buildLowerLevelName() map[Level]string {
mp := make(map[Level]string, len(LevelNames))
for level, s := range LevelNames {
Expand Down

0 comments on commit 4c4c4c2

Please sign in to comment.