logger.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. type Message backend.LogMessage
  43. // reset sets the message back to default values
  44. // This is used when putting a message back into the message pool.
  45. func (m *Message) reset() {
  46. *m = Message{Line: m.Line[:0]}
  47. }
  48. // AsLogMessage returns a pointer to the message as a pointer to
  49. // backend.LogMessage, which is an identical type with a different purpose
  50. func (m *Message) AsLogMessage() *backend.LogMessage {
  51. return (*backend.LogMessage)(m)
  52. }
  53. // Logger is the interface for docker logging drivers.
  54. type Logger interface {
  55. Log(*Message) error
  56. Name() string
  57. Close() error
  58. }
  59. // SizedLogger is the interface for logging drivers that can control
  60. // the size of buffer used for their messages.
  61. type SizedLogger interface {
  62. Logger
  63. BufSize() int
  64. }
  65. // ReadConfig is the configuration passed into ReadLogs.
  66. type ReadConfig struct {
  67. Since time.Time
  68. Until time.Time
  69. Tail int
  70. Follow bool
  71. }
  72. // LogReader is the interface for reading log messages for loggers that support reading.
  73. type LogReader interface {
  74. // ReadLogs reads logs from underlying logging backend.
  75. ReadLogs(ReadConfig) *LogWatcher
  76. }
  77. // LogWatcher is used when consuming logs read from the LogReader interface.
  78. type LogWatcher struct {
  79. // For sending log messages to a reader.
  80. Msg chan *Message
  81. // For sending error messages that occur while reading logs.
  82. Err chan error
  83. consumerOnce sync.Once
  84. consumerGone chan struct{}
  85. }
  86. // NewLogWatcher returns a new LogWatcher.
  87. func NewLogWatcher() *LogWatcher {
  88. return &LogWatcher{
  89. Msg: make(chan *Message, logWatcherBufferSize),
  90. Err: make(chan error, 1),
  91. consumerGone: make(chan struct{}),
  92. }
  93. }
  94. // ConsumerGone notifies that the logs consumer is gone.
  95. func (w *LogWatcher) ConsumerGone() {
  96. // only close if not already closed
  97. w.consumerOnce.Do(func() {
  98. close(w.consumerGone)
  99. })
  100. }
  101. // WatchConsumerGone returns a channel receiver that receives notification
  102. // when the log watcher consumer is gone.
  103. func (w *LogWatcher) WatchConsumerGone() <-chan struct{} {
  104. return w.consumerGone
  105. }
  106. // Capability defines the list of capabilities that a driver can implement
  107. // These capabilities are not required to be a logging driver, however do
  108. // determine how a logging driver can be used
  109. type Capability struct {
  110. // Determines if a log driver can read back logs
  111. ReadLogs bool
  112. }