logger.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. "time"
  12. "github.com/docker/docker/pkg/jsonlog"
  13. )
  14. // ErrReadLogsNotSupported is returned when the logger does not support reading logs.
  15. var ErrReadLogsNotSupported = errors.New("configured logging reader does not support reading")
  16. const (
  17. // TimeFormat is the time format used for timestamps sent to log readers.
  18. TimeFormat = jsonlog.RFC3339NanoFixed
  19. logWatcherBufferSize = 4096
  20. )
  21. // Message is datastructure that represents record from some container.
  22. type Message struct {
  23. ContainerID string
  24. Line []byte
  25. Source string
  26. Timestamp time.Time
  27. }
  28. // Logger is the interface for docker logging drivers.
  29. type Logger interface {
  30. Log(*Message) error
  31. Name() string
  32. Close() error
  33. }
  34. // ReadConfig is the configuration passed into ReadLogs.
  35. type ReadConfig struct {
  36. Since time.Time
  37. Tail int
  38. Follow bool
  39. }
  40. // LogReader is the interface for reading log messages for loggers that support reading.
  41. type LogReader interface {
  42. // Read logs from underlying logging backend
  43. ReadLogs(ReadConfig) *LogWatcher
  44. }
  45. // LogWatcher is used when consuming logs read from the LogReader interface.
  46. type LogWatcher struct {
  47. // For sending log messages to a reader.
  48. Msg chan *Message
  49. // For sending error messages that occur while while reading logs.
  50. Err chan error
  51. closeNotifier chan struct{}
  52. }
  53. // NewLogWatcher returns a new LogWatcher.
  54. func NewLogWatcher() *LogWatcher {
  55. return &LogWatcher{
  56. Msg: make(chan *Message, logWatcherBufferSize),
  57. Err: make(chan error, 1),
  58. closeNotifier: make(chan struct{}),
  59. }
  60. }
  61. // Close notifies the underlying log reader to stop.
  62. func (w *LogWatcher) Close() {
  63. // only close if not already closed
  64. select {
  65. case <-w.closeNotifier:
  66. default:
  67. close(w.closeNotifier)
  68. }
  69. }
  70. // WatchClose returns a channel receiver that receives notification
  71. // when the watcher has been closed. This should only be called from
  72. // one goroutine.
  73. func (w *LogWatcher) WatchClose() <-chan struct{} {
  74. return w.closeNotifier
  75. }