logging.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. /*
  40. NewLogLevelLogger wraps a given logger and adds level based filtering functionality.
  41. */
  42. func NewLogLevelLogger(logger Logger, level string) (*LogLevelLogger, error) {
  43. llevel := LogLevel(strings.ToLower(level))
  44. if llevel != Debug && llevel != Info && llevel != Error {
  45. return nil, fmt.Errorf("Invalid log level: %v", llevel)
  46. }
  47. return &LogLevelLogger{
  48. logger,
  49. llevel,
  50. }, nil
  51. }
  52. /*
  53. Level returns the current log level.
  54. */
  55. func (ll *LogLevelLogger) Level() LogLevel {
  56. return ll.level
  57. }
  58. /*
  59. LogError adds a new error log message.
  60. */
  61. func (ll *LogLevelLogger) LogError(m ...interface{}) {
  62. ll.logger.LogError(m...)
  63. }
  64. /*
  65. LogInfo adds a new info log message.
  66. */
  67. func (ll *LogLevelLogger) LogInfo(m ...interface{}) {
  68. if ll.level == Info || ll.level == Debug {
  69. ll.logger.LogInfo(m...)
  70. }
  71. }
  72. /*
  73. LogDebug adds a new debug log message.
  74. */
  75. func (ll *LogLevelLogger) LogDebug(m ...interface{}) {
  76. if ll.level == Debug {
  77. ll.logger.LogDebug(m...)
  78. }
  79. }
  80. // Logging implementations
  81. // =======================
  82. /*
  83. MemoryLogger collects log messages in a RingBuffer in memory.
  84. */
  85. type MemoryLogger struct {
  86. *datautil.RingBuffer
  87. }
  88. /*
  89. NewMemoryLogger returns a new memory logger instance.
  90. */
  91. func NewMemoryLogger(size int) *MemoryLogger {
  92. return &MemoryLogger{datautil.NewRingBuffer(size)}
  93. }
  94. /*
  95. LogError adds a new error log message.
  96. */
  97. func (ml *MemoryLogger) LogError(m ...interface{}) {
  98. ml.RingBuffer.Add(fmt.Sprintf("error: %v", fmt.Sprint(m...)))
  99. }
  100. /*
  101. LogInfo adds a new info log message.
  102. */
  103. func (ml *MemoryLogger) LogInfo(m ...interface{}) {
  104. ml.RingBuffer.Add(fmt.Sprintf("%v", fmt.Sprint(m...)))
  105. }
  106. /*
  107. LogDebug adds a new debug log message.
  108. */
  109. func (ml *MemoryLogger) LogDebug(m ...interface{}) {
  110. ml.RingBuffer.Add(fmt.Sprintf("debug: %v", fmt.Sprint(m...)))
  111. }
  112. /*
  113. Slice returns the contents of the current log as a slice.
  114. */
  115. func (ml *MemoryLogger) Slice() []string {
  116. sl := ml.RingBuffer.Slice()
  117. ret := make([]string, len(sl))
  118. for i, lm := range sl {
  119. ret[i] = lm.(string)
  120. }
  121. return ret
  122. }
  123. /*
  124. Reset resets the current log.
  125. */
  126. func (ml *MemoryLogger) Reset() {
  127. ml.RingBuffer.Reset()
  128. }
  129. /*
  130. Size returns the current log size.
  131. */
  132. func (ml *MemoryLogger) Size() int {
  133. return ml.RingBuffer.Size()
  134. }
  135. /*
  136. String returns the current log as a string.
  137. */
  138. func (ml *MemoryLogger) String() string {
  139. return ml.RingBuffer.String()
  140. }
  141. /*
  142. StdOutLogger writes log messages to stdout.
  143. */
  144. type StdOutLogger struct {
  145. stdlog func(v ...interface{})
  146. }
  147. /*
  148. NewStdOutLogger returns a stdout logger instance.
  149. */
  150. func NewStdOutLogger() *StdOutLogger {
  151. return &StdOutLogger{log.Print}
  152. }
  153. /*
  154. LogError adds a new error log message.
  155. */
  156. func (sl *StdOutLogger) LogError(m ...interface{}) {
  157. sl.stdlog(fmt.Sprintf("error: %v", fmt.Sprint(m...)))
  158. }
  159. /*
  160. LogInfo adds a new info log message.
  161. */
  162. func (sl *StdOutLogger) LogInfo(m ...interface{}) {
  163. sl.stdlog(fmt.Sprintf("%v", fmt.Sprint(m...)))
  164. }
  165. /*
  166. LogDebug adds a new debug log message.
  167. */
  168. func (sl *StdOutLogger) LogDebug(m ...interface{}) {
  169. sl.stdlog(fmt.Sprintf("debug: %v", fmt.Sprint(m...)))
  170. }
  171. /*
  172. NullLogger discards log messages.
  173. */
  174. type NullLogger struct {
  175. }
  176. /*
  177. NewNullLogger returns a null logger instance.
  178. */
  179. func NewNullLogger() *NullLogger {
  180. return &NullLogger{}
  181. }
  182. /*
  183. LogError adds a new error log message.
  184. */
  185. func (nl *NullLogger) LogError(m ...interface{}) {
  186. }
  187. /*
  188. LogInfo adds a new info log message.
  189. */
  190. func (nl *NullLogger) LogInfo(m ...interface{}) {
  191. }
  192. /*
  193. LogDebug adds a new debug log message.
  194. */
  195. func (nl *NullLogger) LogDebug(m ...interface{}) {
  196. }
  197. /*
  198. BufferLogger logs into a buffer.
  199. */
  200. type BufferLogger struct {
  201. buf io.Writer
  202. }
  203. /*
  204. NewBufferLogger returns a buffer logger instance.
  205. */
  206. func NewBufferLogger(buf io.Writer) *BufferLogger {
  207. return &BufferLogger{buf}
  208. }
  209. /*
  210. LogError adds a new error log message.
  211. */
  212. func (bl *BufferLogger) LogError(m ...interface{}) {
  213. fmt.Fprintln(bl.buf, fmt.Sprintf("error: %v", fmt.Sprint(m...)))
  214. }
  215. /*
  216. LogInfo adds a new info log message.
  217. */
  218. func (bl *BufferLogger) LogInfo(m ...interface{}) {
  219. fmt.Fprintln(bl.buf, fmt.Sprintf("%v", fmt.Sprint(m...)))
  220. }
  221. /*
  222. LogDebug adds a new debug log message.
  223. */
  224. func (bl *BufferLogger) LogDebug(m ...interface{}) {
  225. fmt.Fprintln(bl.buf, fmt.Sprintf("debug: %v", fmt.Sprint(m...)))
  226. }