You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
199 lines
4.2 KiB
199 lines
4.2 KiB
package log |
|
|
|
import ( |
|
"context" |
|
"encoding/json" |
|
"fmt" |
|
cproto "git.gz.internal.jumaiyx.cn/pkg/config/v2/cproto" |
|
klog "git.gz.internal.jumaiyx.cn/pkg/log/klog" |
|
"git.gz.internal.jumaiyx.cn/pkg/log/request" |
|
"git.gz.internal.jumaiyx.cn/pkg/log/tracing" |
|
"os" |
|
"strings" |
|
"sync" |
|
"time" |
|
) |
|
|
|
const ( |
|
|
|
// LevelDebug 终端打印 |
|
LevelDebug = "DEBUG" |
|
|
|
// LevelInfo 不打印 |
|
LevelInfo = "INFO" |
|
|
|
// LevelPath 写入文件【待开发】 |
|
LevelPath = "PATH" |
|
|
|
// LevelPanic 终端打印/写入日志 |
|
LevelPanic = "PANIC" |
|
|
|
logfile = "./log/log.log" |
|
) |
|
|
|
type KLogger interface { |
|
Log(level klog.Level, keyvals ...interface{}) error |
|
Ctx() context.Context |
|
} |
|
|
|
type LoggerData struct { |
|
Server string `json:"server"` |
|
ReqId string `json:"req_id"` |
|
DateTime string `json:"date_time"` |
|
Level string `json:"level"` |
|
Message string `json:"message"` |
|
File []string `json:"file"` |
|
//Line int `json:"line"` |
|
} |
|
|
|
type SystemLog struct { |
|
TraceId string `json:"trace_id"` // 预留链路使用 |
|
SpanId int `json:"span_id"` // 预留链路使用 |
|
ParentId string `json:"parent_id"` // 预留链路父级SpanId |
|
ReqId string `json:"req_id"` // 请求链id |
|
ServerName string `json:"server_name"` // 服务名 |
|
Level string `json:"level"` // 日志等级 |
|
Message string `json:"message"` // 日志内容 |
|
CreateAt string `json:"create_at"` // 日志时间 |
|
Files []string `json:"files"` // 关联文件 |
|
} |
|
|
|
type klogger struct { |
|
// 日志等级 |
|
level string |
|
|
|
// 服务名 |
|
serverName string |
|
|
|
// 日志服务池 |
|
pool *sync.Pool |
|
|
|
// kratos日志包 |
|
logger klog.Logger |
|
|
|
//prefix []interface{} |
|
//hasValuer bool |
|
ctx context.Context |
|
|
|
path string |
|
|
|
// 调试模式,开始这个模式之后只有打印 |
|
debug bool |
|
} |
|
|
|
func NewKLogger(c *cproto.Log) klog.Logger { |
|
|
|
switch c.Level { |
|
case "": |
|
c.Level = LevelDebug |
|
case LevelPath: |
|
if c.Path == "" { |
|
c.Path = logfile |
|
} |
|
default: |
|
c.Level = strings.ToUpper(c.Level) |
|
} |
|
|
|
kl := &klogger{ |
|
level: c.Level, |
|
path: c.Path, |
|
debug: c.Debug, |
|
serverName: c.Name, |
|
} |
|
|
|
if !c.Debug { |
|
//topic := Topic(c.Kafka.Topic) |
|
//kl.pool = &sync.Pool{ |
|
// New: func() interface{} { |
|
// return consumer.Consumer() |
|
// }, |
|
//} |
|
} |
|
|
|
return kl |
|
} |
|
|
|
// Log print the kv pairs log. |
|
func (l *klogger) Log(level klog.Level, keyvals ...interface{}) error { |
|
var ( |
|
reqId string |
|
spanId int |
|
) |
|
l.ctx, reqId = request.GetReqId(l.ctx) |
|
if len(keyvals) == 0 { |
|
return nil |
|
} |
|
if (len(keyvals) & 1) == 1 { |
|
keyvals = append(keyvals, "KEYVALS UNPAIRED") |
|
} |
|
l.ctx, spanId = tracing.GetSpanId(l.ctx) |
|
files := File() |
|
data := SystemLog{ |
|
ReqId: reqId, |
|
SpanId: spanId, |
|
CreateAt: time.Now().Format("2006-01-02 15:04:05"), |
|
Level: level.String(), |
|
Files: files, |
|
ServerName: l.serverName, |
|
} |
|
if l.serverName == "" { |
|
data.ServerName = serviceName() |
|
} |
|
for i := 0; i < len(keyvals); i += 2 { |
|
s, ok := keyvals[i+1].(string) |
|
if ok { |
|
data.Message = s |
|
} else { |
|
s, _ := json.Marshal(keyvals[i+1]) |
|
data.Message = string(s) |
|
} |
|
} |
|
if l.level == LevelDebug { |
|
fmt.Printf("[%s] [%s] req_id:[%s] %s \n", data.CreateAt, data.Level, data.ReqId, data.Message) |
|
} |
|
|
|
if l.level == LevelPanic { |
|
fmt.Printf("[%s] [%s] req_id:[%s] span_id:[%d] %s %v \n", data.CreateAt, data.Level, data.ReqId, data.SpanId, data.Message, strings.Join(data.Files, "\n")) |
|
} |
|
|
|
if l.level == LevelInfo && level.String() == LevelDebug { |
|
fmt.Printf("[%s] [%s] req_id:[%s] %s \n", data.CreateAt, data.Level, data.ReqId, data.Message) |
|
} |
|
|
|
// 忽略debug的打印 |
|
if level.String() != LevelDebug && !l.debug { |
|
//go func() { |
|
// sender := l.pool.Get().(*kafka.Sender) |
|
// if sender != nil { |
|
// if err := sender.Pub(context.TODO(), LogKafkaKey, data); err != nil { |
|
// fmt.Printf("Kafka sender failed:%v", err) |
|
// } |
|
// } |
|
// return |
|
//}() |
|
} |
|
return nil |
|
} |
|
|
|
func (l *klogger) SetCtx(ctx context.Context) { |
|
l.ctx = ctx |
|
} |
|
|
|
func (l *klogger) GetCtx() context.Context { |
|
return l.ctx |
|
} |
|
|
|
func (l *klogger) Close() error { |
|
return nil |
|
} |
|
func mkdir() string { |
|
dir, _ := os.Getwd() |
|
return dir |
|
} |
|
|
|
// 获取服务名 |
|
func serviceName() string { |
|
dir, _ := os.Getwd() |
|
split := strings.Split(dir, "/") |
|
return split[len(split)-1] |
|
}
|
|
|