github.com/SupenBysz/gf-admin-community@v0.7.4/internal/logic/sys_logs/sys_logs.go (about)

     1  package sys_logs
     2  
     3  import (
     4  	"context"
     5  	"github.com/SupenBysz/gf-admin-community/sys_consts"
     6  	"github.com/SupenBysz/gf-admin-community/sys_model/sys_dao"
     7  	"github.com/SupenBysz/gf-admin-community/sys_model/sys_entity"
     8  	"github.com/SupenBysz/gf-admin-community/sys_service"
     9  	"github.com/gogf/gf/v2/database/gdb"
    10  	"github.com/gogf/gf/v2/encoding/gjson"
    11  	"github.com/gogf/gf/v2/errors/gerror"
    12  	"github.com/gogf/gf/v2/frame/g"
    13  	"github.com/gogf/gf/v2/net/ghttp"
    14  	"github.com/gogf/gf/v2/os/glog"
    15  
    16  	"github.com/gogf/gf/v2/os/gtime"
    17  	"github.com/yitter/idgenerator-go/idgen"
    18  )
    19  
    20  type sSysLogs struct {
    21  }
    22  
    23  func init() {
    24  	sys_service.RegisterSysLogs(New())
    25  }
    26  
    27  // New SysLogs 业务日志逻辑实现
    28  func New() *sSysLogs {
    29  	return &sSysLogs{}
    30  }
    31  
    32  // Write 写日志
    33  func (s *sSysLogs) Write(ctx context.Context, err error, info sys_entity.SysLogs) error {
    34  	if info.Category == sys_dao.SysCasbin.Table() {
    35  		info.Category = "Casbin"
    36  	} else if info.Category == sys_dao.SysFile.Table() {
    37  		info.Category = "文件管理"
    38  	} else if info.Category == sys_dao.SysMenu.Table() {
    39  		info.Category = "菜单管理"
    40  	} else if info.Category == sys_dao.SysOrganization.Table() {
    41  		info.Category = "组织管理"
    42  	} else if info.Category == sys_dao.SysPermission.Table() {
    43  		info.Category = "权限"
    44  	} else if info.Category == sys_dao.SysRole.Table() {
    45  		info.Category = "角色"
    46  	} else if info.Category == sys_dao.SysSmsLogs.Table() {
    47  		info.Category = "短信"
    48  	} else if info.Category == sys_dao.SysUser.Table() {
    49  		info.Category = "用户"
    50  	}
    51  
    52  	info.Error = info.Context
    53  
    54  	if err != nil {
    55  		info.Error = err.Error()
    56  	} else {
    57  		info.Error = info.Context
    58  	}
    59  
    60  	if info.Context != "" {
    61  		err = gerror.New(info.Context)
    62  	}
    63  
    64  	if info.Context == "" {
    65  		info.Context = info.Error
    66  	}
    67  
    68  	if info.UserId == 0 {
    69  		g.Try(ctx, func(ctx context.Context) {
    70  			if sys_service.SysSession().Get(ctx) != nil {
    71  				info.UserId = sys_service.SysSession().Get(ctx).JwtClaimsUser.Id
    72  			}
    73  
    74  			r := ghttp.RequestFromCtx(ctx)
    75  			if r != nil {
    76  				info.Content = gjson.MustEncodeString(g.Map{
    77  					"url":    r.URL.Path,
    78  					"body":   r.GetBodyString(),
    79  					"header": r.Header,
    80  				})
    81  			}
    82  		})
    83  	}
    84  
    85  	if sys_consts.Global.LogLevelToDatabaseArr.Search(info.Level) == -1 {
    86  		return err
    87  	}
    88  
    89  	g.Try(ctx, func(ctx context.Context) {
    90  
    91  		info.Id = idgen.NextId()
    92  		info.CreatedAt = gtime.Now()
    93  		sys_dao.SysLogs.Ctx(context.Background()).Cache(gdb.CacheOption{Duration: -1, Force: false}).Insert(info)
    94  	})
    95  
    96  	return err
    97  }
    98  
    99  // Write 写错误日志
   100  func (s *sSysLogs) Error(ctx context.Context, err error, info sys_entity.SysLogs) error {
   101  	info.Level = glog.LEVEL_ERRO
   102  	g.Log(info.Category).Level(info.Level).Error(ctx, info.Context)
   103  	return s.Write(ctx, err, info)
   104  }
   105  
   106  // ErrorSimple 写错误日志
   107  func (s *sSysLogs) ErrorSimple(ctx context.Context, err error, context string, category string) error {
   108  	info := sys_entity.SysLogs{
   109  		Context:  context,
   110  		Category: category,
   111  	}
   112  	return s.Error(ctx, err, info)
   113  }
   114  
   115  // Info 写日志信息
   116  func (s *sSysLogs) Info(ctx context.Context, err error, info sys_entity.SysLogs) error {
   117  	info.Level = glog.LEVEL_INFO
   118  	g.Log(info.Category).Level(info.Level).Info(ctx, info.Context)
   119  	return s.Write(ctx, err, info)
   120  }
   121  
   122  // InfoSimple 写日志信息
   123  func (s *sSysLogs) InfoSimple(ctx context.Context, err error, context string, category string) error {
   124  	info := sys_entity.SysLogs{
   125  		Context:  context,
   126  		Category: category,
   127  	}
   128  	return s.Info(ctx, err, info)
   129  }
   130  
   131  // Warn 写警示日志
   132  func (s *sSysLogs) Warn(ctx context.Context, err error, info sys_entity.SysLogs) error {
   133  	info.Level = glog.LEVEL_WARN
   134  	g.Log(info.Category).Level(info.Level).Warning(ctx, info.Context)
   135  	return s.Write(ctx, err, info)
   136  }
   137  
   138  // WarnSimple 写警示日志
   139  func (s *sSysLogs) WarnSimple(ctx context.Context, err error, context string, category string) error {
   140  	info := sys_entity.SysLogs{
   141  		Context:  context,
   142  		Category: category,
   143  	}
   144  	return s.Warn(ctx, err, info)
   145  }