2022-11-24 23:37:34 +08:00
|
|
|
|
// Package global
|
|
|
|
|
// @Link https://github.com/bufanyun/hotgo
|
2023-02-23 17:53:04 +08:00
|
|
|
|
// @Copyright Copyright (c) 2023 HotGo CLI
|
2022-11-24 23:37:34 +08:00
|
|
|
|
// @Author Ms <133814250@qq.com>
|
|
|
|
|
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
|
|
|
|
package global
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
2023-06-09 19:13:26 +08:00
|
|
|
|
"github.com/gogf/gf/contrib/trace/jaeger/v2"
|
2022-12-11 20:48:18 +08:00
|
|
|
|
"github.com/gogf/gf/v2"
|
2023-11-25 18:36:11 +08:00
|
|
|
|
"github.com/gogf/gf/v2/container/gvar"
|
2023-01-25 11:49:21 +08:00
|
|
|
|
"github.com/gogf/gf/v2/encoding/gjson"
|
2023-01-18 16:23:39 +08:00
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
2023-01-25 11:49:21 +08:00
|
|
|
|
"github.com/gogf/gf/v2/os/gctx"
|
2023-02-23 17:53:04 +08:00
|
|
|
|
"github.com/gogf/gf/v2/os/gfile"
|
2023-01-25 11:49:21 +08:00
|
|
|
|
"github.com/gogf/gf/v2/os/glog"
|
2022-11-24 23:37:34 +08:00
|
|
|
|
"github.com/gogf/gf/v2/os/gtime"
|
2023-01-25 11:49:21 +08:00
|
|
|
|
"github.com/gogf/gf/v2/text/gstr"
|
2023-11-25 18:36:11 +08:00
|
|
|
|
"github.com/gogf/gf/v2/util/gmode"
|
2022-12-11 20:48:18 +08:00
|
|
|
|
"hotgo/internal/consts"
|
2023-02-23 17:53:04 +08:00
|
|
|
|
"hotgo/internal/library/cache"
|
2023-01-25 11:49:21 +08:00
|
|
|
|
"hotgo/internal/library/queue"
|
|
|
|
|
"hotgo/internal/model/entity"
|
|
|
|
|
"hotgo/internal/service"
|
|
|
|
|
"hotgo/utility/charset"
|
2023-06-09 19:13:26 +08:00
|
|
|
|
"hotgo/utility/simple"
|
2023-11-25 18:36:11 +08:00
|
|
|
|
"hotgo/utility/validate"
|
2023-08-02 17:38:40 +08:00
|
|
|
|
"runtime"
|
2023-02-08 20:29:34 +08:00
|
|
|
|
"strings"
|
2022-11-24 23:37:34 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Init(ctx context.Context) {
|
2023-11-25 18:36:11 +08:00
|
|
|
|
// 设置gf运行模式
|
|
|
|
|
SetGFMode(ctx)
|
|
|
|
|
|
2023-05-09 20:13:13 +08:00
|
|
|
|
// 设置服务日志处理
|
2023-11-25 18:36:11 +08:00
|
|
|
|
glog.SetDefaultHandler(LoggingServeLogHandler)
|
2023-05-09 20:13:13 +08:00
|
|
|
|
|
2022-11-24 23:37:34 +08:00
|
|
|
|
// 默认上海时区
|
2023-05-29 20:24:13 +08:00
|
|
|
|
if err := gtime.SetTimeZone("Asia/Shanghai"); err != nil {
|
2023-02-23 17:53:04 +08:00
|
|
|
|
g.Log().Fatalf(ctx, "时区设置异常 err:%+v", err)
|
2022-11-24 23:37:34 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-02 17:38:40 +08:00
|
|
|
|
fmt.Printf("欢迎使用HotGo!\r\n当前运行环境:%v, 运行根路径为:%v \r\nHotGo版本:v%v, gf版本:%v \n", runtime.GOOS, gfile.Pwd(), consts.VersionApp, gf.VERSION)
|
2023-01-18 16:23:39 +08:00
|
|
|
|
|
2023-06-09 19:13:26 +08:00
|
|
|
|
// 初始化链路追踪
|
|
|
|
|
InitTrace(ctx)
|
|
|
|
|
|
2023-02-23 17:53:04 +08:00
|
|
|
|
// 设置缓存适配器
|
|
|
|
|
cache.SetAdapter(ctx)
|
2023-01-25 11:49:21 +08:00
|
|
|
|
|
2023-05-10 23:54:50 +08:00
|
|
|
|
// 初始化功能库配置
|
|
|
|
|
service.SysConfig().InitConfig(ctx)
|
2023-07-20 18:01:10 +08:00
|
|
|
|
|
|
|
|
|
// 加载超管数据
|
|
|
|
|
service.AdminMember().LoadSuperAdmin(ctx)
|
2023-07-26 16:49:09 +08:00
|
|
|
|
|
|
|
|
|
// 订阅集群同步
|
|
|
|
|
SubscribeClusterSync(ctx)
|
2023-01-18 16:23:39 +08:00
|
|
|
|
}
|
2023-01-25 11:49:21 +08:00
|
|
|
|
|
2023-06-05 20:14:57 +08:00
|
|
|
|
// LoggingServeLogHandler 服务日志处理
|
|
|
|
|
// 需要将异常日志保存到服务日志时可以通过SetHandlers设置此方法
|
2023-01-25 11:49:21 +08:00
|
|
|
|
func LoggingServeLogHandler(ctx context.Context, in *glog.HandlerInput) {
|
|
|
|
|
in.Next(ctx)
|
|
|
|
|
|
2023-02-08 20:29:34 +08:00
|
|
|
|
err := g.Try(ctx, func(ctx context.Context) {
|
|
|
|
|
var err error
|
|
|
|
|
defer func() {
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}()
|
2023-02-23 17:53:04 +08:00
|
|
|
|
|
2023-11-25 18:36:11 +08:00
|
|
|
|
// web服务日志不做记录,因为会导致重复记录
|
|
|
|
|
r := g.RequestFromCtx(ctx)
|
|
|
|
|
if r != nil && r.Server != nil && in.Logger.GetConfig().Path == r.Server.Logger().GetConfig().Path {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-08 20:29:34 +08:00
|
|
|
|
conf, err := service.SysConfig().GetLoadServeLog(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-01-25 11:49:21 +08:00
|
|
|
|
|
2023-02-08 20:29:34 +08:00
|
|
|
|
if conf == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-01-25 17:49:16 +08:00
|
|
|
|
|
2023-02-08 20:29:34 +08:00
|
|
|
|
if !conf.Switch {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-01-25 11:49:21 +08:00
|
|
|
|
|
2023-02-08 20:29:34 +08:00
|
|
|
|
if in.LevelFormat == "" || !gstr.InArray(conf.LevelFormat, in.LevelFormat) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-01-25 11:49:21 +08:00
|
|
|
|
|
2023-02-08 20:29:34 +08:00
|
|
|
|
if in.Stack == "" {
|
2023-07-03 11:23:40 +08:00
|
|
|
|
in.Stack = in.Logger.GetStack()
|
2023-02-08 20:29:34 +08:00
|
|
|
|
}
|
2023-01-25 11:49:21 +08:00
|
|
|
|
|
2023-11-25 18:36:11 +08:00
|
|
|
|
if len(in.Content) == 0 {
|
|
|
|
|
in.Content = gstr.StrLimit(gvar.New(in.Values).String(), consts.MaxServeLogContentLen)
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-08 20:29:34 +08:00
|
|
|
|
var data entity.SysServeLog
|
|
|
|
|
data.TraceId = gctx.CtxId(ctx)
|
|
|
|
|
data.LevelFormat = in.LevelFormat
|
2023-11-25 18:36:11 +08:00
|
|
|
|
data.Content = in.Content
|
2023-02-08 20:29:34 +08:00
|
|
|
|
data.Stack = gjson.New(charset.ParseStack(in.Stack))
|
|
|
|
|
data.Line = strings.TrimRight(in.CallerPath, ":")
|
|
|
|
|
data.TriggerNs = in.Time.UnixNano()
|
|
|
|
|
data.Status = consts.StatusEnabled
|
|
|
|
|
|
|
|
|
|
if gstr.Contains(in.Content, `exception recovered`) {
|
|
|
|
|
data.LevelFormat = "PANI"
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-06 17:28:16 +08:00
|
|
|
|
if data.Stack.IsNil() {
|
|
|
|
|
data.Stack = gjson.New(consts.NilJsonToString)
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-08 20:29:34 +08:00
|
|
|
|
if conf.Queue {
|
|
|
|
|
err = queue.Push(consts.QueueServeLogTopic, data)
|
|
|
|
|
} else {
|
|
|
|
|
err = service.SysServeLog().RealWrite(ctx, data)
|
|
|
|
|
}
|
|
|
|
|
})
|
2023-01-25 11:49:21 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
2023-09-06 17:28:16 +08:00
|
|
|
|
g.Dump("LoggingServeLogHandler err:", err)
|
2023-01-25 11:49:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-09 19:13:26 +08:00
|
|
|
|
|
|
|
|
|
// InitTrace 初始化链路追踪
|
|
|
|
|
func InitTrace(ctx context.Context) {
|
2023-08-02 17:38:40 +08:00
|
|
|
|
if !g.Cfg().MustGet(ctx, "jaeger.switch").Bool() {
|
2023-06-09 19:13:26 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tp, err := jaeger.Init(simple.AppName(ctx), g.Cfg().MustGet(ctx, "jaeger.endpoint").String())
|
|
|
|
|
if err != nil {
|
|
|
|
|
g.Log().Fatal(ctx, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
simple.Event().Register(consts.EventServerClose, func(ctx context.Context, args ...interface{}) {
|
|
|
|
|
_ = tp.Shutdown(ctx)
|
|
|
|
|
g.Log().Debug(ctx, "jaeger closed ..")
|
|
|
|
|
})
|
|
|
|
|
}
|
2023-11-25 18:36:11 +08:00
|
|
|
|
|
|
|
|
|
// SetGFMode 设置gf运行模式
|
|
|
|
|
func SetGFMode(ctx context.Context) {
|
|
|
|
|
mode := g.Cfg().MustGet(ctx, "hotgo.mode").String()
|
|
|
|
|
if len(mode) == 0 {
|
|
|
|
|
mode = gmode.NOT_SET
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var modes = []string{gmode.DEVELOP, gmode.TESTING, gmode.STAGING, gmode.PRODUCT}
|
|
|
|
|
|
|
|
|
|
// 如果是有效的运行模式,就进行设置
|
|
|
|
|
if validate.InSlice(modes, mode) {
|
|
|
|
|
gmode.Set(mode)
|
|
|
|
|
}
|
|
|
|
|
}
|