logger.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. "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 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. //
  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.Partial = false
  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 while reading logs.
  89. Err chan error
  90. closeOnce sync.Once
  91. closeNotifier chan struct{}
  92. }
  93. // NewLogWatcher returns a new LogWatcher.
  94. func NewLogWatcher() *LogWatcher {
  95. return &LogWatcher{
  96. Msg: make(chan *Message, logWatcherBufferSize),
  97. Err: make(chan error, 1),
  98. closeNotifier: make(chan struct{}),
  99. }
  100. }
  101. // Close notifies the underlying log reader to stop.
  102. func (w *LogWatcher) Close() {
  103. // only close if not already closed
  104. w.closeOnce.Do(func() {
  105. close(w.closeNotifier)
  106. })
  107. }
  108. // WatchClose returns a channel receiver that receives notification
  109. // when the watcher has been closed. This should only be called from
  110. // one goroutine.
  111. func (w *LogWatcher) WatchClose() <-chan struct{} {
  112. return w.closeNotifier
  113. }
  114. // Capability defines the list of capabilties that a driver can implement
  115. // These capabilities are not required to be a logging driver, however do
  116. // determine how a logging driver can be used
  117. type Capability struct {
  118. // Determines if a log driver can read back logs
  119. ReadLogs bool
  120. }
  121. // MarshalFunc is a func that marshals a message into an arbitrary format
  122. type MarshalFunc func(*Message) ([]byte, error)