config.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package logger
  2. import (
  3. "time"
  4. "github.com/kataras/iris/context"
  5. )
  6. // The SkipperFunc signature, used to serve the main request without logs.
  7. // See `Configuration` too.
  8. type SkipperFunc func(ctx context.Context) bool
  9. // Config contains the options for the logger middleware
  10. // can be optionally be passed to the `New`.
  11. type Config struct {
  12. // Status displays status code (bool).
  13. //
  14. // Defaults to true.
  15. Status bool
  16. // IP displays request's remote address (bool).
  17. //
  18. // Defaults to true.
  19. IP bool
  20. // Method displays the http method (bool).
  21. //
  22. // Defaults to true.
  23. Method bool
  24. // Path displays the request path (bool).
  25. //
  26. // Defaults to true.
  27. Path bool
  28. // Query will append the URL Query to the Path.
  29. // Path should be true too.
  30. //
  31. // Defaults to false.
  32. Query bool
  33. // Columns will display the logs as a formatted columns-rows text (bool).
  34. // If custom `LogFunc` has been provided then this field is useless and users should
  35. // use the `Columinize` function of the logger to get the output result as columns.
  36. //
  37. // Defaults to false.
  38. Columns bool
  39. // MessageContextKeys if not empty,
  40. // the middleware will try to fetch
  41. // the contents with `ctx.Values().Get(MessageContextKey)`
  42. // and if available then these contents will be
  43. // appended as part of the logs (with `%v`, in order to be able to set a struct too),
  44. // if Columns field was setted to true then
  45. // a new column will be added named 'Message'.
  46. //
  47. // Defaults to empty.
  48. MessageContextKeys []string
  49. // MessageHeaderKeys if not empty,
  50. // the middleware will try to fetch
  51. // the contents with `ctx.Values().Get(MessageHeaderKey)`
  52. // and if available then these contents will be
  53. // appended as part of the logs (with `%v`, in order to be able to set a struct too),
  54. // if Columns field was setted to true then
  55. // a new column will be added named 'HeaderMessage'.
  56. //
  57. // Defaults to empty.
  58. MessageHeaderKeys []string
  59. // LogFunc is the writer which logs are written to,
  60. // if missing the logger middleware uses the app.Logger().Infof instead.
  61. // Note that message argument can be empty.
  62. LogFunc func(now time.Time, latency time.Duration, status, ip, method, path string, message interface{}, headerMessage interface{})
  63. // Skippers used to skip the logging i.e by `ctx.Path()` and serve
  64. // the next/main handler immediately.
  65. Skippers []SkipperFunc
  66. // the Skippers as one function in order to reduce the time needed to
  67. // combine them at serve time.
  68. skip SkipperFunc
  69. }
  70. // DefaultConfig returns a default config
  71. // that have all boolean fields to true except `Columns`,
  72. // all strings are empty,
  73. // LogFunc and Skippers to nil as well.
  74. func DefaultConfig() Config {
  75. return Config{
  76. Status: true,
  77. IP: true,
  78. Method: true,
  79. Path: true,
  80. Query: false,
  81. Columns: false,
  82. LogFunc: nil,
  83. Skippers: nil,
  84. skip: nil,
  85. }
  86. }
  87. // AddSkipper adds a skipper to the configuration.
  88. func (c *Config) AddSkipper(sk SkipperFunc) {
  89. c.Skippers = append(c.Skippers, sk)
  90. c.buildSkipper()
  91. }
  92. func (c *Config) buildSkipper() {
  93. if len(c.Skippers) == 0 {
  94. return
  95. }
  96. skippersLocked := c.Skippers[0:]
  97. c.skip = func(ctx context.Context) bool {
  98. for _, s := range skippersLocked {
  99. if s(ctx) {
  100. return true
  101. }
  102. }
  103. return false
  104. }
  105. }