logger.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 // import "github.com/docker/docker/daemon/logger"
  9. import (
  10. "sync"
  11. "time"
  12. "github.com/docker/docker/api/types/backend"
  13. )
  14. // ErrReadLogsNotSupported is returned when the underlying log driver does not support reading
  15. type ErrReadLogsNotSupported struct{}
  16. func (ErrReadLogsNotSupported) Error() string {
  17. return "configured logging driver does not support reading"
  18. }
  19. // NotImplemented makes this error implement the `NotImplemented` interface from api/errdefs
  20. func (ErrReadLogsNotSupported) NotImplemented() {}
  21. const (
  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 data structure 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. //
  39. // Message is subtyped from backend.LogMessage because there is a lot of
  40. // internal complexity around the Message type that should not be exposed
  41. // to any package not explicitly importing the logger type.
  42. //
  43. // Any changes made to this struct must also be updated in the `reset` function
  44. type Message backend.LogMessage
  45. // reset sets the message back to default values
  46. // This is used when putting a message back into the message pool.
  47. // Any changes to the `Message` struct should be reflected here.
  48. func (m *Message) reset() {
  49. m.Line = m.Line[:0]
  50. m.Source = ""
  51. m.Attrs = nil
  52. m.PLogMetaData = nil
  53. m.Err = nil
  54. }
  55. // AsLogMessage returns a pointer to the message as a pointer to
  56. // backend.LogMessage, which is an identical type with a different purpose
  57. func (m *Message) AsLogMessage() *backend.LogMessage {
  58. return (*backend.LogMessage)(m)
  59. }
  60. // Logger is the interface for docker logging drivers.
  61. type Logger interface {
  62. Log(*Message) error
  63. Name() string
  64. Close() error
  65. }
  66. // SizedLogger is the interface for logging drivers that can control
  67. // the size of buffer used for their messages.
  68. type SizedLogger interface {
  69. Logger
  70. BufSize() int
  71. }
  72. // ReadConfig is the configuration passed into ReadLogs.
  73. type ReadConfig struct {
  74. Since time.Time
  75. Until time.Time
  76. Tail int
  77. Follow bool
  78. }
  79. // LogReader is the interface for reading log messages for loggers that support reading.
  80. type LogReader interface {
  81. // Read logs from underlying logging backend
  82. ReadLogs(ReadConfig) *LogWatcher
  83. }
  84. // LogWatcher is used when consuming logs read from the LogReader interface.
  85. type LogWatcher struct {
  86. // For sending log messages to a reader.
  87. Msg chan *Message
  88. // For sending error messages that occur while reading logs.
  89. Err chan error
  90. producerOnce sync.Once
  91. producerGone chan struct{}
  92. consumerOnce sync.Once
  93. consumerGone chan struct{}
  94. }
  95. // NewLogWatcher returns a new LogWatcher.
  96. func NewLogWatcher() *LogWatcher {
  97. return &LogWatcher{
  98. Msg: make(chan *Message, logWatcherBufferSize),
  99. Err: make(chan error, 1),
  100. producerGone: make(chan struct{}),
  101. consumerGone: make(chan struct{}),
  102. }
  103. }
  104. // ProducerGone notifies the underlying log reader that
  105. // the logs producer (a container) is gone.
  106. func (w *LogWatcher) ProducerGone() {
  107. // only close if not already closed
  108. w.producerOnce.Do(func() {
  109. close(w.producerGone)
  110. })
  111. }
  112. // WatchProducerGone returns a channel receiver that receives notification
  113. // once the logs producer (a container) is gone.
  114. func (w *LogWatcher) WatchProducerGone() <-chan struct{} {
  115. return w.producerGone
  116. }
  117. // ConsumerGone notifies that the logs consumer is gone.
  118. func (w *LogWatcher) ConsumerGone() {
  119. // only close if not already closed
  120. w.consumerOnce.Do(func() {
  121. close(w.consumerGone)
  122. })
  123. }
  124. // WatchConsumerGone returns a channel receiver that receives notification
  125. // when the log watcher consumer is gone.
  126. func (w *LogWatcher) WatchConsumerGone() <-chan struct{} {
  127. return w.consumerGone
  128. }
  129. // Capability defines the list of capabilities that a driver can implement
  130. // These capabilities are not required to be a logging driver, however do
  131. // determine how a logging driver can be used
  132. type Capability struct {
  133. // Determines if a log driver can read back logs
  134. ReadLogs bool
  135. }
  136. // MarshalFunc is a func that marshals a message into an arbitrary format
  137. type MarshalFunc func(*Message) ([]byte, error)