上传日志优化代码

This commit is contained in:
liwei1dao 2022-07-13 15:06:48 +08:00
parent 0e7e6169a2
commit 2bb353817e
14 changed files with 141 additions and 57 deletions

View File

@ -42,6 +42,7 @@ const (
ModuleShop core.M_Modules = "shop" //商店模块
ModuleTask core.M_Modules = "task" //任务模块
ModuleStory core.M_Modules = "story" //主线模块
ModuleNotify core.M_Modules = "notify" //公告模块
)
//RPC服务接口定义处

View File

@ -50,8 +50,11 @@ func NewSys(option ...Option) (sys ILog, err error) {
}
func Clone(option ...Option) ILog {
if defsys != nil {
return defsys.Clone(option...)
}
return nil
}
func Debug(msg string, fields ...Field) { defsys.Debug(msg, fields...) }
func Info(msg string, fields ...Field) { defsys.Info(msg, fields...) }

View File

@ -2,10 +2,12 @@ package gm
import (
"go_dreamfactory/lego/utils/mapstructure"
"go_dreamfactory/modules"
)
type (
Options struct {
modules.Options
Port int
Key string
}
@ -13,6 +15,9 @@ type (
func (this *Options) LoadConfig(settings map[string]interface{}) (err error) {
if settings != nil {
if err = this.Options.LoadConfig(settings); err != nil {
return
}
err = mapstructure.Decode(settings, this)
}
return

View File

@ -2,7 +2,6 @@ package items
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/pb"
"time"
@ -37,7 +36,7 @@ func (this *apiComp) Getlist(session comm.IUserSession, req *pb.ItemsGetlistReq)
}()
if items, err = this.module.modelItems.Pack_QueryUserPack(session.GetUserId()); err != nil {
log.Errorf("QueryUserPackReq err:%v", err)
this.module.Errorf("QueryUserPackReq err:%v", err)
code = pb.ErrorCode_CacheReadError
return
} else {

View File

@ -7,7 +7,6 @@ import (
cfg "go_dreamfactory/sys/configure/structs"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
)
const (
@ -17,11 +16,13 @@ const (
///背包配置管理组件
type ConfigureComp struct {
modules.MCompConfigure
module *Items
}
//组件初始化接口
func (this *ConfigureComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.MCompConfigure.Init(service, module, comp, options)
this.module = module.(*Items)
err = this.LoadConfigure(game_item, cfg.NewGame_item)
return
}
@ -32,7 +33,7 @@ func (this *ConfigureComp) GetItemsConfigure() (items map[int32]*cfg.Game_itemDa
v interface{}
)
if v, err = this.GetConfigure(game_item); err != nil {
log.Errorf("err:%v", err)
this.module.Errorf("err:%v", err)
return
} else {
items = v.(*cfg.Game_item).GetDataMap()
@ -47,12 +48,12 @@ func (this *ConfigureComp) GetItemConfigure(id int32) (item *cfg.Game_itemData,
ok bool
)
if v, err = this.GetConfigure(game_item); err != nil {
log.Errorf("err:%v", err)
this.module.Errorf("err:%v", err)
return
} else {
if item, ok = v.(*cfg.Game_item).GetDataMap()[id]; !ok {
err = fmt.Errorf("no found item:%d configure", id)
log.Errorf("err:%v", err)
this.module.Errorf("err:%v", err)
return
}
}
@ -70,7 +71,7 @@ func (this *ConfigureComp) GetPackItemByType(itmes []*pb.DB_UserItemData, usetyp
err error
)
if v, err = this.GetConfigure(game_item); err != nil {
log.Errorf("err:%v", err)
this.module.Errorf("err:%v", err)
return
} else {
table = v.(*cfg.Game_item)
@ -80,7 +81,7 @@ func (this *ConfigureComp) GetPackItemByType(itmes []*pb.DB_UserItemData, usetyp
result = append(result, v)
}
} else {
log.Errorf("no found itemConfigure:%d", v.ItemId)
this.module.Errorf("no found itemConfigure:%d", v.ItemId)
}
}
}

View File

@ -34,14 +34,18 @@ func (this *ModelItemsComp) Init(service core.IService, module core.IModule, com
///查询用户背包数据
func (this *ModelItemsComp) Pack_QueryUserPack(uId string) (itmes []*pb.DB_UserItemData, err error) {
itmes = make([]*pb.DB_UserItemData, 0)
err = this.GetList(uId, &itmes)
if err = this.GetList(uId, &itmes); err != nil {
this.module.Errorf("err:%v", err)
}
return
}
///查询用户指定格子的物品数据
func (this *ModelItemsComp) Pack_QueryUserPackByGridId(uId string, grid string) (itme *pb.DB_UserItemData, err error) {
itme = &pb.DB_UserItemData{}
err = this.GetListObj(uId, grid, itme)
if err = this.GetListObj(uId, grid, itme); err != nil {
this.module.Errorf("err:%v", err)
}
return
}
@ -55,7 +59,9 @@ func (this *ModelItemsComp) Pack_AddUserPack(uId string, itmes ...*pb.DB_UserIte
//更新用户的背包信息
func (this *ModelItemsComp) Pack_DelUserPack(uId string, ids ...string) (err error) {
err = this.DelListlds(uId, ids...)
if err = this.DelListlds(uId, ids...); err != nil {
this.module.Errorf("err:%v", err)
}
return
}
@ -71,7 +77,10 @@ func (this *ModelItemsComp) Pack_UpdateUserPack(uId string, itmes ...*pb.DB_User
//更新用户的背包信息
func (this *ModelItemsComp) Pack_DeleteUserPack(uId string, gridIds ...string) (err error) {
err = this.DelListlds(uId, gridIds...)
if err = this.DelListlds(uId, gridIds...); err != nil {
this.module.Errorf("err:%v", err)
return
}
return
}
@ -82,6 +91,7 @@ func (this *ModelItemsComp) Pack_QueryUserPackItemsAmount(uId string, itemid ...
err error
)
if itmes, err = this.Pack_QueryUserPack(uId); err != nil {
this.module.Errorf("err:%v", err)
return
}
result = map[int32]uint32{}
@ -108,6 +118,7 @@ func (this *ModelItemsComp) Pack_AddItemToUserPack(uId string, itemId int32, add
return
}
if itmes, err = this.Pack_QueryUserPack(uId); err != nil {
this.module.Errorf("err:%v", err)
return
}
add, update, del, leftnum = this.pack_addItemToUserPack(itmes, itemId, addnum)
@ -120,16 +131,19 @@ func (this *ModelItemsComp) Pack_AddItemToUserPack(uId string, itemId int32, add
}
if len(add) > 0 {
if err = this.Pack_AddUserPack(uId, add...); err != nil {
this.module.Errorf("err:%v", err)
return
}
}
if len(del) > 0 {
if err = this.Pack_DeleteUserPack(uId, del...); err != nil {
this.module.Errorf("err:%v", err)
return
}
}
if len(update) > 0 {
if err = this.Pack_UpdateUserPack(uId, update...); err != nil {
this.module.Errorf("err:%v", err)
return
}
}
@ -147,6 +161,7 @@ func (this *ModelItemsComp) Pack_AddItemsToUserPack(uId string, items map[int32]
leftnum int64
)
if itmes, err = this.Pack_QueryUserPack(uId); err != nil {
this.module.Errorf("err:%v", err)
return
}
for k, v := range items {
@ -160,16 +175,19 @@ func (this *ModelItemsComp) Pack_AddItemsToUserPack(uId string, items map[int32]
}
if len(add) > 0 {
if err = this.Pack_AddUserPack(uId, add...); err != nil {
this.module.Errorf("err:%v", err)
return
}
}
if len(del) > 0 {
if err = this.Pack_DeleteUserPack(uId, del...); err != nil {
this.module.Errorf("err:%v", err)
return
}
}
if len(update) > 0 {
if err = this.Pack_UpdateUserPack(uId, update...); err != nil {
this.module.Errorf("err:%v", err)
return
}
}
@ -190,10 +208,12 @@ func (this *ModelItemsComp) Pack_AddItemToUserPackByGrid(uId string, gridid stri
return
}
if conf, err = this.module.configure.GetItemConfigure(itemId); err != nil {
this.module.Errorf("err:%v", err)
return
}
if itme, err = this.Pack_QueryUserPackByGridId(uId, gridid); err != nil {
this.module.Errorf("err:%v", err)
return
}
amount = int64(itme.Amount)

View File

@ -6,7 +6,6 @@ import (
"go_dreamfactory/pb"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
)
/*
@ -48,7 +47,7 @@ func (this *Items) OnInstallComp() {
//IItems-------------------------------------------------------------------------------------------------------------------------------
///查询用户背包物品数量
func (this *Items) QueryItemAmount(source *comm.ModuleCallSource, uId string, itemid int32) (amount uint32) {
defer log.Debugf("获取物品 uId:%s itemid:%d addnum:%d ", uId, itemid, amount)
defer this.Debugf("获取物品 uId:%s itemid:%d addnum:%d ", uId, itemid, amount)
amount = 0
if result := this.modelItems.Pack_QueryUserPackItemsAmount(uId, itemid); result != nil && len(result) > 0 {
return result[itemid]
@ -65,9 +64,9 @@ func (this *Items) QueryItemsAmount(source *comm.ModuleCallSource, uId string, i
///添加单个物品到背包 (可以加物品和减物品)
func (this *Items) AddItem(source *comm.ModuleCallSource, uId string, itemid, addnum int32) (code pb.ErrorCode) {
var err error
defer log.Debugf("给用户添加物品 uId:%s itemid:%d addnum:%d issucc:%v", uId, itemid, addnum, err == nil)
defer this.Debugf("给用户添加物品 uId:%s itemid:%d addnum:%d issucc:%v", uId, itemid, addnum, err == nil)
if err = this.modelItems.Pack_AddItemToUserPack(uId, itemid, addnum); err != nil {
log.Errorf("给用户添加物品 uId:%s itemid:%d addnum:%d err:%v", uId, itemid, addnum, err)
this.Errorf("给用户添加物品 uId:%s itemid:%d addnum:%d err:%v", uId, itemid, addnum, err)
if err == ItemNotEnoughError {
code = pb.ErrorCode_ItemsNoEnough
} else if err == PackGridNumUpper {
@ -82,9 +81,9 @@ func (this *Items) AddItem(source *comm.ModuleCallSource, uId string, itemid, ad
///添加多个物品到背包 (可以加物品和减物品)
func (this *Items) AddItems(source *comm.ModuleCallSource, uId string, items map[int32]int32) (code pb.ErrorCode) {
var err error
defer log.Debugf("给用户添加物品 uId:%s items:%d items:%v", uId, items, err == nil)
defer this.Debugf("给用户添加物品 uId:%s items:%d items:%v", uId, items, err == nil)
if err = this.modelItems.Pack_AddItemsToUserPack(uId, items); err != nil {
log.Errorf("给用户添加物品 uId:%s items:%d err:%v", uId, items, err)
this.Errorf("给用户添加物品 uId:%s items:%d err:%v", uId, items, err)
if err == ItemNotEnoughError {
code = pb.ErrorCode_ItemsNoEnough
} else if err == PackGridNumUpper {

View File

@ -23,6 +23,7 @@ type ModuleBase struct {
cbase.ModuleBase
module core.IModule
service base.IRPCXService
options IOptions
scomp comm.ISC_GateRouteComp //网关服务组件
//常用的一些通用模块 在底层注册好
ModuleUser comm.IUser //用户模块
@ -32,11 +33,17 @@ type ModuleBase struct {
ModuleTask comm.ITask //任务
}
//重构模块配置对象
func (this *ModuleBase) NewOptions() (options core.IModuleOptions) {
return new(Options)
}
//模块初始化接口
func (this *ModuleBase) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
err = this.ModuleBase.Init(service, module, options)
this.service = service.(base.IRPCXService)
this.module = module
this.options = options.(IOptions)
return
}
@ -228,3 +235,35 @@ func (this *ModuleBase) DispenseRes(uid string, res []*cfg.Game_atn) (code pb.Er
}
return
}
//日志接口
func (this *ModuleBase) Debugf(format string, a ...interface{}) {
if this.options.GetDebug() {
this.options.GetLog().Debugf(fmt.Sprintf("[Module:%s] ", this.module.GetType())+format, a...)
}
}
func (this *ModuleBase) Infof(format string, a ...interface{}) {
if this.options.GetDebug() {
this.options.GetLog().Infof(fmt.Sprintf("[Module:%s] ", this.module.GetType())+format, a...)
}
}
func (this *ModuleBase) Warnf(format string, a ...interface{}) {
if this.options.GetDebug() {
this.options.GetLog().Warnf(fmt.Sprintf("[Module:%s] ", this.module.GetType())+format, a...)
}
}
func (this *ModuleBase) Errorf(format string, a ...interface{}) {
if this.options.GetDebug() {
this.options.GetLog().Errorf(fmt.Sprintf("[Module:%s] ", this.module.GetType())+format, a...)
}
}
func (this *ModuleBase) Panicf(format string, a ...interface{}) {
if this.options.GetDebug() {
this.options.GetLog().Panicf(fmt.Sprintf("[Module:%s] ", this.module.GetType())+format, a...)
}
}
func (this *ModuleBase) Fatalf(format string, a ...interface{}) {
if this.options.GetDebug() {
this.options.GetLog().Fatalf(fmt.Sprintf("[Module:%s] ", this.module.GetType())+format, a...)
}
}

View File

@ -1,23 +0,0 @@
package notify
import (
"go_dreamfactory/modules"
"go_dreamfactory/lego/core"
)
const (
game_equipment = "game_equipment.json"
)
///背包配置管理组件
type configureComp struct {
modules.MCompConfigure
}
//组件初始化接口
func (this *configureComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.ModuleCompBase.Init(service, module, comp, options)
return
}

View File

@ -3,7 +3,6 @@ package notify
import (
"context"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"time"
@ -43,14 +42,14 @@ func (this *modelNotifyComp) GetFullNotify() (result []*pb.DBSystemNotify, err e
for c.Next(context.Background()) {
notify := &pb.DBSystemNotify{}
if err = c.Decode(notify); err != nil {
log.Errorf("GetFullNotify err:%v", err)
this.module.Errorf("GetFullNotify err:%v", err)
break
}
notifys[notify.Id] = notify
}
if len(notifys) > 0 {
if err = this.Redis.HMSet(this.TableName, notifys); err != nil {
log.Errorf("GetFullNotify err:%v", err)
this.module.Errorf("GetFullNotify err:%v", err)
}
}
}

View File

@ -20,7 +20,6 @@ func NewModule() core.IModule {
type Notification struct {
modules.ModuleBase
api_comp *apiComp
configure *configureComp
modelNotify *modelNotifyComp
}
@ -40,5 +39,4 @@ func (this *Notification) OnInstallComp() {
this.ModuleBase.OnInstallComp()
this.api_comp = this.RegisterComp(new(apiComp)).(*apiComp)
this.modelNotify = this.RegisterComp(new(modelNotifyComp)).(*modelNotifyComp)
this.configure = this.RegisterComp(new(configureComp)).(*configureComp)
}

42
modules/options.go Normal file
View File

@ -0,0 +1,42 @@
package modules
import (
"fmt"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/lego/utils/mapstructure"
)
type (
IOptions interface {
core.IModuleOptions
GetDebug() bool
GetLog() log.ILog
}
Options struct {
Debug bool //日志是否开启
Log log.ILog
}
)
func (this *Options) GetDebug() bool {
return this.Debug
}
func (this *Options) GetLog() log.ILog {
return this.Log
}
func (this *Options) LoadConfig(settings map[string]interface{}) (err error) {
if settings != nil {
err = mapstructure.Decode(settings, this)
}
if this.Debug && this.Log == nil {
this.Log = log.Clone()
if this.Log == nil {
err = fmt.Errorf("Log is nil")
return
}
}
return
}

View File

@ -3,7 +3,6 @@ package shop
import (
"fmt"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
cfg "go_dreamfactory/sys/configure/structs"
@ -17,11 +16,13 @@ const (
///背包配置管理组件
type configureComp struct {
modules.MCompConfigure
module *Shop
}
//组件初始化接口
func (this *configureComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.ModuleCompBase.Init(service, module, comp, options)
this.module = module.(*Shop)
this.LoadConfigure(game_shop, cfg.NewGame_shop)
this.LoadConfigure(game_shopitem, cfg.NewGame_shopitem)
return
@ -34,12 +35,12 @@ func (this *configureComp) GetShopConfigure(id int32) (configure *cfg.Game_shopD
ok bool
)
if v, err = this.GetConfigure(game_shop); err != nil {
log.Errorf("err:%v", err)
this.module.Errorf("err:%v", err)
return
} else {
if configure, ok = v.(*cfg.Game_shop).GetDataMap()[id]; !ok {
err = fmt.Errorf("ShopConfigure not found:%d ", id)
log.Errorf("err:%v", err)
this.module.Errorf("err:%v", err)
return
}
}
@ -53,12 +54,12 @@ func (this *configureComp) GetShopItemsConfigure(key int32) (result *cfg.Game_sh
ok bool
)
if v, err = this.GetConfigure(game_shopitem); err != nil {
log.Errorf("err:%v", err)
this.module.Errorf("err:%v", err)
return
} else {
if result, ok = v.(*cfg.Game_shopitem).GetDataMap()[key]; !ok {
err = fmt.Errorf("ShopConfigure not found:%d ", key)
log.Errorf("err:%v", err)
this.module.Errorf("err:%v", err)
return
}
}
@ -73,7 +74,7 @@ func (this *configureComp) GetShopItemsConfigureByGroups(groupid int32, user *pb
table *cfg.Game_shopitem
)
if v, err = this.GetConfigure(game_shopitem); err != nil {
log.Errorf("err:%v", err)
this.module.Errorf("err:%v", err)
return
} else {
table = v.(*cfg.Game_shopitem)
@ -99,7 +100,7 @@ func (this *configureComp) GetShopItemsConfigureByIds(keys ...int32) (result []*
ok bool
)
if v, err = this.GetConfigure(game_shopitem); err != nil {
log.Errorf("err:%v", err)
this.module.Errorf("err:%v", err)
return
} else {
table = v.(*cfg.Game_shopitem)
@ -107,7 +108,7 @@ func (this *configureComp) GetShopItemsConfigureByIds(keys ...int32) (result []*
if item, ok = table.GetDataMap()[v]; ok {
result = append(result, item)
} else {
log.Errorf("no found GetShopItemsConfigureByIds:%d", v)
this.module.Errorf("no found GetShopItemsConfigureByIds:%d", v)
}
}
}

View File

@ -2,7 +2,6 @@ package shop
import (
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/lego/sys/mgo"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
@ -35,6 +34,7 @@ func (this *modelShopItemsComp) QueryUserShopData(uId string) (result map[int32]
result = make(map[int32]*pb.DBShopItem)
data := make([]*pb.DBShopItem, 0)
if err = this.GetList(uId, &data); err != nil && err != mgo.MongodbNil {
err = nil
return
}
err = nil
@ -52,7 +52,7 @@ func (this *modelShopItemsComp) QueryUserShopDataByGoodId(uId string, goodId int
)
data = make([]*pb.DBShopItem, 0)
if err = this.GetList(uId, &data); err != nil {
log.Errorf("err%v", err)
this.module.Errorf("err%v", err)
return
}
for _, v := range data {