logging.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package util
  11. import (
  12. "fmt"
  13. "io"
  14. "log"
  15. "strings"
  16. "devt.de/krotik/common/datautil"
  17. )
  18. // Loger with loglevel support
  19. // ===========================
  20. /*
  21. LogLevel represents a logging level
  22. */
  23. type LogLevel string
  24. /*
  25. Log levels
  26. */
  27. const (
  28. Debug LogLevel = "debug"
  29. Info = "info"
  30. Error = "error"
  31. )
  32. /*
  33. LogLevelLogger is a wrapper around loggers to add log level functionality.
  34. */
  35. type LogLevelLogger struct {
  36. logger Logger
  37. level LogLevel
  38. }
  39. func NewLogLevelLogger(logger Logger, level string) (*LogLevelLogger, error) {
  40. llevel := LogLevel(strings.ToLower(level))
  41. if llevel != Debug && llevel != Info && llevel != Error {
  42. return nil, fmt.Errorf("Invalid log level: %v", llevel)
  43. }
  44. return &LogLevelLogger{
  45. logger,
  46. llevel,
  47. }, nil
  48. }
  49. /*
  50. Level returns the current log level.
  51. */
  52. func (ll *LogLevelLogger) Level() LogLevel {
  53. return ll.level
  54. }
  55. /*
  56. LogError adds a new error log message.
  57. */
  58. func (ll *LogLevelLogger) LogError(m ...interface{}) {
  59. ll.logger.LogError(m...)
  60. }
  61. /*
  62. LogInfo adds a new info log message.
  63. */
  64. func (ll *LogLevelLogger) LogInfo(m ...interface{}) {
  65. if ll.level == Info || ll.level == Debug {
  66. ll.logger.LogInfo(m...)
  67. }
  68. }
  69. /*
  70. LogDebug adds a new debug log message.
  71. */
  72. func (ll *LogLevelLogger) LogDebug(m ...interface{}) {
  73. if ll.level == Debug {
  74. ll.logger.LogDebug(m...)
  75. }
  76. }
  77. // Logging implementations
  78. // =======================
  79. /*
  80. MemoryLogger collects log messages in a RingBuffer in memory.
  81. */
  82. type MemoryLogger struct {
  83. *datautil.RingBuffer
  84. }
  85. /*
  86. NewMemoryLogger returns a new memory logger instance.
  87. */
  88. func NewMemoryLogger(size int) *MemoryLogger {
  89. return &MemoryLogger{datautil.NewRingBuffer(size)}
  90. }
  91. /*
  92. LogError adds a new error log message.
  93. */
  94. func (ml *MemoryLogger) LogError(m ...interface{}) {
  95. ml.RingBuffer.Add(fmt.Sprintf("error: %v", fmt.Sprint(m...)))
  96. }
  97. /*
  98. LogInfo adds a new info log message.
  99. */
  100. func (ml *MemoryLogger) LogInfo(m ...interface{}) {
  101. ml.RingBuffer.Add(fmt.Sprintf("%v", fmt.Sprint(m...)))
  102. }
  103. /*
  104. LogDebug adds a new debug log message.
  105. */
  106. func (ml *MemoryLogger) LogDebug(m ...interface{}) {
  107. ml.RingBuffer.Add(fmt.Sprintf("debug: %v", fmt.Sprint(m...)))
  108. }
  109. /*
  110. Slice returns the contents of the current log as a slice.
  111. */
  112. func (ml *MemoryLogger) Slice() []string {
  113. sl := ml.RingBuffer.Slice()
  114. ret := make([]string, len(sl))
  115. for i, lm := range sl {
  116. ret[i] = lm.(string)
  117. }
  118. return ret
  119. }
  120. /*
  121. Reset resets the current log.
  122. */
  123. func (ml *MemoryLogger) Reset() {
  124. ml.RingBuffer.Reset()
  125. }
  126. /*
  127. Size returns the current log size.
  128. */
  129. func (ml *MemoryLogger) Size() int {
  130. return ml.RingBuffer.Size()
  131. }
  132. /*
  133. String returns the current log as a string.
  134. */
  135. func (ml *MemoryLogger) String() string {
  136. return ml.RingBuffer.String()
  137. }
  138. /*
  139. StdOutLogger writes log messages to stdout.
  140. */
  141. type StdOutLogger struct {
  142. stdlog func(v ...interface{})
  143. }
  144. /*
  145. NewStdOutLogger returns a stdout logger instance.
  146. */
  147. func NewStdOutLogger() *StdOutLogger {
  148. return &StdOutLogger{log.Print}
  149. }
  150. /*
  151. LogError adds a new error log message.
  152. */
  153. func (sl *StdOutLogger) LogError(m ...interface{}) {
  154. sl.stdlog(fmt.Sprintf("error: %v", fmt.Sprint(m...)))
  155. }
  156. /*
  157. LogInfo adds a new info log message.
  158. */
  159. func (sl *StdOutLogger) LogInfo(m ...interface{}) {
  160. sl.stdlog(fmt.Sprintf("%v", fmt.Sprint(m...)))
  161. }
  162. /*
  163. LogDebug adds a new debug log message.
  164. */
  165. func (sl *StdOutLogger) LogDebug(m ...interface{}) {
  166. sl.stdlog(fmt.Sprintf("debug: %v", fmt.Sprint(m...)))
  167. }
  168. /*
  169. NullLogger discards log messages.
  170. */
  171. type NullLogger struct {
  172. }
  173. /*
  174. NewNullLogger returns a null logger instance.
  175. */
  176. func NewNullLogger() *NullLogger {
  177. return &NullLogger{}
  178. }
  179. /*
  180. LogError adds a new error log message.
  181. */
  182. func (nl *NullLogger) LogError(m ...interface{}) {
  183. }
  184. /*
  185. LogInfo adds a new info log message.
  186. */
  187. func (nl *NullLogger) LogInfo(m ...interface{}) {
  188. }
  189. /*
  190. LogDebug adds a new debug log message.
  191. */
  192. func (nl *NullLogger) LogDebug(m ...interface{}) {
  193. }
  194. /*
  195. BufferLogger logs into a buffer.
  196. */
  197. type BufferLogger struct {
  198. buf io.Writer
  199. }
  200. /*
  201. NewNullLogger returns a buffer logger instance.
  202. */
  203. func NewBufferLogger(buf io.Writer) *BufferLogger {
  204. return &BufferLogger{buf}
  205. }
  206. /*
  207. LogError adds a new error log message.
  208. */
  209. func (bl *BufferLogger) LogError(m ...interface{}) {
  210. fmt.Fprintln(bl.buf, fmt.Sprintf("error: %v", fmt.Sprint(m...)))
  211. }
  212. /*
  213. LogInfo adds a new info log message.
  214. */
  215. func (bl *BufferLogger) LogInfo(m ...interface{}) {
  216. fmt.Fprintln(bl.buf, fmt.Sprintf("%v", fmt.Sprint(m...)))
  217. }
  218. /*
  219. LogDebug adds a new debug log message.
  220. */
  221. func (bl *BufferLogger) LogDebug(m ...interface{}) {
  222. fmt.Fprintln(bl.buf, fmt.Sprintf("debug: %v", fmt.Sprint(m...)))
  223. }