main.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "os"
  7. "time"
  8. "github.com/sirupsen/logrus"
  9. "go.uber.org/zap"
  10. )
  11. func mainPage(w http.ResponseWriter, r *http.Request) {
  12. fmt.Fprintln(w, "hello world")
  13. }
  14. type AccessLogger struct {
  15. StdLogger *log.Logger
  16. ZapLogger *zap.SugaredLogger
  17. LogrusLogger *logrus.Entry
  18. }
  19. func (ac *AccessLogger) accessLogMiddleware(next http.Handler) http.Handler {
  20. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  21. start := time.Now()
  22. next.ServeHTTP(w, r)
  23. fmt.Printf("FMT [%s] %s, %s %s\n",
  24. r.Method, r.RemoteAddr, r.URL.Path, time.Since(start))
  25. log.Printf("LOG [%s] %s, %s %s\n",
  26. r.Method, r.RemoteAddr, r.URL.Path, time.Since(start))
  27. ac.StdLogger.Printf("[%s] %s, %s %s\n",
  28. r.Method, r.RemoteAddr, r.URL.Path, time.Since(start))
  29. ac.ZapLogger.Info(r.URL.Path,
  30. zap.String("method", r.Method),
  31. zap.String("remote_addr", r.RemoteAddr),
  32. zap.String("url", r.URL.Path),
  33. zap.Duration("work_time", time.Since(start)),
  34. )
  35. ac.LogrusLogger.WithFields(logrus.Fields{
  36. "method": r.Method,
  37. "remote_addr": r.RemoteAddr,
  38. "work_time": time.Since(start),
  39. }).Info(r.URL.Path)
  40. })
  41. }
  42. // -----------
  43. func main() {
  44. addr := "localhost"
  45. port := 8080
  46. // std
  47. fmt.Printf("STD starting server at %s:%d", addr, port)
  48. // std
  49. log.Printf("STD starting server at %s:%d", addr, port)
  50. // zap
  51. // у zap-а нет логгера по-умолчанию
  52. zapLogger, _ := zap.NewProduction()
  53. defer zapLogger.Sync()
  54. zapLogger.Info("starting server",
  55. zap.String("logger", "ZAP"),
  56. zap.String("host", addr),
  57. zap.Int("port", port),
  58. )
  59. // logrus
  60. logrus.SetFormatter(&logrus.TextFormatter{DisableColors: true})
  61. logrus.WithFields(logrus.Fields{
  62. "logger": "LOGRUS",
  63. "host": addr,
  64. "port": port,
  65. }).Info("Starting server")
  66. AccessLogOut := new(AccessLogger)
  67. // std
  68. AccessLogOut.StdLogger = log.New(os.Stdout, "STD ", log.LUTC|log.Lshortfile)
  69. // zap
  70. sugar := zapLogger.Sugar().With(
  71. zap.String("mode", "[access_log]"),
  72. zap.String("logger", "ZAP"),
  73. )
  74. AccessLogOut.ZapLogger = sugar
  75. // logrus
  76. contextLogger := logrus.WithFields(logrus.Fields{
  77. "mode": "[access_log]",
  78. "logger": "LOGRUS",
  79. })
  80. logrus.SetFormatter(&logrus.JSONFormatter{})
  81. AccessLogOut.LogrusLogger = contextLogger
  82. // server stuff
  83. siteMux := http.NewServeMux()
  84. siteMux.HandleFunc("/", mainPage)
  85. siteHandler := AccessLogOut.accessLogMiddleware(siteMux)
  86. http.ListenAndServe(":8080", siteHandler)
  87. }