PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/entry.go

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