glog_logger_handler.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package glog
  7. import (
  8. "bytes"
  9. "context"
  10. "time"
  11. )
  12. // Handler is function handler for custom logging content outputs.
  13. type Handler func(ctx context.Context, in *HandlerInput)
  14. // HandlerInput is the input parameter struct for logging Handler.
  15. type HandlerInput struct {
  16. Logger *Logger // Logger.
  17. Ctx context.Context // Context.
  18. Buffer *bytes.Buffer // Buffer for logging content outputs.
  19. Time time.Time // Logging time, which is the time that logging triggers.
  20. TimeFormat string // Formatted time string, like "2016-01-09 12:00:00".
  21. Color int // Using color, like COLOR_RED, COLOR_BLUE, etc.
  22. Level int // Using level, like LEVEL_INFO, LEVEL_ERRO, etc.
  23. LevelFormat string // Formatted level string, like "DEBU", "ERRO", etc.
  24. CallerFunc string // The source function name that calls logging.
  25. CallerPath string // The source file path and its line number that calls logging.
  26. CtxStr string // The retrieved context value string from context.
  27. Prefix string // Custom prefix string for logging content.
  28. Content string // Content is the main logging content that passed by you.
  29. IsAsync bool // IsAsync marks it is in asynchronous logging.
  30. handlerIndex int // Middleware handling index for internal usage.
  31. }
  32. // Next calls the next logging handler in middleware way.
  33. func (i *HandlerInput) Next() {
  34. if len(i.Logger.config.Handlers)-1 > i.handlerIndex {
  35. i.handlerIndex++
  36. i.Logger.config.Handlers[i.handlerIndex](i.Ctx, i)
  37. } else {
  38. defaultHandler(i.Ctx, i)
  39. }
  40. }
  41. // String returns the logging content formatted by default logging handler.
  42. func (i *HandlerInput) String(withColor ...bool) string {
  43. formatWithColor := false
  44. if len(withColor) > 0 {
  45. formatWithColor = withColor[0]
  46. }
  47. return i.getDefaultBuffer(formatWithColor).String()
  48. }
  49. func (i *HandlerInput) getDefaultBuffer(withColor bool) *bytes.Buffer {
  50. buffer := bytes.NewBuffer(nil)
  51. if i.TimeFormat != "" {
  52. buffer.WriteString(i.TimeFormat)
  53. }
  54. if i.LevelFormat != "" {
  55. if withColor {
  56. i.addStringToBuffer(buffer, i.Logger.getColoredStr(
  57. i.Logger.getColorByLevel(i.Level), i.LevelFormat,
  58. ))
  59. } else {
  60. i.addStringToBuffer(buffer, i.LevelFormat)
  61. }
  62. }
  63. if i.Prefix != "" {
  64. i.addStringToBuffer(buffer, i.Prefix)
  65. }
  66. if i.CtxStr != "" {
  67. i.addStringToBuffer(buffer, i.CtxStr)
  68. }
  69. if i.CallerFunc != "" {
  70. i.addStringToBuffer(buffer, i.CallerFunc)
  71. }
  72. if i.CallerPath != "" {
  73. i.addStringToBuffer(buffer, i.CallerPath)
  74. }
  75. if i.Content != "" {
  76. i.addStringToBuffer(buffer, i.Content)
  77. }
  78. i.addStringToBuffer(buffer, "\n")
  79. return buffer
  80. }
  81. func (i *HandlerInput) getRealBuffer(withColor bool) *bytes.Buffer {
  82. if i.Buffer.Len() > 0 {
  83. return i.Buffer
  84. }
  85. return i.getDefaultBuffer(withColor)
  86. }
  87. // defaultHandler is the default handler for logger.
  88. func defaultHandler(ctx context.Context, in *HandlerInput) {
  89. buffer := in.Logger.doDefaultPrint(ctx, in)
  90. if in.Buffer.Len() == 0 {
  91. in.Buffer = buffer
  92. }
  93. }
  94. func (i *HandlerInput) addStringToBuffer(buffer *bytes.Buffer, strings ...string) {
  95. for _, s := range strings {
  96. if buffer.Len() > 0 {
  97. buffer.WriteByte(' ')
  98. }
  99. buffer.WriteString(s)
  100. }
  101. }