monitor_init.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package logs
  2. import (
  3. "github.com/gogf/gf/v2/os/gcfg"
  4. "github.com/gogf/gf/v2/os/gctx"
  5. "github.com/gogf/gf/v2/os/glog"
  6. "log"
  7. )
  8. var GInfo *glog.Logger // 系统日志输出
  9. var GReq *serverLog // 服务请求记录
  10. func InitLogs() {
  11. ctx := gctx.New()
  12. logCommon := &commonLogConfig{
  13. Path: gcfg.Instance().MustGet(ctx, "system.log.path", "./logs").String(),
  14. Debug: gcfg.Instance().MustGet(ctx, "system.log.debug", false).Bool(),
  15. Stdout: gcfg.Instance().MustGet(ctx, "system.log.stdout", false).Bool(),
  16. }
  17. //初始化GInfo
  18. gInfoName := gcfg.Instance().MustGet(ctx, "system.log.pattern", "system-{Ymd}.log").String()
  19. GInfo = initBaseLog(logCommon, gInfoName)
  20. //设置系统默认日志
  21. glog.SetDefaultLogger(GInfo)
  22. //将log输出转至GInfo
  23. log.SetOutput(GInfo)
  24. //初始化通知
  25. var noticeConfig NoticeConfig
  26. if err := gcfg.Instance().MustGet(ctx, "system.alarm").Scan(&noticeConfig); err != nil {
  27. GInfo.Errorf(ctx, "nsq通知配置异常", err)
  28. }
  29. //初始化请求记录
  30. GReq = initServerLog(serverLogConfig{
  31. commonLogConfig: logCommon,
  32. ServerErrorStack: gcfg.Instance().MustGet(ctx, "system.log.serverErrorStack", false).Bool(),
  33. ServerErrorLogEnabled: gcfg.Instance().MustGet(ctx, "system.log.serverErrorLogEnabled", false).Bool(),
  34. ServerErrorLogPattern: gcfg.Instance().MustGet(ctx, "system.log.serverErrorLogPattern", "system-{Ymd}.log").String(),
  35. ServerAccessLogEnabled: gcfg.Instance().MustGet(ctx, "system.log.serverAccessLogEnabled", false).Bool(),
  36. ServerAccessLogPattern: gcfg.Instance().MustGet(ctx, "system.log.serverAccessLogPattern", "system-{Ymd}.log").String(),
  37. ServerRequestTimeout: gcfg.Instance().MustGet(ctx, "system.log.serverRequestTimeout", 500).Int64(),
  38. }, noticeConfig)
  39. }