github.com/clintkitson/docker@v1.9.1/daemon/logger/logger.go (about)

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