You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

color.go 1.9KB

Rewrite logger system (#24726) ## ⚠️ Breaking The `log.<mode>.<logger>` style config has been dropped. If you used it, please check the new config manual & app.example.ini to make your instance output logs as expected. Although many legacy options still work, it's encouraged to upgrade to the new options. The SMTP logger is deleted because SMTP is not suitable to collect logs. If you have manually configured Gitea log options, please confirm the logger system works as expected after upgrading. ## Description Close #12082 and maybe more log-related issues, resolve some related FIXMEs in old code (which seems unfixable before) Just like rewriting queue #24505 : make code maintainable, clear legacy bugs, and add the ability to support more writers (eg: JSON, structured log) There is a new document (with examples): `logging-config.en-us.md` This PR is safer than the queue rewriting, because it's just for logging, it won't break other logic. ## The old problems The logging system is quite old and difficult to maintain: * Unclear concepts: Logger, NamedLogger, MultiChannelledLogger, SubLogger, EventLogger, WriterLogger etc * Some code is diffuclt to konw whether it is right: `log.DelNamedLogger("console")` vs `log.DelNamedLogger(log.DEFAULT)` vs `log.DelLogger("console")` * The old system heavily depends on ini config system, it's difficult to create new logger for different purpose, and it's very fragile. * The "color" trick is difficult to use and read, many colors are unnecessary, and in the future structured log could help * It's difficult to add other log formats, eg: JSON format * The log outputer doesn't have full control of its goroutine, it's difficult to make outputer have advanced behaviors * The logs could be lost in some cases: eg: no Fatal error when using CLI. * Config options are passed by JSON, which is quite fragile. * INI package makes the KEY in `[log]` section visible in `[log.sub1]` and `[log.sub1.subA]`, this behavior is quite fragile and would cause more unclear problems, and there is no strong requirement to support `log.<mode>.<logger>` syntax. ## The new design See `logger.go` for documents. ## Screenshot <details> ![image](https://github.com/go-gitea/gitea/assets/2114189/4462d713-ba39-41f5-bb08-de912e67e1ff) ![image](https://github.com/go-gitea/gitea/assets/2114189/b188035e-f691-428b-8b2d-ff7b2199b2f9) ![image](https://github.com/go-gitea/gitea/assets/2114189/132e9745-1c3b-4e00-9e0d-15eaea495dee) </details> ## TODO * [x] add some new tests * [x] fix some tests * [x] test some sub-commands (manually ....) --------- Co-authored-by: Jason Song <i@wolfogre.com> Co-authored-by: delvh <dev.lh@web.de> Co-authored-by: Giteabot <teabot@gitea.io>
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package log
  4. import (
  5. "fmt"
  6. "strconv"
  7. )
  8. const escape = "\033"
  9. // ColorAttribute defines a single SGR Code
  10. type ColorAttribute int
  11. // Base ColorAttributes
  12. const (
  13. Reset ColorAttribute = iota
  14. Bold
  15. Faint
  16. Italic
  17. Underline
  18. BlinkSlow
  19. BlinkRapid
  20. ReverseVideo
  21. Concealed
  22. CrossedOut
  23. )
  24. // Foreground text colors
  25. const (
  26. FgBlack ColorAttribute = iota + 30
  27. FgRed
  28. FgGreen
  29. FgYellow
  30. FgBlue
  31. FgMagenta
  32. FgCyan
  33. FgWhite
  34. )
  35. // Foreground Hi-Intensity text colors
  36. const (
  37. FgHiBlack ColorAttribute = iota + 90
  38. FgHiRed
  39. FgHiGreen
  40. FgHiYellow
  41. FgHiBlue
  42. FgHiMagenta
  43. FgHiCyan
  44. FgHiWhite
  45. )
  46. // Background text colors
  47. const (
  48. BgBlack ColorAttribute = iota + 40
  49. BgRed
  50. BgGreen
  51. BgYellow
  52. BgBlue
  53. BgMagenta
  54. BgCyan
  55. BgWhite
  56. )
  57. // Background Hi-Intensity text colors
  58. const (
  59. BgHiBlack ColorAttribute = iota + 100
  60. BgHiRed
  61. BgHiGreen
  62. BgHiYellow
  63. BgHiBlue
  64. BgHiMagenta
  65. BgHiCyan
  66. BgHiWhite
  67. )
  68. var (
  69. resetBytes = ColorBytes(Reset)
  70. fgCyanBytes = ColorBytes(FgCyan)
  71. fgGreenBytes = ColorBytes(FgGreen)
  72. )
  73. type ColoredValue struct {
  74. v any
  75. colors []ColorAttribute
  76. }
  77. func (c *ColoredValue) Format(f fmt.State, verb rune) {
  78. _, _ = f.Write(ColorBytes(c.colors...))
  79. s := fmt.Sprintf(fmt.FormatString(f, verb), c.v)
  80. _, _ = f.Write([]byte(s))
  81. _, _ = f.Write(resetBytes)
  82. }
  83. func NewColoredValue(v any, color ...ColorAttribute) *ColoredValue {
  84. return &ColoredValue{v: v, colors: color}
  85. }
  86. // ColorBytes converts a list of ColorAttributes to a byte array
  87. func ColorBytes(attrs ...ColorAttribute) []byte {
  88. bytes := make([]byte, 0, 20)
  89. bytes = append(bytes, escape[0], '[')
  90. if len(attrs) > 0 {
  91. bytes = append(bytes, strconv.Itoa(int(attrs[0]))...)
  92. for _, a := range attrs[1:] {
  93. bytes = append(bytes, ';')
  94. bytes = append(bytes, strconv.Itoa(int(a))...)
  95. }
  96. } else {
  97. bytes = append(bytes, strconv.Itoa(int(Bold))...)
  98. }
  99. bytes = append(bytes, 'm')
  100. return bytes
  101. }