logger.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. "sync"
  12. "time"
  13. "github.com/docker/docker/api/types/backend"
  14. "github.com/docker/docker/pkg/jsonlog"
  15. )
  16. // ErrReadLogsNotSupported is returned when the logger does not support reading logs.
  17. var ErrReadLogsNotSupported = errors.New("configured logging driver does not support reading")
  18. const (
  19. // TimeFormat is the time format used for timestamps sent to log readers.
  20. TimeFormat = jsonlog.RFC3339NanoFixed
  21. logWatcherBufferSize = 4096
  22. )
  23. var messagePool = &sync.Pool{New: func() interface{} { return &Message{Line: make([]byte, 0, 256)} }}
  24. // NewMessage returns a new message from the message sync.Pool
  25. func NewMessage() *Message {
  26. return messagePool.Get().(*Message)
  27. }
  28. // PutMessage puts the specified message back n the message pool.
  29. // The message fields are reset before putting into the pool.
  30. func PutMessage(msg *Message) {
  31. msg.reset()
  32. messagePool.Put(msg)
  33. }
  34. // Message is datastructure that represents piece of output produced by some
  35. // container. The Line member is a slice of an array whose contents can be
  36. // changed after a log driver's Log() method returns.
  37. //
  38. // Message is subtyped from backend.LogMessage because there is a lot of
  39. // internal complexity around the Message type that should not be exposed
  40. // to any package not explicitly importing the logger type.
  41. //
  42. // Any changes made to this struct must also be updated in the `reset` function
  43. type Message backend.LogMessage
  44. // reset sets the message back to default values
  45. // This is used when putting a message back into the message pool.
  46. // Any changes to the `Message` struct should be reflected here.
  47. func (m *Message) reset() {
  48. m.Line = m.Line[:0]
  49. m.Source = ""
  50. m.Attrs = nil
  51. m.Partial = false
  52. m.Err = nil
  53. }
  54. // AsLogMessage returns a pointer to the message as a pointer to
  55. // backend.LogMessage, which is an identical type with a different purpose
  56. func (m *Message) AsLogMessage() *backend.LogMessage {
  57. return (*backend.LogMessage)(m)
  58. }
  59. // Logger is the interface for docker logging drivers.
  60. type Logger interface {
  61. Log(*Message) error
  62. Name() string
  63. Close() error
  64. }
  65. // ReadConfig is the configuration passed into ReadLogs.
  66. type ReadConfig struct {
  67. Since time.Time
  68. Tail int
  69. Follow bool
  70. }
  71. // LogReader is the interface for reading log messages for loggers that support reading.
  72. type LogReader interface {
  73. // Read logs from underlying logging backend
  74. ReadLogs(ReadConfig) *LogWatcher
  75. }
  76. // LogWatcher is used when consuming logs read from the LogReader interface.
  77. type LogWatcher struct {
  78. // For sending log messages to a reader.
  79. Msg chan *Message
  80. // For sending error messages that occur while while reading logs.
  81. Err chan error
  82. closeOnce sync.Once
  83. closeNotifier chan struct{}
  84. }
  85. // NewLogWatcher returns a new LogWatcher.
  86. func NewLogWatcher() *LogWatcher {
  87. return &LogWatcher{
  88. Msg: make(chan *Message, logWatcherBufferSize),
  89. Err: make(chan error, 1),
  90. closeNotifier: make(chan struct{}),
  91. }
  92. }
  93. // Close notifies the underlying log reader to stop.
  94. func (w *LogWatcher) Close() {
  95. // only close if not already closed
  96. w.closeOnce.Do(func() {
  97. close(w.closeNotifier)
  98. })
  99. }
  100. // WatchClose returns a channel receiver that receives notification
  101. // when the watcher has been closed. This should only be called from
  102. // one goroutine.
  103. func (w *LogWatcher) WatchClose() <-chan struct{} {
  104. return w.closeNotifier
  105. }
  106. // Capability defines the list of capabilties 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. }