logs.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. GReq = initServerLog(serverLogConfig{
  26. commonLogConfig: logCommon,
  27. ServerErrorStack: gcfg.Instance().MustGet(ctx, "system.log.serverErrorStack", false).Bool(),
  28. ServerRequestLogSaveDb: gcfg.Instance().MustGet(ctx, "system.log.serverRequestLogSaveDb", false).Bool(),
  29. ServerErrorLogEnabled: gcfg.Instance().MustGet(ctx, "system.log.serverErrorLogEnabled", false).Bool(),
  30. ServerErrorLogPattern: gcfg.Instance().MustGet(ctx, "system.log.serverErrorLogPattern", "system-{Ymd}.log").String(),
  31. ServerAccessLogEnabled: gcfg.Instance().MustGet(ctx, "system.log.serverAccessLogEnabled", false).Bool(),
  32. ServerAccessLogPattern: gcfg.Instance().MustGet(ctx, "system.log.serverAccessLogPattern", "system-{Ymd}.log").String(),
  33. ServerRequestTimeout: gcfg.Instance().MustGet(ctx, "system.log.serverRequestTimeout", 500).Int64(),
  34. })
  35. }