git.zd.zone/hrpc/hrpc@v0.0.12/log/option.go (about)

     1  package log
     2  
     3  import (
     4  	"io"
     5  
     6  	"git.zd.zone/hrpc/hrpc/log/hook"
     7  	"github.com/sirupsen/logrus"
     8  )
     9  
    10  // Option defines a set of options
    11  type Option struct {
    12  	// DisableReportCaller will remove the report caller info if true
    13  	DisableReportCaller bool
    14  	// Formatter will format the log message as output
    15  	Formatter logrus.Formatter
    16  	// Outer defines where the log info outputs
    17  	Outer io.Writer
    18  	// Hooks 钩子回调
    19  	Hooks []hook.Hook
    20  	// Environment the env string
    21  	Environment string
    22  	StackSkip   int
    23  }
    24  
    25  // With should be used if you want to customize you logger
    26  func With(opts ...Option) {
    27  	for _, opt := range opts {
    28  		if opt.DisableReportCaller {
    29  			option.DisableReportCaller = opt.DisableReportCaller
    30  		}
    31  		if opt.Formatter != nil {
    32  			option.Formatter = opt.Formatter
    33  		}
    34  		if opt.Outer != nil {
    35  			option.Outer = opt.Outer
    36  		}
    37  		if len(opt.Hooks) != 0 {
    38  			option.Hooks = append(option.Hooks, opt.Hooks...)
    39  		}
    40  		if opt.Environment != "" {
    41  			option.Environment = opt.Environment
    42  		}
    43  		if opt.StackSkip != 0 {
    44  			option.StackSkip = opt.StackSkip
    45  		}
    46  	}
    47  	// update settings
    48  	setup()
    49  }
    50  
    51  // AddHook for adding a hook
    52  func AddHooks(hooks ...hook.Hook) error {
    53  	if logger == nil {
    54  		return nil
    55  	}
    56  	for _, hook := range hooks {
    57  		if err := hook.Establish(); err != nil {
    58  			return err
    59  		}
    60  		logger.AddHook(hook)
    61  	}
    62  	return nil
    63  }