http urls monitor.

entry.go 7.3KB

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