entry.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package logrus
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "os"
  7. "time"
  8. )
  9. // Defines the key when adding errors using WithError.
  10. var ErrorKey = "error"
  11. // An entry is the final or intermediate Logrus logging entry. It contains all
  12. // the fields passed with WithField{,s}. It's finally logged when Debug, Info,
  13. // Warn, Error, Fatal or Panic is called on it. These objects can be reused and
  14. // passed around as much as you wish to avoid field duplication.
  15. type Entry struct {
  16. Logger *Logger
  17. // Contains all the fields set by the user.
  18. Data Fields
  19. // Time at which the log entry was created
  20. Time time.Time
  21. // Level the log entry was logged at: Debug, Info, Warn, Error, Fatal or Panic
  22. Level Level
  23. // Message passed to Debug, Info, Warn, Error, Fatal or Panic
  24. Message string
  25. }
  26. func NewEntry(logger *Logger) *Entry {
  27. return &Entry{
  28. Logger: logger,
  29. // Default is three fields, give a little extra room
  30. Data: make(Fields, 5),
  31. }
  32. }
  33. // Returns a reader for the entry, which is a proxy to the formatter.
  34. func (entry *Entry) Reader() (*bytes.Buffer, error) {
  35. serialized, err := entry.Logger.Formatter.Format(entry)
  36. return bytes.NewBuffer(serialized), err
  37. }
  38. // Returns the string representation from the reader and ultimately the
  39. // formatter.
  40. func (entry *Entry) String() (string, error) {
  41. reader, err := entry.Reader()
  42. if err != nil {
  43. return "", err
  44. }
  45. return reader.String(), err
  46. }
  47. // Add an error as single field (using the key defined in ErrorKey) to the Entry.
  48. func (entry *Entry) WithError(err error) *Entry {
  49. return entry.WithField(ErrorKey, err)
  50. }
  51. // Add a single field to the Entry.
  52. func (entry *Entry) WithField(key string, value interface{}) *Entry {
  53. return entry.WithFields(Fields{key: value})
  54. }
  55. // Add a map of fields to the Entry.
  56. func (entry *Entry) WithFields(fields Fields) *Entry {
  57. data := Fields{}
  58. for k, v := range entry.Data {
  59. data[k] = v
  60. }
  61. for k, v := range fields {
  62. data[k] = v
  63. }
  64. return &Entry{Logger: entry.Logger, Data: data}
  65. }
  66. // This function is not declared with a pointer value because otherwise
  67. // race conditions will occur when using multiple goroutines
  68. func (entry Entry) log(level Level, msg string) {
  69. entry.Time = time.Now()
  70. entry.Level = level
  71. entry.Message = msg
  72. if err := entry.Logger.Hooks.Fire(level, &entry); err != nil {
  73. entry.Logger.mu.Lock()
  74. fmt.Fprintf(os.Stderr, "Failed to fire hook: %v\n", err)
  75. entry.Logger.mu.Unlock()
  76. }
  77. reader, err := entry.Reader()
  78. if err != nil {
  79. entry.Logger.mu.Lock()
  80. fmt.Fprintf(os.Stderr, "Failed to obtain reader, %v\n", err)
  81. entry.Logger.mu.Unlock()
  82. }
  83. entry.Logger.mu.Lock()
  84. defer entry.Logger.mu.Unlock()
  85. _, err = io.Copy(entry.Logger.Out, reader)
  86. if err != nil {
  87. fmt.Fprintf(os.Stderr, "Failed to write to log, %v\n", err)
  88. }
  89. // To avoid Entry#log() returning a value that only would make sense for
  90. // panic() to use in Entry#Panic(), we avoid the allocation by checking
  91. // directly here.
  92. if level <= PanicLevel {
  93. panic(&entry)
  94. }
  95. }
  96. func (entry *Entry) Debug(args ...interface{}) {
  97. if entry.Logger.Level >= DebugLevel {
  98. entry.log(DebugLevel, fmt.Sprint(args...))
  99. }
  100. }
  101. func (entry *Entry) Print(args ...interface{}) {
  102. entry.Info(args...)
  103. }
  104. func (entry *Entry) Info(args ...interface{}) {
  105. if entry.Logger.Level >= InfoLevel {
  106. entry.log(InfoLevel, fmt.Sprint(args...))
  107. }
  108. }
  109. func (entry *Entry) Warn(args ...interface{}) {
  110. if entry.Logger.Level >= WarnLevel {
  111. entry.log(WarnLevel, fmt.Sprint(args...))
  112. }
  113. }
  114. func (entry *Entry) Warning(args ...interface{}) {
  115. entry.Warn(args...)
  116. }
  117. func (entry *Entry) Error(args ...interface{}) {
  118. if entry.Logger.Level >= ErrorLevel {
  119. entry.log(ErrorLevel, fmt.Sprint(args...))
  120. }
  121. }
  122. func (entry *Entry) Fatal(args ...interface{}) {
  123. if entry.Logger.Level >= FatalLevel {
  124. entry.log(FatalLevel, fmt.Sprint(args...))
  125. }
  126. os.Exit(1)
  127. }
  128. func (entry *Entry) Panic(args ...interface{}) {
  129. if entry.Logger.Level >= PanicLevel {
  130. entry.log(PanicLevel, fmt.Sprint(args...))
  131. }
  132. panic(fmt.Sprint(args...))
  133. }
  134. // Entry Printf family functions
  135. func (entry *Entry) Debugf(format string, args ...interface{}) {
  136. if entry.Logger.Level >= DebugLevel {
  137. entry.Debug(fmt.Sprintf(format, args...))
  138. }
  139. }
  140. func (entry *Entry) Infof(format string, args ...interface{}) {
  141. if entry.Logger.Level >= InfoLevel {
  142. entry.Info(fmt.Sprintf(format, args...))
  143. }
  144. }
  145. func (entry *Entry) Printf(format string, args ...interface{}) {
  146. entry.Infof(format, args...)
  147. }
  148. func (entry *Entry) Warnf(format string, args ...interface{}) {
  149. if entry.Logger.Level >= WarnLevel {
  150. entry.Warn(fmt.Sprintf(format, args...))
  151. }
  152. }
  153. func (entry *Entry) Warningf(format string, args ...interface{}) {
  154. entry.Warnf(format, args...)
  155. }
  156. func (entry *Entry) Errorf(format string, args ...interface{}) {
  157. if entry.Logger.Level >= ErrorLevel {
  158. entry.Error(fmt.Sprintf(format, args...))
  159. }
  160. }
  161. func (entry *Entry) Fatalf(format string, args ...interface{}) {
  162. if entry.Logger.Level >= FatalLevel {
  163. entry.Fatal(fmt.Sprintf(format, args...))
  164. }
  165. os.Exit(1)
  166. }
  167. func (entry *Entry) Panicf(format string, args ...interface{}) {
  168. if entry.Logger.Level >= PanicLevel {
  169. entry.Panic(fmt.Sprintf(format, args...))
  170. }
  171. }
  172. // Entry Println family functions
  173. func (entry *Entry) Debugln(args ...interface{}) {
  174. if entry.Logger.Level >= DebugLevel {
  175. entry.Debug(entry.sprintlnn(args...))
  176. }
  177. }
  178. func (entry *Entry) Infoln(args ...interface{}) {
  179. if entry.Logger.Level >= InfoLevel {
  180. entry.Info(entry.sprintlnn(args...))
  181. }
  182. }
  183. func (entry *Entry) Println(args ...interface{}) {
  184. entry.Infoln(args...)
  185. }
  186. func (entry *Entry) Warnln(args ...interface{}) {
  187. if entry.Logger.Level >= WarnLevel {
  188. entry.Warn(entry.sprintlnn(args...))
  189. }
  190. }
  191. func (entry *Entry) Warningln(args ...interface{}) {
  192. entry.Warnln(args...)
  193. }
  194. func (entry *Entry) Errorln(args ...interface{}) {
  195. if entry.Logger.Level >= ErrorLevel {
  196. entry.Error(entry.sprintlnn(args...))
  197. }
  198. }
  199. func (entry *Entry) Fatalln(args ...interface{}) {
  200. if entry.Logger.Level >= FatalLevel {
  201. entry.Fatal(entry.sprintlnn(args...))
  202. }
  203. os.Exit(1)
  204. }
  205. func (entry *Entry) Panicln(args ...interface{}) {
  206. if entry.Logger.Level >= PanicLevel {
  207. entry.Panic(entry.sprintlnn(args...))
  208. }
  209. }
  210. // Sprintlnn => Sprint no newline. This is to get the behavior of how
  211. // fmt.Sprintln where spaces are always added between operands, regardless of
  212. // their type. Instead of vendoring the Sprintln implementation to spare a
  213. // string allocation, we do the simplest thing.
  214. func (entry *Entry) sprintlnn(args ...interface{}) string {
  215. msg := fmt.Sprintln(args...)
  216. return msg[:len(msg)-1]
  217. }