logger.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Package logger defines interfaces that logger drivers implement to
  2. // log messages.
  3. //
  4. // The other half of a logger driver is the implementation of the
  5. // factory, which holds the contextual instance information that
  6. // allows multiple loggers of the same type to perform different
  7. // actions, such as logging to different locations.
  8. package logger
  9. import (
  10. "errors"
  11. "sort"
  12. "strings"
  13. "sync"
  14. "time"
  15. "github.com/docker/docker/pkg/jsonlog"
  16. )
  17. // ErrReadLogsNotSupported is returned when the logger does not support reading logs.
  18. var ErrReadLogsNotSupported = errors.New("configured logging driver does not support reading")
  19. const (
  20. // TimeFormat is the time format used for timestamps sent to log readers.
  21. TimeFormat = jsonlog.RFC3339NanoFixed
  22. logWatcherBufferSize = 4096
  23. )
  24. var messagePool = &sync.Pool{New: func() interface{} { return &Message{Line: make([]byte, 0, 256)} }}
  25. // NewMessage returns a new message from the message sync.Pool
  26. func NewMessage() *Message {
  27. return messagePool.Get().(*Message)
  28. }
  29. // PutMessage puts the specified message back n the message pool.
  30. // The message fields are reset before putting into the pool.
  31. func PutMessage(msg *Message) {
  32. msg.reset()
  33. messagePool.Put(msg)
  34. }
  35. // Message is datastructure that represents piece of output produced by some
  36. // container. The Line member is a slice of an array whose contents can be
  37. // changed after a log driver's Log() method returns.
  38. // Any changes made to this struct must also be updated in the `reset` function
  39. type Message struct {
  40. Line []byte
  41. Source string
  42. Timestamp time.Time
  43. Attrs LogAttributes
  44. Partial bool
  45. }
  46. // reset sets the message back to default values
  47. // This is used when putting a message back into the message pool.
  48. // Any changes to the `Message` struct should be reflected here.
  49. func (m *Message) reset() {
  50. m.Line = m.Line[:0]
  51. m.Source = ""
  52. m.Attrs = nil
  53. m.Partial = false
  54. }
  55. // LogAttributes is used to hold the extra attributes available in the log message
  56. // Primarily used for converting the map type to string and sorting.
  57. type LogAttributes map[string]string
  58. type byKey []string
  59. func (s byKey) Len() int { return len(s) }
  60. func (s byKey) Less(i, j int) bool {
  61. keyI := strings.Split(s[i], "=")
  62. keyJ := strings.Split(s[j], "=")
  63. return keyI[0] < keyJ[0]
  64. }
  65. func (s byKey) Swap(i, j int) {
  66. s[i], s[j] = s[j], s[i]
  67. }
  68. func (a LogAttributes) String() string {
  69. var ss byKey
  70. for k, v := range a {
  71. ss = append(ss, k+"="+v)
  72. }
  73. sort.Sort(ss)
  74. return strings.Join(ss, ",")
  75. }
  76. // Logger is the interface for docker logging drivers.
  77. type Logger interface {
  78. Log(*Message) error
  79. Name() string
  80. Close() error
  81. }
  82. // ReadConfig is the configuration passed into ReadLogs.
  83. type ReadConfig struct {
  84. Since time.Time
  85. Tail int
  86. Follow bool
  87. }
  88. // LogReader is the interface for reading log messages for loggers that support reading.
  89. type LogReader interface {
  90. // Read logs from underlying logging backend
  91. ReadLogs(ReadConfig) *LogWatcher
  92. }
  93. // LogWatcher is used when consuming logs read from the LogReader interface.
  94. type LogWatcher struct {
  95. // For sending log messages to a reader.
  96. Msg chan *Message
  97. // For sending error messages that occur while while reading logs.
  98. Err chan error
  99. closeOnce sync.Once
  100. closeNotifier chan struct{}
  101. }
  102. // NewLogWatcher returns a new LogWatcher.
  103. func NewLogWatcher() *LogWatcher {
  104. return &LogWatcher{
  105. Msg: make(chan *Message, logWatcherBufferSize),
  106. Err: make(chan error, 1),
  107. closeNotifier: make(chan struct{}),
  108. }
  109. }
  110. // Close notifies the underlying log reader to stop.
  111. func (w *LogWatcher) Close() {
  112. // only close if not already closed
  113. w.closeOnce.Do(func() {
  114. close(w.closeNotifier)
  115. })
  116. }
  117. // WatchClose returns a channel receiver that receives notification
  118. // when the watcher has been closed. This should only be called from
  119. // one goroutine.
  120. func (w *LogWatcher) WatchClose() <-chan struct{} {
  121. return w.closeNotifier
  122. }