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.

level.go 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package log
  5. import (
  6. "bytes"
  7. "fmt"
  8. "os"
  9. "strings"
  10. jsoniter "github.com/json-iterator/go"
  11. )
  12. // Level is the level of the logger
  13. type Level int
  14. const (
  15. // TRACE represents the lowest log level
  16. TRACE Level = iota
  17. // DEBUG is for debug logging
  18. DEBUG
  19. // INFO is for information
  20. INFO
  21. // WARN is for warning information
  22. WARN
  23. // ERROR is for error reporting
  24. ERROR
  25. // CRITICAL is for critical errors
  26. CRITICAL
  27. // FATAL is for fatal errors
  28. FATAL
  29. // NONE is for no logging
  30. NONE
  31. )
  32. var toString = map[Level]string{
  33. TRACE: "trace",
  34. DEBUG: "debug",
  35. INFO: "info",
  36. WARN: "warn",
  37. ERROR: "error",
  38. CRITICAL: "critical",
  39. FATAL: "fatal",
  40. NONE: "none",
  41. }
  42. var toLevel = map[string]Level{
  43. "trace": TRACE,
  44. "debug": DEBUG,
  45. "info": INFO,
  46. "warn": WARN,
  47. "error": ERROR,
  48. "critical": CRITICAL,
  49. "fatal": FATAL,
  50. "none": NONE,
  51. }
  52. // Levels returns all the possible logging levels
  53. func Levels() []string {
  54. keys := make([]string, 0)
  55. for key := range toLevel {
  56. keys = append(keys, key)
  57. }
  58. return keys
  59. }
  60. func (l Level) String() string {
  61. s, ok := toString[l]
  62. if ok {
  63. return s
  64. }
  65. return "info"
  66. }
  67. // Color returns the color string for this Level
  68. func (l Level) Color() *[]byte {
  69. color, ok := levelToColor[l]
  70. if ok {
  71. return &(color)
  72. }
  73. none := levelToColor[NONE]
  74. return &none
  75. }
  76. // MarshalJSON takes a Level and turns it into text
  77. func (l Level) MarshalJSON() ([]byte, error) {
  78. buffer := bytes.NewBufferString(`"`)
  79. buffer.WriteString(toString[l])
  80. buffer.WriteString(`"`)
  81. return buffer.Bytes(), nil
  82. }
  83. // FromString takes a level string and returns a Level
  84. func FromString(level string) Level {
  85. temp, ok := toLevel[strings.ToLower(level)]
  86. if !ok {
  87. return INFO
  88. }
  89. return temp
  90. }
  91. // UnmarshalJSON takes text and turns it into a Level
  92. func (l *Level) UnmarshalJSON(b []byte) error {
  93. var tmp interface{}
  94. json := jsoniter.ConfigCompatibleWithStandardLibrary
  95. err := json.Unmarshal(b, &tmp)
  96. if err != nil {
  97. fmt.Fprintf(os.Stderr, "Err: %v", err)
  98. return err
  99. }
  100. switch v := tmp.(type) {
  101. case string:
  102. *l = FromString(v)
  103. case int:
  104. *l = FromString(Level(v).String())
  105. default:
  106. *l = INFO
  107. }
  108. return nil
  109. }