level.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package golog
  2. import (
  3. "strings"
  4. "github.com/kataras/pio"
  5. )
  6. // Level is a number which defines the log level.
  7. type Level uint32
  8. // The available built'n log levels, users can add or modify a level via `Levels` field.
  9. const (
  10. // DisableLevel will disable the printer.
  11. DisableLevel Level = iota
  12. // FatalLevel will `os.Exit(1)` no matter the level of the logger.
  13. // If the logger's level is fatal, error, warn, info or debug
  14. // then it will print the log message too.
  15. FatalLevel
  16. // ErrorLevel will print only errors.
  17. ErrorLevel
  18. // WarnLevel will print errors and warnings.
  19. WarnLevel
  20. // InfoLevel will print errors, warnings and infos.
  21. InfoLevel
  22. // DebugLevel will print on any level, fatals, errors, warnings, infos and debug logs.
  23. DebugLevel
  24. )
  25. // Levels contains the levels and their
  26. // mapped (pointer of, in order to be able to be modified) metadata, callers
  27. // are allowed to modify this package-level global variable
  28. // without any loses.
  29. var Levels = map[Level]*LevelMetadata{
  30. DisableLevel: {
  31. Name: "disable",
  32. AlternativeNames: []string{"disabled"},
  33. RawText: "",
  34. ColorfulText: "",
  35. },
  36. FatalLevel: {
  37. Name: "fatal",
  38. RawText: "[FTAL]",
  39. // white foreground but red background, it's nice
  40. ColorfulText: pio.RedBackground("[FTAL]"),
  41. },
  42. ErrorLevel: {
  43. Name: "error",
  44. RawText: "[ERRO]",
  45. ColorfulText: pio.Red("[ERRO]"),
  46. },
  47. WarnLevel: {
  48. Name: "warn",
  49. AlternativeNames: []string{"warning"},
  50. RawText: "[WARN]",
  51. ColorfulText: pio.Purple("[WARN]"),
  52. },
  53. InfoLevel: {
  54. Name: "info",
  55. RawText: "[INFO]",
  56. ColorfulText: pio.LightGreen("[INFO]"),
  57. },
  58. DebugLevel: {
  59. Name: "debug",
  60. RawText: "[DBUG]",
  61. ColorfulText: pio.Yellow("[DBUG]"),
  62. },
  63. }
  64. func fromLevelName(levelName string) Level {
  65. for level, meta := range Levels {
  66. if meta.Name == levelName {
  67. return level
  68. }
  69. for _, altName := range meta.AlternativeNames {
  70. if altName == levelName {
  71. return level
  72. }
  73. }
  74. }
  75. return DisableLevel
  76. }
  77. // LevelMetadata describes the information
  78. // behind a log Level, each level has its own unique metadata.
  79. type LevelMetadata struct {
  80. // The Name of the Level
  81. // that named (lowercased) will be used
  82. // to convert a string level on `SetLevel`
  83. // to the correct Level type.
  84. Name string
  85. // AlternativeNames are the names that can be referred to this specific log level.
  86. // i.e Name = "warn"
  87. // AlternativeNames = []string{"warning"}, it's an optional field,
  88. // therefore we keep Name as a simple string and created this new field.
  89. AlternativeNames []string
  90. // Tha RawText will be the prefix of the log level
  91. // when output doesn't supports colors.
  92. //
  93. // When RawText is changed its ColorfulText is also changed
  94. // to a default color, but callers are able to change it too.
  95. RawText string
  96. // The ColorfulText will be the prefix of the log level
  97. // when output supports colors, almost everything except
  98. // os files and putty-based terminals(?).
  99. //
  100. // If ColorfulText is empty then built'n colors
  101. // are being used to wrap the "RawText".
  102. ColorfulText string
  103. }
  104. // Text returns the text that should be
  105. // prepended to the log message when a specific
  106. // log level is being written.
  107. func (m *LevelMetadata) Text(enableColor bool) string {
  108. if enableColor {
  109. return m.ColorfulText
  110. }
  111. return m.RawText
  112. }
  113. // SetText can modify the prefix that will be prepended
  114. // to the output message log when `Error/Errorf` functions are being used.
  115. //
  116. // If "newRawText" is empty then it will just skip the Text set-ing.
  117. // If "newColorfulText" is empty then it will update the text color version using
  118. // the default values by using the new raw text.
  119. func (m *LevelMetadata) SetText(newRawText string, newColorfulText string) {
  120. if newRawText != "" {
  121. oldRawText := m.RawText
  122. m.RawText = newRawText
  123. m.ColorfulText = strings.Replace(m.ColorfulText, oldRawText, newRawText, -1)
  124. }
  125. if newColorfulText != "" {
  126. m.ColorfulText = newColorfulText
  127. }
  128. }
  129. var (
  130. // ErrorText can modify the prefix that will be prepended
  131. // to the output message log when `Error/Errorf` functions are being used.
  132. //
  133. // If "newColorfulText" is empty then it will update the text color version using
  134. // the default values by using the new raw text.
  135. //
  136. // Defaults to "[ERRO]" and pio.Red("[ERRO]").
  137. //
  138. // Deprecated Use `Levels[ErrorLevel].SetText(string, string)` instead.
  139. ErrorText = Levels[ErrorLevel].SetText
  140. // WarnText can modify the prefix that will be prepended
  141. // to the output message log when `Warn/Warnf` functions are being used.
  142. //
  143. // If "newColorfulText" is empty then it will update the text color version using
  144. // the default values by using the new raw text.
  145. //
  146. // Defaults to "[WARN]" and pio.Purple("[WARN]").
  147. //
  148. // Deprecated Use `Levels[WarnLevel].SetText(string, string)` instead.
  149. WarnText = Levels[WarnLevel].SetText
  150. // InfoText can modify the prefix that will be prepended
  151. // to the output message log when `Info/Infof` functions are being used.
  152. //
  153. // If "newColorfulText" is empty then it will update the text color version using
  154. // the default values by using the new raw text.
  155. //
  156. // Defaults to "[INFO]" and pio.LightGreen("[INFO]").
  157. //
  158. // Deprecated Use `Levels[InfoLevel].SetText(string, string)` instead.
  159. InfoText = Levels[InfoLevel].SetText
  160. // DebugText can modify the prefix that will be prepended
  161. // to the output message log when `Info/Infof` functions are being used.
  162. //
  163. // If "newColorfulText" is empty then it will update the text color version using
  164. // the default values by using the new raw text.
  165. //
  166. // Defaults to "[DBUG]" and pio.Yellow("[DBUG]").
  167. //
  168. // Deprecated Use `Levels[DebugLevel].SetText(string, string)` instead.
  169. DebugText = Levels[DebugLevel].SetText
  170. // GetTextForLevel is the function which
  171. // has the "final" responsibility to generate the text (colorful or not)
  172. // that is prepended to the leveled log message
  173. // when `Error/Errorf, Warn/Warnf, Info/Infof or Debug/Debugf`
  174. // functions are being called.
  175. //
  176. // It can be used to override the default behavior, at the start-up state.
  177. GetTextForLevel = func(level Level, enableColor bool) string {
  178. if meta, ok := Levels[level]; ok {
  179. return meta.Text(enableColor)
  180. }
  181. return ""
  182. }
  183. )