Documentation ¶
Overview ¶
Package log 是kratos日志库.
一、主要功能:
- 日志打印到elk
- 日志打印到本地,内部使用log4go
- 日志打印到标准输出
- 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 ¶
- func Close() (err error)
- func Debug(format string, args ...interface{})
- func Debugc(ctx context.Context, format string, args ...interface{})
- func Debugv(ctx context.Context, args ...D)
- func Debugw(ctx context.Context, args ...interface{})
- func Error(format string, args ...interface{})
- func Errorc(ctx context.Context, format string, args ...interface{})
- func Errorv(ctx context.Context, args ...D)
- func Errorw(ctx context.Context, args ...interface{})
- func Fatal(format string, args ...interface{})
- func Fatalc(ctx context.Context, format string, args ...interface{})
- func Fatalv(ctx context.Context, args ...D)
- func Fatalw(ctx context.Context, args ...interface{})
- func Info(format string, args ...interface{})
- func Infoc(ctx context.Context, format string, args ...interface{})
- func Infov(ctx context.Context, args ...D)
- func Infow(ctx context.Context, args ...interface{})
- func Init(conf *Config)
- func SetFormat(format string)
- func Warn(format string, args ...interface{})
- func Warnc(ctx context.Context, format string, args ...interface{})
- func Warnv(ctx context.Context, args ...D)
- func Warnw(ctx context.Context, args ...interface{})
- type Config
- type D
- func KV(key string, value interface{}) D
- func KVDuration(key string, value time.Duration) D
- func KVFloat32(key string, value float32) D
- func KVFloat64(key string, value float64) D
- func KVInt(key string, value int) D
- func KVInt64(key string, value int64) D
- func KVString(key string, value string) D
- func KVUint(key string, value uint) D
- func KVUint64(key string, value uint64) D
- type FileHandler
- type Handler
- type Handlers
- type Level
- type Render
- type StdoutHandler
- type Verbose
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Debug ¶ added in v0.6.0
func Debug(format string, args ...interface{})
Debug logs a message at the debug log level.
func Debugw ¶ added in v0.6.0
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 Errorw ¶
Errorw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.
func Fatal ¶ added in v0.6.0
func Fatal(format string, args ...interface{})
Fatal logs a message at the fatal log level.
func Fatalw ¶ added in v0.6.0
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 Infow ¶
Infow logs a message with some additional context. The variadic key-value pairs are treated as they are in With.
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.
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 ¶
D represents a map of entry level data used for structured logging. type D map[string]interface{}
func KV ¶
KV return a log kv for logging field. NOTE: use KV{type name} can avoid object alloc and get better performance. []~( ̄▽ ̄)~*干杯
func KVDuration ¶
KVDuration construct Field with Duration 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) 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.
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 (*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 ¶
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.