PageRenderTime 57ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/entry.go

https://github.com/rayleyva/logrus
Go | 219 lines | 171 code | 41 blank | 7 comment | 28 complexity | 0ce1f5b0019ac73fe4c46f94bee81c10 MD5 | raw file
  1. package logrus
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "os"
  7. "time"
  8. )
  9. type Entry struct {
  10. Logger *Logger
  11. Data Fields
  12. }
  13. var baseTimestamp time.Time
  14. func NewEntry(logger *Logger) *Entry {
  15. return &Entry{
  16. Logger: logger,
  17. // Default is three fields, give a little extra room
  18. Data: make(Fields, 5),
  19. }
  20. }
  21. func (entry *Entry) Reader() (*bytes.Buffer, error) {
  22. serialized, err := entry.Logger.Formatter.Format(entry)
  23. return bytes.NewBuffer(serialized), err
  24. }
  25. func (entry *Entry) String() (string, error) {
  26. reader, err := entry.Reader()
  27. if err != nil {
  28. return "", err
  29. }
  30. return reader.String(), err
  31. }
  32. func (entry *Entry) WithField(key string, value interface{}) *Entry {
  33. entry.Data[key] = value
  34. return entry
  35. }
  36. func (entry *Entry) WithFields(fields Fields) *Entry {
  37. for key, value := range fields {
  38. entry.WithField(key, value)
  39. }
  40. return entry
  41. }
  42. func (entry *Entry) log(level string, levelInt Level, msg string) string {
  43. entry.Data["time"] = time.Now().String()
  44. entry.Data["level"] = level
  45. entry.Data["msg"] = msg
  46. if err := entry.Logger.Hooks.Fire(levelInt, entry); err != nil {
  47. fmt.Fprintf(os.Stderr, "Failed to fire hook", err)
  48. }
  49. reader, err := entry.Reader()
  50. if err != nil {
  51. fmt.Fprintf(os.Stderr, "Failed to obtain reader, %v", err)
  52. }
  53. entry.Logger.mu.Lock()
  54. defer entry.Logger.mu.Unlock()
  55. _, err = io.Copy(entry.Logger.Out, reader)
  56. if err != nil {
  57. fmt.Fprintf(os.Stderr, "Failed to write to log, %v", err)
  58. }
  59. return reader.String()
  60. }
  61. func (entry *Entry) Debug(args ...interface{}) {
  62. if entry.Logger.Level >= Debug {
  63. entry.log("debug", Debug, fmt.Sprint(args...))
  64. }
  65. }
  66. func (entry *Entry) Print(args ...interface{}) {
  67. entry.Info(args...)
  68. }
  69. func (entry *Entry) Info(args ...interface{}) {
  70. if entry.Logger.Level >= Info {
  71. entry.log("info", Info, fmt.Sprint(args...))
  72. }
  73. }
  74. func (entry *Entry) Warn(args ...interface{}) {
  75. if entry.Logger.Level >= Warn {
  76. entry.log("warning", Warn, fmt.Sprint(args...))
  77. }
  78. }
  79. func (entry *Entry) Error(args ...interface{}) {
  80. if entry.Logger.Level >= Error {
  81. entry.log("error", Error, fmt.Sprint(args...))
  82. }
  83. }
  84. func (entry *Entry) Fatal(args ...interface{}) {
  85. if entry.Logger.Level >= Fatal {
  86. entry.log("fatal", Fatal, fmt.Sprint(args...))
  87. }
  88. os.Exit(1)
  89. }
  90. func (entry *Entry) Panic(args ...interface{}) {
  91. if entry.Logger.Level >= Panic {
  92. msg := entry.log("panic", Panic, fmt.Sprint(args...))
  93. panic(msg)
  94. }
  95. panic(fmt.Sprint(args...))
  96. }
  97. // Entry Printf family functions
  98. func (entry *Entry) Debugf(format string, args ...interface{}) {
  99. if entry.Logger.Level >= Debug {
  100. entry.Debug(fmt.Sprintf(format, args...))
  101. }
  102. }
  103. func (entry *Entry) Infof(format string, args ...interface{}) {
  104. if entry.Logger.Level >= Info {
  105. entry.Info(fmt.Sprintf(format, args...))
  106. }
  107. }
  108. func (entry *Entry) Printf(format string, args ...interface{}) {
  109. entry.Infof(format, args...)
  110. }
  111. func (entry *Entry) Warnf(format string, args ...interface{}) {
  112. if entry.Logger.Level >= Warn {
  113. entry.Warn(fmt.Sprintf(format, args...))
  114. }
  115. }
  116. func (entry *Entry) Warningf(format string, args ...interface{}) {
  117. entry.Warnf(format, args...)
  118. }
  119. func (entry *Entry) Errorf(format string, args ...interface{}) {
  120. if entry.Logger.Level >= Error {
  121. entry.Error(fmt.Sprintf(format, args...))
  122. }
  123. }
  124. func (entry *Entry) Fatalf(format string, args ...interface{}) {
  125. if entry.Logger.Level >= Fatal {
  126. entry.Fatal(fmt.Sprintf(format, args...))
  127. }
  128. }
  129. func (entry *Entry) Panicf(format string, args ...interface{}) {
  130. if entry.Logger.Level >= Panic {
  131. entry.Panic(fmt.Sprintf(format, args...))
  132. }
  133. }
  134. // Entry Println family functions
  135. func (entry *Entry) Debugln(args ...interface{}) {
  136. if entry.Logger.Level >= Debug {
  137. entry.Debug(entry.sprintlnn(args...))
  138. }
  139. }
  140. func (entry *Entry) Infoln(args ...interface{}) {
  141. if entry.Logger.Level >= Info {
  142. entry.Info(entry.sprintlnn(args...))
  143. }
  144. }
  145. func (entry *Entry) Println(args ...interface{}) {
  146. entry.Infoln(args...)
  147. }
  148. func (entry *Entry) Warnln(args ...interface{}) {
  149. if entry.Logger.Level >= Warn {
  150. entry.Warn(entry.sprintlnn(args...))
  151. }
  152. }
  153. func (entry *Entry) Warningln(args ...interface{}) {
  154. entry.Warnln(args...)
  155. }
  156. func (entry *Entry) Errorln(args ...interface{}) {
  157. if entry.Logger.Level >= Error {
  158. entry.Error(entry.sprintlnn(args...))
  159. }
  160. }
  161. func (entry *Entry) Fatalln(args ...interface{}) {
  162. if entry.Logger.Level >= Fatal {
  163. entry.Fatal(entry.sprintlnn(args...))
  164. }
  165. }
  166. func (entry *Entry) Panicln(args ...interface{}) {
  167. if entry.Logger.Level >= Panic {
  168. entry.Panic(entry.sprintlnn(args...))
  169. }
  170. }
  171. // Sprintlnn => Sprint no newline. This is to get the behavior of how
  172. // fmt.Sprintln where spaces are always added between operands, regardless of
  173. // their type. Instead of vendoring the Sprintln implementation to spare a
  174. // string allocation, we do the simplest thing.
  175. func (entry *Entry) sprintlnn(args ...interface{}) string {
  176. msg := fmt.Sprintln(args...)
  177. return msg[:len(msg)-1]
  178. }