github.com/mssola/docker@v1.8.1/daemon/logger/logger.go (about)

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