logging.go 4.7 KB

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