handler.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package log
  2. import (
  3. "context"
  4. "time"
  5. pkgerr "github.com/pkg/errors"
  6. )
  7. const (
  8. _timeFormat = "2006-01-02T15:04:05.999999"
  9. // log level defined in level.go.
  10. _levelValue = "level_value"
  11. // log level name: INFO, WARN...
  12. _level = "level"
  13. // log time.
  14. _time = "time"
  15. // request path.
  16. // _title = "title"
  17. // log file.
  18. _source = "source"
  19. // common log filed.
  20. _log = "log"
  21. // app name.
  22. _appID = "app_id"
  23. // container ID.
  24. _instanceID = "instance_id"
  25. // uniq ID from trace.
  26. _tid = "traceid"
  27. // request time.
  28. // _ts = "ts"
  29. // requester.
  30. _caller = "caller"
  31. // container environment: prod, pre, uat, fat.
  32. _deplyEnv = "env"
  33. // container area.
  34. _zone = "zone"
  35. // mirror flag
  36. _mirror = "mirror"
  37. // color.
  38. _color = "color"
  39. // cluster.
  40. _cluster = "cluster"
  41. )
  42. // Handler is used to handle log events, outputting them to
  43. // stdio or sending them to remote services. See the "handlers"
  44. // directory for implementations.
  45. //
  46. // It is left up to Handlers to implement thread-safety.
  47. type Handler interface {
  48. // Log handle log
  49. // variadic D is k-v struct represent log content
  50. Log(context.Context, Level, ...D)
  51. // SetFormat set render format on log output
  52. // see StdoutHandler.SetFormat for detail
  53. SetFormat(string)
  54. // Close handler
  55. Close() error
  56. }
  57. func newHandlers(filters []string, handlers ...Handler) *Handlers {
  58. set := make(map[string]struct{})
  59. for _, k := range filters {
  60. set[k] = struct{}{}
  61. }
  62. return &Handlers{filters: set, handlers: handlers}
  63. }
  64. // Handlers a bundle for hander with filter function.
  65. type Handlers struct {
  66. filters map[string]struct{}
  67. handlers []Handler
  68. }
  69. // Log handlers logging.
  70. func (hs Handlers) Log(c context.Context, lv Level, d ...D) {
  71. var fn string
  72. for i := range d {
  73. if _, ok := hs.filters[d[i].Key]; ok {
  74. d[i].Value = "***"
  75. }
  76. if d[i].Key == _source {
  77. fn = d[i].Value.(string)
  78. }
  79. }
  80. if fn == "" {
  81. d = append(d, KV(_source, funcName(4)))
  82. }
  83. d = append(d, KV(_time, time.Now()), KV(_levelValue, int(lv)), KV(_level, lv.String()))
  84. errIncr(lv, fn)
  85. for _, h := range hs.handlers {
  86. h.Log(c, lv, d...)
  87. }
  88. }
  89. // Close close resource.
  90. func (hs Handlers) Close() (err error) {
  91. for _, h := range hs.handlers {
  92. if e := h.Close(); e != nil {
  93. err = pkgerr.WithStack(e)
  94. }
  95. }
  96. return
  97. }
  98. // SetFormat .
  99. func (hs Handlers) SetFormat(format string) {
  100. for _, h := range hs.handlers {
  101. h.SetFormat(format)
  102. }
  103. }