github.com/wfusion/gofusion@v1.1.14/log/encoder/skip_caller.go (about)

     1  package encoder
     2  
     3  import (
     4  	"go.uber.org/zap/zapcore"
     5  
     6  	"github.com/wfusion/gofusion/common/utils"
     7  )
     8  
     9  var (
    10  	SkipCallers = []string{
    11  		"*github.com/wfusion/gofusion*/log/candy.go",
    12  		"*github.com/wfusion/gofusion*/log/logger.go",
    13  		"*github.com/wfusion/gofusion*/log/customlogger/*.go",
    14  		"*github.com/wfusion/gofusion*/db/tx.go",
    15  		"*github.com/wfusion/gofusion*/db/dal.go",
    16  		"*github.com/wfusion/gofusion*/db/candy.go",
    17  		"*github.com/wfusion/gofusion*/db/plugins/*.go",
    18  		"*github.com/wfusion/gofusion*/db/callbacks/*.go",
    19  		"*github.com/wfusion/gofusion*/db/softdelete/*.go",
    20  		"*github.com/wfusion/gofusion*/cron/log.go",
    21  		"*github.com/wfusion/gofusion*/async/log.go",
    22  		"*github.com/wfusion/gofusion*/mq/log.go",
    23  		"*github.com/wfusion/gofusion*/mq/mq.go",
    24  		"*github.com/wfusion/gofusion*/routine/*.go",
    25  		"*github.com/wfusion/gofusion*/common/infra/asynq/*.go",
    26  		"*github.com/wfusion/gofusion*/common/infra/watermill/log.go",
    27  	}
    28  
    29  	// Positions in the call stack when tracing to report the calling method
    30  	minimumCallerDepthOpt         utils.OptionExtender
    31  	defaultSuffixedRegOpt         utils.OptionExtender
    32  	defaultSuffixesRegPatternList = []string{
    33  		`go.uber.org/zap(|@v.*)/.*go$`,
    34  		`gorm.io/gorm(|@v.*)/.*go$`,
    35  		`mysql@v.*/.*go$`,
    36  		`postgres@v.*/.*go`,
    37  		`sqlserver@v.*/.*go`,
    38  		`mongo-driver(|@v.*)/.*go$`,
    39  		`github.com/redis/go-redis/v9(|@v.*)/.*go$`,
    40  		`asm_(amd64|arm64)\.s$`,
    41  	}
    42  )
    43  
    44  const (
    45  	maximumCallerDepth int = 25
    46  	knownLogrusFrames  int = 7 // should be github.com/wfusion/gofusion/log/candy.go:14
    47  )
    48  
    49  func init() {
    50  	defaultSuffixedRegOpt = utils.SkipRegexps(defaultSuffixesRegPatternList...)
    51  	minimumCallerDepthOpt = utils.SkipKnownDepth(knownLogrusFrames)
    52  }
    53  
    54  // SkipCallerEncoder skip custom stack when encoding stack
    55  func SkipCallerEncoder(skipSuffixed []string, shorter bool) func(
    56  	caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
    57  	skipGlobOpt := utils.SkipGlobs(skipSuffixed...)
    58  	return func(entryCaller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
    59  		caller := utils.GetCaller(
    60  			maximumCallerDepth,
    61  			skipGlobOpt,
    62  			defaultSuffixedRegOpt,
    63  			minimumCallerDepthOpt,
    64  		)
    65  
    66  		entryCaller.PC = caller.PC
    67  		entryCaller.File = caller.File
    68  		entryCaller.Line = caller.Line
    69  		entryCaller.Function = caller.Function
    70  		if shorter {
    71  			enc.AppendString(entryCaller.TrimmedPath())
    72  		} else {
    73  			enc.AppendString(entryCaller.FullPath())
    74  		}
    75  	}
    76  }