Documentation

Overview

    Package log 是kratos日志库.

    一、主要功能:

    1. 日志打印到elk
    2. 日志打印到本地,内部使用log4go
    3. 日志打印到标准输出
    4. verbose日志实现,参考glog实现,可通过设置不同verbose级别,默认不开启
    

    二、日志配置

    1. 默认agent配置

    目前日志已经实现默认配置,可以根据env自动切换远程日志。可以直接使用以下方式:
    log.Init(nil)
    

    2. 启动参数 or 环境变量

    启动参数		环境变量		说明
    log.stdout	LOG_STDOUT	是否开启标准输出
    log.agent	LOG_AGENT	远端日志地址:unixpacket:///var/run/lancer/collector_tcp.sock?timeout=100ms&chan=1024
    log.dir		LOG_DIR		文件日志路径
    log.v		LOG_V		verbose日志级别
    log.module	LOG_MODULE	可单独配置每个文件的verbose级别:file=1,file2=2
    log.filter	LOG_FILTER	配置需要过滤的字段:field1,field2
    

    3. 配置文件 但是如果有特殊需要可以走一下格式配置:

    [log]
    	family = "xxx-service"
    	dir = "/data/log/xxx-service/"
    	stdout = true
    	vLevel = 3
    	filter = ["fileld1", "field2"]
    [log.module]
    	"dao_user" = 2
    	"servic*" = 1
    [log.agent]
    	taskID = "00000x"
    	proto = "unixpacket"
    	addr = "/var/run/lancer/collector_tcp.sock"
    	chanSize = 10240
    

    三、配置说明

    1.log

    family		项目名,默认读环境变量$APPID
    studout		标准输出,prod环境不建议开启
    filter		配置需要过滤掉的字段,以“***”替换
    dir		文件日志地址,prod环境不建议开启
    v		开启verbose级别日志,可指定全局级别
    

    2. log.module

    可单独配置每个文件的verbose级别
    

    3. log.agent 远端日志配置项

    taskID		lancer分配的taskID
    proto		网络协议,常见:tcp, udp, unixgram
    addr		网络地址,常见:ip:prot, sock
    chanSize	日志队列长度
    

    四、最佳实践

    1. KVString 使用 KVString 代替 KV 可以减少对象分配, 避免给 golang GC 造成压力.

    Index

    Constants

    This section is empty.

    Variables

    This section is empty.

    Functions

    func Close

    func Close() (err error)

      Close close resource.

      func Debug

      func Debug(format string, args ...interface{})

        Debug logs a message at the debug log level.

        func Debugc

        func Debugc(ctx context.Context, format string, args ...interface{})

          Debugc logs a message at the debug log level.

          func Debugv

          func Debugv(ctx context.Context, args ...D)

            Debugv logs a message at the debug log level.

            func Debugw

            func Debugw(ctx context.Context, args ...interface{})

              Debugw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

              func Error

              func Error(format string, args ...interface{})

                Error logs a message at the error log level.

                func Errorc

                func Errorc(ctx context.Context, format string, args ...interface{})

                  Errorc logs a message at the error log level.

                  func Errorv

                  func Errorv(ctx context.Context, args ...D)

                    Errorv logs a message at the error log level.

                    func Errorw

                    func Errorw(ctx context.Context, args ...interface{})

                      Errorw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

                      func Fatal

                      func Fatal(format string, args ...interface{})

                        Fatal logs a message at the fatal log level.

                        func Fatalc

                        func Fatalc(ctx context.Context, format string, args ...interface{})

                          Fatalc logs a message at the fatal log level.

                          func Fatalv

                          func Fatalv(ctx context.Context, args ...D)

                            Fatalv logs a message at the error log level.

                            func Fatalw

                            func Fatalw(ctx context.Context, args ...interface{})

                              Fatalw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

                              func Info

                              func Info(format string, args ...interface{})

                                Info logs a message at the info log level.

                                func Infoc

                                func Infoc(ctx context.Context, format string, args ...interface{})

                                  Infoc logs a message at the info log level.

                                  func Infov

                                  func Infov(ctx context.Context, args ...D)

                                    Infov logs a message at the info log level.

                                    func Infow

                                    func Infow(ctx context.Context, args ...interface{})

                                      Infow logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

                                      func Init

                                      func Init(conf *Config)

                                        Init create logger with context.

                                        func SetFormat

                                        func SetFormat(format string)

                                          SetFormat only effective on stdout and file handler %T time format at "15:04:05.999" on stdout handler, "15:04:05 MST" on file handler %t time format at "15:04:05" on stdout handler, "15:04" on file on file handler %D data format at "2006/01/02" %d data format at "01/02" %L log level e.g. INFO WARN ERROR %M log message and additional fields: key=value this is log message NOTE below pattern not support on file handler %f function name and line number e.g. model.Get:121 %i instance id %e deploy env e.g. dev uat fat prod %z zone %S full file name and line number: /a/b/c/d.go:23 %s final file name element and line number: d.go:23

                                          func Warn

                                          func Warn(format string, args ...interface{})

                                            Warn logs a message at the warning log level.

                                            func Warnc

                                            func Warnc(ctx context.Context, format string, args ...interface{})

                                              Warnc logs a message at the warning log level.

                                              func Warnv

                                              func Warnv(ctx context.Context, args ...D)

                                                Warnv logs a message at the warning log level.

                                                func Warnw

                                                func Warnw(ctx context.Context, args ...interface{})

                                                  Warnw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

                                                  Types

                                                  type Config

                                                  type Config struct {
                                                  	Family string
                                                  	Host   string
                                                  
                                                  	// stdout
                                                  	Stdout bool
                                                  
                                                  	// file
                                                  	Dir string
                                                  	// buffer size
                                                  	FileBufferSize int64
                                                  	// MaxLogFile
                                                  	MaxLogFile int
                                                  	// RotateSize
                                                  	RotateSize int64
                                                  
                                                  	// V Enable V-leveled logging at the specified level.
                                                  	V int32
                                                  	// Module=""
                                                  	// The syntax of the argument is a map of pattern=N,
                                                  	// where pattern is a literal file name (minus the ".go" suffix) or
                                                  	// "glob" pattern and N is a V level. For instance:
                                                  	// [module]
                                                  	//   "service" = 1
                                                  	//   "dao*" = 2
                                                  	// sets the V level to 2 in all Go files whose names begin "dao".
                                                  	Module map[string]int32
                                                  	// Filter tell log handler which field are sensitive message, use * instead.
                                                  	Filter []string
                                                  }

                                                    Config log config.

                                                    type D

                                                    type D = core.Field

                                                      D represents a map of entry level data used for structured logging. type D map[string]interface{}

                                                      func KV

                                                      func KV(key string, value interface{}) D

                                                        KV return a log kv for logging field. NOTE: use KV{type name} can avoid object alloc and get better performance. []~( ̄▽ ̄)~*干杯

                                                        func KVDuration

                                                        func KVDuration(key string, value time.Duration) D

                                                          KVDuration construct Field with Duration value.

                                                          func KVFloat32

                                                          func KVFloat32(key string, value float32) D

                                                            KVFloat32 construct Field with float32 value.

                                                            func KVFloat64

                                                            func KVFloat64(key string, value float64) D

                                                              KVFloat64 construct Field with float64 value.

                                                              func KVInt

                                                              func KVInt(key string, value int) D

                                                                KVInt construct Field with int value.

                                                                func KVInt64

                                                                func KVInt64(key string, value int64) D

                                                                  KVInt64 construct D with int64 value.

                                                                  func KVString

                                                                  func KVString(key string, value string) D

                                                                    KVString construct Field with string value.

                                                                    func KVUint

                                                                    func KVUint(key string, value uint) D

                                                                      KVUint construct Field with uint value.

                                                                      func KVUint64

                                                                      func KVUint64(key string, value uint64) D

                                                                        KVUint64 construct Field with uint64 value.

                                                                        type FileHandler

                                                                        type FileHandler struct {
                                                                        	// contains filtered or unexported fields
                                                                        }

                                                                          FileHandler .

                                                                          func NewFile

                                                                          func NewFile(dir string, bufferSize, rotateSize int64, maxLogFile int) *FileHandler

                                                                            NewFile crete a file logger.

                                                                            func (*FileHandler) Close

                                                                            func (h *FileHandler) Close() error

                                                                              Close log handler

                                                                              func (*FileHandler) Log

                                                                              func (h *FileHandler) Log(ctx context.Context, lv Level, args ...D)

                                                                                Log loggint to file .

                                                                                func (*FileHandler) SetFormat

                                                                                func (h *FileHandler) SetFormat(format string)

                                                                                  SetFormat set log format

                                                                                  type Handler

                                                                                  type Handler interface {
                                                                                  	// Log handle log
                                                                                  	// variadic D is k-v struct represent log content
                                                                                  	Log(context.Context, Level, ...D)
                                                                                  
                                                                                  	// SetFormat set render format on log output
                                                                                  	// see StdoutHandler.SetFormat for detail
                                                                                  	SetFormat(string)
                                                                                  
                                                                                  	// Close handler
                                                                                  	Close() error
                                                                                  }

                                                                                    Handler is used to handle log events, outputting them to stdio or sending them to remote services. See the "handlers" directory for implementations.

                                                                                    It is left up to Handlers to implement thread-safety.

                                                                                    type Handlers

                                                                                    type Handlers struct {
                                                                                    	// contains filtered or unexported fields
                                                                                    }

                                                                                      Handlers a bundle for hander with filter function.

                                                                                      func (Handlers) Close

                                                                                      func (hs Handlers) Close() (err error)

                                                                                        Close close resource.

                                                                                        func (Handlers) Log

                                                                                        func (hs Handlers) Log(ctx context.Context, lv Level, d ...D)

                                                                                          Log handlers logging.

                                                                                          func (Handlers) SetFormat

                                                                                          func (hs Handlers) SetFormat(format string)

                                                                                            SetFormat .

                                                                                            type Level

                                                                                            type Level int

                                                                                              Level of severity.

                                                                                              func (Level) String

                                                                                              func (l Level) String() string

                                                                                                String implementation.

                                                                                                type Render

                                                                                                type Render interface {
                                                                                                	Render(io.Writer, map[string]interface{}) error
                                                                                                	RenderString(map[string]interface{}) string
                                                                                                }

                                                                                                  Render render log output

                                                                                                  type StdoutHandler

                                                                                                  type StdoutHandler struct {
                                                                                                  	// contains filtered or unexported fields
                                                                                                  }

                                                                                                    StdoutHandler stdout log handler

                                                                                                    func NewStdout

                                                                                                    func NewStdout() *StdoutHandler

                                                                                                      NewStdout create a stdout log handler

                                                                                                      func (*StdoutHandler) Close

                                                                                                      func (h *StdoutHandler) Close() error

                                                                                                        Close stdout loging

                                                                                                        func (*StdoutHandler) Log

                                                                                                        func (h *StdoutHandler) Log(ctx context.Context, lv Level, args ...D)

                                                                                                          Log stdout loging, only for developing env.

                                                                                                          func (*StdoutHandler) SetFormat

                                                                                                          func (h *StdoutHandler) SetFormat(format string)

                                                                                                            SetFormat set stdout log output format %T time format at "15:04:05.999" %t time format at "15:04:05" %D data format at "2006/01/02" %d data format at "01/02" %L log level e.g. INFO WARN ERROR %f function name and line number e.g. model.Get:121 %i instance id %e deploy env e.g. dev uat fat prod %z zone %S full file name and line number: /a/b/c/d.go:23 %s final file name element and line number: d.go:23 %M log message and additional fields: key=value this is log message

                                                                                                            type Verbose

                                                                                                            type Verbose bool

                                                                                                              Verbose is a boolean type that implements Info, Infov (like Printf) etc.

                                                                                                              func V

                                                                                                              func V(v int32) Verbose

                                                                                                                V reports whether verbosity at the call site is at least the requested level. The returned value is a boolean of type Verbose, which implements Info, Infov etc. These methods will write to the Info log if called. Thus, one may write either

                                                                                                                if log.V(2) { log.Info("log this") }
                                                                                                                

                                                                                                                or

                                                                                                                log.V(2).Info("log this")
                                                                                                                

                                                                                                                The second form is shorter but the first is cheaper if logging is off because it does not evaluate its arguments.

                                                                                                                Whether an individual call to V generates a log record depends on the setting of the Config.VLevel and Config.Module flags; both are off by default. If the level in the call to V is at least the value of Config.VLevel, or of Config.Module for the source file containing the call, the V call will log. v must be more than 0.

                                                                                                                func (Verbose) Close

                                                                                                                func (v Verbose) Close() (err error)

                                                                                                                  Close close resource.

                                                                                                                  func (Verbose) Info

                                                                                                                  func (v Verbose) Info(format string, args ...interface{})

                                                                                                                    Info logs a message at the info log level.

                                                                                                                    func (Verbose) Infov

                                                                                                                    func (v Verbose) Infov(ctx context.Context, args ...D)

                                                                                                                      Infov logs a message at the info log level.

                                                                                                                      func (Verbose) Infow

                                                                                                                      func (v Verbose) Infow(ctx context.Context, args ...interface{})

                                                                                                                        Infow logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

                                                                                                                        Directories

                                                                                                                        Path Synopsis
                                                                                                                        internal
                                                                                                                        core
                                                                                                                        Package core houses zap's shared internal buffer pool.
                                                                                                                        Package core houses zap's shared internal buffer pool.