level.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright (C) 2014 Space Monkey, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package spacelog
  15. import (
  16. "fmt"
  17. "strconv"
  18. "strings"
  19. )
  20. type LogLevel int32
  21. const (
  22. Trace LogLevel = 5
  23. Debug LogLevel = 10
  24. Info LogLevel = 20
  25. Notice LogLevel = 30
  26. Warning LogLevel = 40
  27. Error LogLevel = 50
  28. Critical LogLevel = 60
  29. // syslog has Alert
  30. // syslog has Emerg
  31. DefaultLevel = Notice
  32. )
  33. // String returns the log level name in short form
  34. func (l LogLevel) String() string {
  35. switch l.Match() {
  36. case Critical:
  37. return "CRIT"
  38. case Error:
  39. return "ERR"
  40. case Warning:
  41. return "WARN"
  42. case Notice:
  43. return "NOTE"
  44. case Info:
  45. return "INFO"
  46. case Debug:
  47. return "DEBUG"
  48. case Trace:
  49. return "TRACE"
  50. default:
  51. return "UNSET"
  52. }
  53. }
  54. // String returns the log level name in long human readable form
  55. func (l LogLevel) Name() string {
  56. switch l.Match() {
  57. case Critical:
  58. return "critical"
  59. case Error:
  60. return "error"
  61. case Warning:
  62. return "warning"
  63. case Notice:
  64. return "notice"
  65. case Info:
  66. return "info"
  67. case Debug:
  68. return "debug"
  69. case Trace:
  70. return "trace"
  71. default:
  72. return "unset"
  73. }
  74. }
  75. // Match returns the greatest named log level that is less than or equal to
  76. // the receiver log level. For example, if the log level is 43, Match() will
  77. // return 40 (Warning)
  78. func (l LogLevel) Match() LogLevel {
  79. if l >= Critical {
  80. return Critical
  81. }
  82. if l >= Error {
  83. return Error
  84. }
  85. if l >= Warning {
  86. return Warning
  87. }
  88. if l >= Notice {
  89. return Notice
  90. }
  91. if l >= Info {
  92. return Info
  93. }
  94. if l >= Debug {
  95. return Debug
  96. }
  97. if l >= Trace {
  98. return Trace
  99. }
  100. return 0
  101. }
  102. // LevelFromString will convert a named log level to its corresponding value
  103. // type, or error if both the name was unknown and an integer value was unable
  104. // to be parsed.
  105. func LevelFromString(str string) (LogLevel, error) {
  106. switch strings.ToLower(str) {
  107. case "crit", "critical":
  108. return Critical, nil
  109. case "err", "error":
  110. return Error, nil
  111. case "warn", "warning":
  112. return Warning, nil
  113. case "note", "notice":
  114. return Notice, nil
  115. case "info":
  116. return Info, nil
  117. case "debug":
  118. return Debug, nil
  119. case "trace":
  120. return Trace, nil
  121. }
  122. val, err := strconv.ParseInt(str, 10, 32)
  123. if err == nil {
  124. return LogLevel(val), nil
  125. }
  126. return 0, fmt.Errorf("Invalid log level: %s", str)
  127. }