struct.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package logs
  2. import (
  3. "github.com/gogf/gf/v2/os/glog"
  4. )
  5. type commonLogConfig struct {
  6. Path string //系统日志默认文件默认报错路径。默认为./logs
  7. Debug bool //是否打印调试信息。默认false
  8. Stdout bool //是否输出到控制台。默认false
  9. }
  10. func initBaseLog(c *commonLogConfig, fileName string) (l *glog.Logger) {
  11. l = glog.New()
  12. _ = l.SetPath(c.Path)
  13. l.SetFile(fileName)
  14. l.SetDebug(c.Debug)
  15. l.SetStdoutPrint(c.Stdout)
  16. return
  17. }
  18. // 请求记录配置
  19. type serverLogConfig struct {
  20. *commonLogConfig
  21. ServerErrorStack bool //当Server捕获到异常时是否记录堆栈信息到日志中。默认为true
  22. ServerRequestLogSaveDb bool //请求日志是否保存至数据库。默认false
  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. }
  34. func initServerLog(sc serverLogConfig) *serverLog {
  35. accessLog, errorLog := glog.New(), glog.New()
  36. _ = accessLog.SetPath(sc.Path)
  37. _ = errorLog.SetPath(sc.Path)
  38. accessLog.SetFile(sc.ServerAccessLogPattern)
  39. errorLog.SetFile(sc.ServerErrorLogPattern)
  40. accessLog.SetDebug(sc.Debug)
  41. errorLog.SetDebug(sc.Debug)
  42. accessLog.SetStdoutPrint(sc.Stdout)
  43. errorLog.SetStdoutPrint(sc.Stdout)
  44. slog := &serverLog{
  45. accessLog: accessLog,
  46. errorLog: errorLog,
  47. Config: sc,
  48. }
  49. return slog
  50. }