logger.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package negroni
  2. import (
  3. "bytes"
  4. "log"
  5. "net/http"
  6. "os"
  7. "text/template"
  8. "time"
  9. )
  10. // LoggerEntry is the structure passed to the template.
  11. type LoggerEntry struct {
  12. StartTime string
  13. Status int
  14. Duration time.Duration
  15. Hostname string
  16. Method string
  17. Path string
  18. Request *http.Request
  19. }
  20. // LoggerDefaultFormat is the format logged used by the default Logger instance.
  21. var LoggerDefaultFormat = "{{.StartTime}} | {{.Status}} | \t {{.Duration}} | {{.Hostname}} | {{.Method}} {{.Path}}"
  22. // LoggerDefaultDateFormat is the format used for date by the default Logger instance.
  23. var LoggerDefaultDateFormat = time.RFC3339
  24. // ALogger interface
  25. type ALogger interface {
  26. Println(v ...interface{})
  27. Printf(format string, v ...interface{})
  28. }
  29. // Logger is a middleware handler that logs the request as it goes in and the response as it goes out.
  30. type Logger struct {
  31. // ALogger implements just enough log.Logger interface to be compatible with other implementations
  32. ALogger
  33. dateFormat string
  34. template *template.Template
  35. }
  36. // NewLogger returns a new Logger instance
  37. func NewLogger() *Logger {
  38. logger := &Logger{ALogger: log.New(os.Stdout, "[negroni] ", 0), dateFormat: LoggerDefaultDateFormat}
  39. logger.SetFormat(LoggerDefaultFormat)
  40. return logger
  41. }
  42. func (l *Logger) SetFormat(format string) {
  43. l.template = template.Must(template.New("negroni_parser").Parse(format))
  44. }
  45. func (l *Logger) SetDateFormat(format string) {
  46. l.dateFormat = format
  47. }
  48. func (l *Logger) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
  49. start := time.Now()
  50. next(rw, r)
  51. res := rw.(ResponseWriter)
  52. log := LoggerEntry{
  53. StartTime: start.Format(l.dateFormat),
  54. Status: res.Status(),
  55. Duration: time.Since(start),
  56. Hostname: r.Host,
  57. Method: r.Method,
  58. Path: r.URL.Path,
  59. Request: r,
  60. }
  61. buff := &bytes.Buffer{}
  62. l.template.Execute(buff, log)
  63. l.Println(buff.String())
  64. }