logger.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 reader 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. // Message is datastructure that represents piece of output produced by some
  25. // container. The Line member is a slice of an array whose contents can be
  26. // changed after a log driver's Log() method returns.
  27. type Message struct {
  28. Line []byte
  29. Source string
  30. Timestamp time.Time
  31. Attrs LogAttributes
  32. Partial bool
  33. }
  34. // CopyMessage creates a copy of the passed-in Message which will remain
  35. // unchanged if the original is changed. Log drivers which buffer Messages
  36. // rather than dispatching them during their Log() method should use this
  37. // function to obtain a Message whose Line member's contents won't change.
  38. func CopyMessage(msg *Message) *Message {
  39. m := new(Message)
  40. m.Line = make([]byte, len(msg.Line))
  41. copy(m.Line, msg.Line)
  42. m.Source = msg.Source
  43. m.Timestamp = msg.Timestamp
  44. m.Partial = msg.Partial
  45. m.Attrs = make(LogAttributes)
  46. for k, v := range m.Attrs {
  47. m.Attrs[k] = v
  48. }
  49. return m
  50. }
  51. // LogAttributes is used to hold the extra attributes available in the log message
  52. // Primarily used for converting the map type to string and sorting.
  53. type LogAttributes map[string]string
  54. type byKey []string
  55. func (s byKey) Len() int { return len(s) }
  56. func (s byKey) Less(i, j int) bool {
  57. keyI := strings.Split(s[i], "=")
  58. keyJ := strings.Split(s[j], "=")
  59. return keyI[0] < keyJ[0]
  60. }
  61. func (s byKey) Swap(i, j int) {
  62. s[i], s[j] = s[j], s[i]
  63. }
  64. func (a LogAttributes) String() string {
  65. var ss byKey
  66. for k, v := range a {
  67. ss = append(ss, k+"="+v)
  68. }
  69. sort.Sort(ss)
  70. return strings.Join(ss, ",")
  71. }
  72. // Logger is the interface for docker logging drivers.
  73. type Logger interface {
  74. Log(*Message) error
  75. Name() string
  76. Close() error
  77. }
  78. // ReadConfig is the configuration passed into ReadLogs.
  79. type ReadConfig struct {
  80. Since time.Time
  81. Tail int
  82. Follow bool
  83. }
  84. // LogReader is the interface for reading log messages for loggers that support reading.
  85. type LogReader interface {
  86. // Read logs from underlying logging backend
  87. ReadLogs(ReadConfig) *LogWatcher
  88. }
  89. // LogWatcher is used when consuming logs read from the LogReader interface.
  90. type LogWatcher struct {
  91. // For sending log messages to a reader.
  92. Msg chan *Message
  93. // For sending error messages that occur while while reading logs.
  94. Err chan error
  95. closeOnce sync.Once
  96. closeNotifier chan struct{}
  97. }
  98. // NewLogWatcher returns a new LogWatcher.
  99. func NewLogWatcher() *LogWatcher {
  100. return &LogWatcher{
  101. Msg: make(chan *Message, logWatcherBufferSize),
  102. Err: make(chan error, 1),
  103. closeNotifier: make(chan struct{}),
  104. }
  105. }
  106. // Close notifies the underlying log reader to stop.
  107. func (w *LogWatcher) Close() {
  108. // only close if not already closed
  109. w.closeOnce.Do(func() {
  110. close(w.closeNotifier)
  111. })
  112. }
  113. // WatchClose returns a channel receiver that receives notification
  114. // when the watcher has been closed. This should only be called from
  115. // one goroutine.
  116. func (w *LogWatcher) WatchClose() <-chan struct{} {
  117. return w.closeNotifier
  118. }