1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-01-10 00:29:01 +02:00
kratos/pkg/log/handler.go

119 lines
2.4 KiB
Go
Raw Normal View History

package log
import (
"context"
2019-03-06 13:11:45 +02:00
"time"
pkgerr "github.com/pkg/errors"
)
const (
2019-03-06 13:11:45 +02:00
_timeFormat = "2006-01-02T15:04:05.999999"
// log level defined in level.go.
_levelValue = "level_value"
// log level name: INFO, WARN...
_level = "level"
// log time.
_time = "time"
2019-06-09 14:57:28 +02:00
// request path.
// _title = "title"
2019-03-06 13:11:45 +02:00
// log file.
_source = "source"
// common log filed.
_log = "log"
2019-03-06 13:11:45 +02:00
// app name.
_appID = "app_id"
// container ID.
_instanceID = "instance_id"
// uniq ID from trace.
_tid = "traceid"
// request time.
2019-06-09 14:57:28 +02:00
// _ts = "ts"
2019-03-06 13:11:45 +02:00
// requester.
_caller = "caller"
// container environment: prod, pre, uat, fat.
_deplyEnv = "env"
// container area.
_zone = "zone"
// mirror flag
_mirror = "mirror"
// color.
_color = "color"
2019-06-09 14:57:28 +02:00
// env_color
_envColor = "env_color"
2019-03-06 13:11:45 +02:00
// cluster.
_cluster = "cluster"
)
// 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 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
}
2019-03-06 13:11:45 +02:00
func newHandlers(filters []string, handlers ...Handler) *Handlers {
set := make(map[string]struct{})
for _, k := range filters {
set[k] = struct{}{}
}
return &Handlers{filters: set, handlers: handlers}
}
// Handlers a bundle for hander with filter function.
type Handlers struct {
filters map[string]struct{}
handlers []Handler
}
// Log handlers logging.
2019-06-09 14:57:28 +02:00
func (hs Handlers) Log(ctx context.Context, lv Level, d ...D) {
hasSource := false
2019-03-06 13:11:45 +02:00
for i := range d {
if _, ok := hs.filters[d[i].Key]; ok {
d[i].Value = "***"
}
if d[i].Key == _source {
2019-04-02 10:58:20 +02:00
hasSource = true
2019-03-06 13:11:45 +02:00
}
}
2019-04-02 10:58:20 +02:00
if !hasSource {
fn := funcName(3)
2019-06-09 14:57:28 +02:00
errIncr(lv, fn)
2019-04-02 10:58:20 +02:00
d = append(d, KVString(_source, fn))
2019-03-06 13:11:45 +02:00
}
2019-04-02 10:58:20 +02:00
d = append(d, KV(_time, time.Now()), KVInt64(_levelValue, int64(lv)), KVString(_level, lv.String()))
2019-03-06 13:11:45 +02:00
for _, h := range hs.handlers {
2019-06-09 14:57:28 +02:00
h.Log(ctx, lv, d...)
}
}
// Close close resource.
func (hs Handlers) Close() (err error) {
2019-03-06 13:11:45 +02:00
for _, h := range hs.handlers {
if e := h.Close(); e != nil {
err = pkgerr.WithStack(e)
}
}
return
}
// SetFormat .
func (hs Handlers) SetFormat(format string) {
2019-03-06 13:11:45 +02:00
for _, h := range hs.handlers {
h.SetFormat(format)
}
}