logger.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. log.Printf("%s\n", err.Error())
  46. if RollBarEnabled && !RollBarSkipErrors.contain(err.Error()) {
  47. rollbar.Error(err)
  48. }
  49. }
  50. func LogRequests(handler http.Handler) http.Handler {
  51. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  52. start := time.Now()
  53. nw := &ResponseWriter{
  54. ResponseWriter: w,
  55. Content: []byte{},
  56. Size: 0,
  57. Status: http.StatusOK,
  58. }
  59. handler.ServeHTTP(nw, r)
  60. ua := strings.TrimSpace(r.Header.Get("User-Agent"))
  61. if ua == "" || len(ua) > 256 {
  62. ua = "-"
  63. }
  64. msg := fmt.Sprintf(
  65. "\"%s\" \"%s %s\" %d %d \"%.3f ms\" \"%s\"\n",
  66. strings.Join(helpers.ClientIPs(r), ", "),
  67. r.Method,
  68. r.URL,
  69. nw.Status,
  70. nw.Size,
  71. time.Since(start).Seconds(),
  72. ua,
  73. )
  74. log.Printf("%s", msg)
  75. if nw.Status < 400 {
  76. if AccessLogFile != "" {
  77. if err := appendToLogFile(AccessLogFile, start.Format("2006/01/02 15:04:05")+" "+msg); err != nil {
  78. log.Printf("%s\n", err.Error())
  79. }
  80. }
  81. } else {
  82. if ErrorLogFile != "" {
  83. if err := appendToLogFile(ErrorLogFile, start.Format("2006/01/02 15:04:05")+" "+msg); err != nil {
  84. log.Printf("%s\n", err.Error())
  85. }
  86. }
  87. }
  88. if RollBarEnabled && !RollBarSkipStatusCodes.contain(nw.Status) {
  89. if !RollBarSkipErrors.contain(string(nw.Content)) {
  90. rollbar.Error(r, nw.Status, nw.Size, string(nw.Content))
  91. }
  92. }
  93. })
  94. }