config.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 set 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 set 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(endTime time.Time, latency time.Duration, status, ip, method, path string, message interface{}, headerMessage interface{})
  63. // LogFuncCtx can be used instead of `LogFunc` if handlers need to customize the output based on
  64. // custom request-time information that the LogFunc isn't aware of.
  65. LogFuncCtx func(ctx *context.Context, latency time.Duration)
  66. // Skippers used to skip the logging i.e by `ctx.Path()` and serve
  67. // the next/main handler immediately.
  68. Skippers []SkipperFunc
  69. // the Skippers as one function in order to reduce the time needed to
  70. // combine them at serve time.
  71. skip SkipperFunc
  72. }
  73. // DefaultConfig returns a default config
  74. // that have all boolean fields to true except `Columns`,
  75. // all strings are empty,
  76. // LogFunc and Skippers to nil as well.
  77. func DefaultConfig() Config {
  78. return Config{
  79. Status: true,
  80. IP: true,
  81. Method: true,
  82. Path: true,
  83. Query: false,
  84. Columns: false,
  85. LogFunc: nil,
  86. LogFuncCtx: nil,
  87. Skippers: nil,
  88. skip: nil,
  89. }
  90. }
  91. // AddSkipper adds a skipper to the configuration.
  92. func (c *Config) AddSkipper(sk SkipperFunc) {
  93. c.Skippers = append(c.Skippers, sk)
  94. c.buildSkipper()
  95. }
  96. func (c *Config) buildSkipper() {
  97. if len(c.Skippers) == 0 {
  98. return
  99. }
  100. skippersLocked := c.Skippers[0:]
  101. c.skip = func(ctx *context.Context) bool {
  102. for _, s := range skippersLocked {
  103. if s(ctx) {
  104. return true
  105. }
  106. }
  107. return false
  108. }
  109. }