serverLogs.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package logs
  2. import (
  3. "github.com/gogf/gf/v2/os/glog"
  4. "log"
  5. )
  6. type commonLogConfig struct {
  7. Path string //系统日志默认文件默认报错路径。默认为./logs
  8. Debug bool //是否打印调试信息。默认false
  9. Stdout bool //是否输出到控制台。默认false
  10. }
  11. func initBaseLog(c *commonLogConfig, fileName string) (l *glog.Logger) {
  12. l = glog.New()
  13. _ = l.SetPath(c.Path)
  14. l.SetFile(fileName)
  15. l.SetDebug(c.Debug)
  16. l.SetStdoutPrint(c.Stdout)
  17. return
  18. }
  19. // 请求记录配置
  20. type serverLogConfig struct {
  21. *commonLogConfig
  22. ServerErrorStack bool //当Server捕获到异常时是否记录堆栈信息到日志中。默认为true
  23. ServerErrorLogEnabled bool //是否记录访问异常日志到日志中。默认为true
  24. ServerErrorLogPattern string //访问异常日志文件格式。默认为"error-{Ymd}.log"
  25. ServerAccessLogEnabled bool //是否记录访问日志。默认为false
  26. ServerAccessLogPattern string //记录访问日志文件格式。默认为"access-{Ymd}.log"
  27. ServerRequestTimeout int64
  28. }
  29. // server 请求日志
  30. type serverLog struct {
  31. accessLog, errorLog *glog.Logger
  32. Config serverLogConfig
  33. Notice *Notice
  34. }
  35. func initServerLog(sc serverLogConfig, nc NoticeConfig) *serverLog {
  36. accessLog, errorLog := glog.New(), glog.New()
  37. _ = accessLog.SetPath(sc.Path)
  38. _ = errorLog.SetPath(sc.Path)
  39. accessLog.SetFile(sc.ServerAccessLogPattern)
  40. errorLog.SetFile(sc.ServerErrorLogPattern)
  41. accessLog.SetDebug(sc.Debug)
  42. errorLog.SetDebug(sc.Debug)
  43. accessLog.SetStdoutPrint(sc.Stdout)
  44. errorLog.SetStdoutPrint(sc.Stdout)
  45. notice, err := newNotice(nc)
  46. if err != nil {
  47. log.Println("GNotice nsq通知初始化异常", err)
  48. }
  49. return &serverLog{
  50. accessLog: accessLog,
  51. errorLog: errorLog,
  52. Config: sc,
  53. Notice: notice,
  54. }
  55. }