config.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package config
  2. import (
  3. "io"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "strings"
  8. "git.giaever.org/joachimmg/go-log.git/errors"
  9. )
  10. type LogPrefix string
  11. type LogLevel int
  12. type LogHandle io.Writer
  13. type LogFlag int
  14. type LogDir string
  15. var (
  16. // DiscardHandle: used when log level isnt reached (discard log)
  17. DiscardHandle LogHandle = ioutil.Discard
  18. // HandleTrace: output dest for trace log
  19. HandleTrace LogHandle = os.Stdout
  20. // HandleInfo: output dest for info log
  21. HandleInfo LogHandle = os.Stdout
  22. // HandleWarning: output dest for warning log
  23. HandleWarning LogHandle = os.Stdout
  24. // HandleError: output dest for error log
  25. HandleError LogHandle = os.Stdout
  26. // HandlePanic: output dest for panic log
  27. HandlePanic LogHandle = os.Stderr
  28. // Ldir: Log directory, where file (not "output") logs are stored.
  29. Ldir LogDir = LogDir(os.TempDir() + string(os.PathSeparator) + "go-log")
  30. )
  31. const (
  32. // LogFormat: format in front of log text
  33. LogFormat = log.Ldate | log.Lmicroseconds
  34. // FileMask: filedescriptor mask
  35. FileMask = os.O_CREATE | os.O_WRONLY | os.O_APPEND
  36. // FilePerm: File permission, 0600
  37. FilePerm = 0600
  38. )
  39. const (
  40. // PrefixTrace: Log prefix for trace
  41. PrefixTrace LogPrefix = "TRACE"
  42. // PrefixInfo: Log prefix for info
  43. PrefixInfo LogPrefix = "INFO"
  44. // PrefixWarning: Log prefix for warning
  45. PrefixWarning LogPrefix = "WARNING"
  46. // PrefixError: Log prefix for error
  47. PrefixError LogPrefix = "ERROR"
  48. // PrefixPanic: Log prefix for panic
  49. PrefixPanic LogPrefix = "PANIC"
  50. )
  51. const (
  52. // Trace level
  53. Trace LogLevel = 1<<iota | 0
  54. // Info level
  55. Info LogLevel = 1<<iota | Trace
  56. // Warning level
  57. Warning LogLevel = 1<<iota | Info
  58. // Error level
  59. Error LogLevel = 1<<iota | Warning
  60. // Panic level
  61. Panic LogLevel = 1<<iota | Error
  62. )
  63. var (
  64. // Llo (log level output)
  65. Llo LogLevel = Info
  66. // Llf (log level file)
  67. Llf LogLevel = Warning
  68. )
  69. // Set log level
  70. func (c *LogLevel) Set(l string) error {
  71. l = strings.ToUpper(l)
  72. if l == "TRACE" {
  73. *c = Trace
  74. } else if l == "INFO" {
  75. *c = Info
  76. } else if l == "WARNING" {
  77. *c = Warning
  78. } else if l == "ERROR" {
  79. *c = Error
  80. } else if l == "PANIC" {
  81. *c = Panic
  82. } else {
  83. return errors.UnknownLevel
  84. }
  85. return nil
  86. }
  87. // String function to print log level
  88. func (c *LogLevel) String() string {
  89. switch *c {
  90. case Trace:
  91. return "TRACE"
  92. case Info:
  93. return "INFO"
  94. case Warning:
  95. return "WARNING"
  96. case Error:
  97. return "ERROR"
  98. default:
  99. return "PANIC"
  100. }
  101. }
  102. // Set logging directory
  103. func (d *LogDir) Set(nd string) error {
  104. Ldir = LogDir(nd)
  105. return nil
  106. }
  107. // String function to print log level
  108. func (d *LogDir) String() string {
  109. return string(*d)
  110. }