PageRenderTime 37ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Godeps/_workspace/src/github.com/Sirupsen/logrus/entry.go

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