logger.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package logger
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "os"
  7. "strings"
  8. "time"
  9. "github.com/rollbar/rollbar-go"
  10. "github.com/vladimirok5959/golang-utils/utils/http/helpers"
  11. )
  12. var AccessLogFile = ""
  13. var ErrorLogFile = ""
  14. func appendToLogFile(fileName, msg string) error {
  15. flags := os.O_RDWR | os.O_CREATE | os.O_APPEND
  16. f, err := os.OpenFile(fileName, flags, 0666)
  17. if err != nil {
  18. return err
  19. }
  20. defer f.Close()
  21. if _, err := fmt.Fprint(f, msg); err != nil {
  22. return err
  23. }
  24. return nil
  25. }
  26. func LogError(format string, a ...any) {
  27. msg := fmt.Sprintf(format, a...)
  28. log.Printf("%s", msg)
  29. if ErrorLogFile != "" {
  30. if err := appendToLogFile(ErrorLogFile, time.Now().Format("2006/01/02 15:04:05")+" "+msg); err != nil {
  31. log.Printf("%s\n", err.Error())
  32. }
  33. }
  34. }
  35. func LogInfo(format string, a ...any) {
  36. msg := fmt.Sprintf(format, a...)
  37. log.Printf("%s", msg)
  38. if AccessLogFile != "" {
  39. if err := appendToLogFile(AccessLogFile, time.Now().Format("2006/01/02 15:04:05")+" "+msg); err != nil {
  40. log.Printf("%s\n", err.Error())
  41. }
  42. }
  43. }
  44. func LogInternalError(err error) {
  45. msg := fmt.Sprintf("%s\n", err.Error())
  46. log.Printf("%s", msg)
  47. if ErrorLogFile != "" {
  48. if err := appendToLogFile(ErrorLogFile, time.Now().Format("2006/01/02 15:04:05")+" "+msg); err != nil {
  49. log.Printf("%s\n", err.Error())
  50. }
  51. }
  52. if RollBarEnabled && !RollBarSkipErrors.contain(err.Error()) {
  53. rollbar.Error(err)
  54. }
  55. }
  56. func LogRequests(handler http.Handler) http.Handler {
  57. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  58. start := time.Now()
  59. nw := &ResponseWriter{
  60. ResponseWriter: w,
  61. Content: []byte{},
  62. Size: 0,
  63. Status: http.StatusOK,
  64. }
  65. handler.ServeHTTP(nw, r)
  66. ua := strings.TrimSpace(r.Header.Get("User-Agent"))
  67. if ua == "" || len(ua) > 256 {
  68. ua = "-"
  69. }
  70. msg := fmt.Sprintf(
  71. "\"%s\" \"%s %s\" %d %d \"%.3f ms\" \"%s\"\n",
  72. strings.Join(helpers.ClientIPs(r), ", "),
  73. r.Method,
  74. r.URL,
  75. nw.Status,
  76. nw.Size,
  77. time.Since(start).Seconds(),
  78. ua,
  79. )
  80. log.Printf("%s", msg)
  81. if nw.Status < 400 {
  82. if AccessLogFile != "" {
  83. if err := appendToLogFile(AccessLogFile, start.Format("2006/01/02 15:04:05")+" "+msg); err != nil {
  84. log.Printf("%s\n", err.Error())
  85. }
  86. }
  87. } else {
  88. if ErrorLogFile != "" {
  89. if err := appendToLogFile(ErrorLogFile, start.Format("2006/01/02 15:04:05")+" "+msg); err != nil {
  90. log.Printf("%s\n", err.Error())
  91. }
  92. }
  93. }
  94. if RollBarEnabled && !RollBarSkipStatusCodes.contain(nw.Status) {
  95. if !RollBarSkipErrors.contain(string(nw.Content)) {
  96. rollbar.Error(r, nw.Status, nw.Size, string(nw.Content))
  97. }
  98. }
  99. })
  100. }