logger.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. // LogAttributes is used to hold the extra attributes available in the log message
  60. // Primarily used for converting the map type to string and sorting.
  61. // Imported here so it can be used internally with less refactoring
  62. type LogAttributes backend.LogAttributes
  63. // Logger is the interface for docker logging drivers.
  64. type Logger interface {
  65. Log(*Message) error
  66. Name() string
  67. Close() error
  68. }
  69. // ReadConfig is the configuration passed into ReadLogs.
  70. type ReadConfig struct {
  71. Since time.Time
  72. Tail int
  73. Follow bool
  74. }
  75. // LogReader is the interface for reading log messages for loggers that support reading.
  76. type LogReader interface {
  77. // Read logs from underlying logging backend
  78. ReadLogs(ReadConfig) *LogWatcher
  79. }
  80. // LogWatcher is used when consuming logs read from the LogReader interface.
  81. type LogWatcher struct {
  82. // For sending log messages to a reader.
  83. Msg chan *Message
  84. // For sending error messages that occur while while reading logs.
  85. Err chan error
  86. closeOnce sync.Once
  87. closeNotifier chan struct{}
  88. }
  89. // NewLogWatcher returns a new LogWatcher.
  90. func NewLogWatcher() *LogWatcher {
  91. return &LogWatcher{
  92. Msg: make(chan *Message, logWatcherBufferSize),
  93. Err: make(chan error, 1),
  94. closeNotifier: make(chan struct{}),
  95. }
  96. }
  97. // Close notifies the underlying log reader to stop.
  98. func (w *LogWatcher) Close() {
  99. // only close if not already closed
  100. w.closeOnce.Do(func() {
  101. close(w.closeNotifier)
  102. })
  103. }
  104. // WatchClose returns a channel receiver that receives notification
  105. // when the watcher has been closed. This should only be called from
  106. // one goroutine.
  107. func (w *LogWatcher) WatchClose() <-chan struct{} {
  108. return w.closeNotifier
  109. }
  110. // Capability defines the list of capabilties that a driver can implement
  111. // These capabilities are not required to be a logging driver, however do
  112. // determine how a logging driver can be used
  113. type Capability struct {
  114. // Determines if a log driver can read back logs
  115. ReadLogs bool
  116. }