level.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // mauLogger - A logger for Go programs
  2. // Copyright (C) 2016-2018 Tulir Asokan
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. package maulogger
  17. import (
  18. "fmt"
  19. )
  20. // Level is the severity level of a log entry.
  21. type Level struct {
  22. Name string
  23. Severity, Color int
  24. }
  25. var (
  26. // LevelDebug is the level for debug messages.
  27. LevelDebug = Level{Name: "DEBUG", Color: -1, Severity: 0}
  28. // LevelInfo is the level for basic log messages.
  29. LevelInfo = Level{Name: "INFO", Color: 36, Severity: 10}
  30. // LevelWarn is the level saying that something went wrong, but the program will continue operating mostly normally.
  31. LevelWarn = Level{Name: "WARN", Color: 33, Severity: 50}
  32. // LevelError is the level saying that something went wrong and the program may not operate as expected, but will still continue.
  33. LevelError = Level{Name: "ERROR", Color: 31, Severity: 100}
  34. // LevelFatal is the level saying that something went wrong and the program will not operate normally.
  35. LevelFatal = Level{Name: "FATAL", Color: 35, Severity: 9001}
  36. )
  37. // GetColor gets the ANSI escape color code for the log level.
  38. func (lvl Level) GetColor() []byte {
  39. if lvl.Color < 0 {
  40. return []byte("")
  41. }
  42. return []byte(fmt.Sprintf("\x1b[%dm", lvl.Color))
  43. }
  44. // GetReset gets the ANSI escape reset code.
  45. func (lvl Level) GetReset() []byte {
  46. if lvl.Color < 0 {
  47. return []byte("")
  48. }
  49. return []byte("\x1b[0m")
  50. }