2015-07-21 22:26:52 +00:00
|
|
|
// Package logger defines interfaces that logger drivers implement to
|
|
|
|
// log messages.
|
|
|
|
//
|
|
|
|
// The other half of a logger driver is the implementation of the
|
|
|
|
// factory, which holds the contextual instance information that
|
|
|
|
// allows multiple loggers of the same type to perform different
|
|
|
|
// actions, such as logging to different locations.
|
2015-02-03 23:41:26 +00:00
|
|
|
package logger
|
|
|
|
|
2015-04-09 04:23:30 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
2016-04-08 16:15:08 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2015-04-09 04:23:30 +00:00
|
|
|
"time"
|
2015-07-03 13:50:06 +00:00
|
|
|
|
2015-12-15 19:49:41 +00:00
|
|
|
"github.com/docker/docker/pkg/jsonlog"
|
2015-04-09 04:23:30 +00:00
|
|
|
)
|
|
|
|
|
2015-07-21 22:26:52 +00:00
|
|
|
// ErrReadLogsNotSupported is returned when the logger does not support reading logs.
|
2015-07-03 13:50:06 +00:00
|
|
|
var ErrReadLogsNotSupported = errors.New("configured logging reader does not support reading")
|
|
|
|
|
|
|
|
const (
|
2015-07-21 22:26:52 +00:00
|
|
|
// TimeFormat is the time format used for timestamps sent to log readers.
|
2015-12-15 19:49:41 +00:00
|
|
|
TimeFormat = jsonlog.RFC3339NanoFixed
|
2015-07-03 13:50:06 +00:00
|
|
|
logWatcherBufferSize = 4096
|
|
|
|
)
|
2015-02-03 23:41:26 +00:00
|
|
|
|
2015-07-21 22:26:52 +00:00
|
|
|
// Message is datastructure that represents record from some container.
|
2015-02-03 23:41:26 +00:00
|
|
|
type Message struct {
|
2016-05-31 19:46:55 +00:00
|
|
|
Line []byte
|
|
|
|
Source string
|
|
|
|
Timestamp time.Time
|
|
|
|
Attrs LogAttributes
|
2016-04-08 16:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LogAttributes is used to hold the extra attributes available in the log message
|
|
|
|
// Primarily used for converting the map type to string and sorting.
|
|
|
|
type LogAttributes map[string]string
|
|
|
|
type byKey []string
|
|
|
|
|
|
|
|
func (s byKey) Len() int { return len(s) }
|
|
|
|
func (s byKey) Less(i, j int) bool {
|
|
|
|
keyI := strings.Split(s[i], "=")
|
|
|
|
keyJ := strings.Split(s[j], "=")
|
|
|
|
return keyI[0] < keyJ[0]
|
|
|
|
}
|
|
|
|
func (s byKey) Swap(i, j int) {
|
|
|
|
s[i], s[j] = s[j], s[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a LogAttributes) String() string {
|
|
|
|
var ss byKey
|
|
|
|
for k, v := range a {
|
|
|
|
ss = append(ss, k+"="+v)
|
|
|
|
}
|
|
|
|
sort.Sort(ss)
|
|
|
|
return strings.Join(ss, ",")
|
2015-02-03 23:41:26 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 22:26:52 +00:00
|
|
|
// Logger is the interface for docker logging drivers.
|
2015-02-03 23:41:26 +00:00
|
|
|
type Logger interface {
|
|
|
|
Log(*Message) error
|
|
|
|
Name() string
|
|
|
|
Close() error
|
2015-07-01 00:40:13 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 22:26:52 +00:00
|
|
|
// ReadConfig is the configuration passed into ReadLogs.
|
2015-07-03 13:50:06 +00:00
|
|
|
type ReadConfig struct {
|
|
|
|
Since time.Time
|
|
|
|
Tail int
|
|
|
|
Follow bool
|
|
|
|
}
|
|
|
|
|
2015-07-21 22:26:52 +00:00
|
|
|
// LogReader is the interface for reading log messages for loggers that support reading.
|
2015-07-03 13:50:06 +00:00
|
|
|
type LogReader interface {
|
|
|
|
// Read logs from underlying logging backend
|
|
|
|
ReadLogs(ReadConfig) *LogWatcher
|
|
|
|
}
|
|
|
|
|
2015-07-21 22:26:52 +00:00
|
|
|
// LogWatcher is used when consuming logs read from the LogReader interface.
|
2015-07-03 13:50:06 +00:00
|
|
|
type LogWatcher struct {
|
2015-07-21 22:26:52 +00:00
|
|
|
// For sending log messages to a reader.
|
2015-07-03 13:50:06 +00:00
|
|
|
Msg chan *Message
|
2015-07-21 22:26:52 +00:00
|
|
|
// For sending error messages that occur while while reading logs.
|
2015-07-03 13:50:06 +00:00
|
|
|
Err chan error
|
|
|
|
closeNotifier chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLogWatcher returns a new LogWatcher.
|
|
|
|
func NewLogWatcher() *LogWatcher {
|
|
|
|
return &LogWatcher{
|
|
|
|
Msg: make(chan *Message, logWatcherBufferSize),
|
|
|
|
Err: make(chan error, 1),
|
|
|
|
closeNotifier: make(chan struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-21 22:26:52 +00:00
|
|
|
// Close notifies the underlying log reader to stop.
|
2015-07-03 13:50:06 +00:00
|
|
|
func (w *LogWatcher) Close() {
|
2015-07-31 20:49:07 +00:00
|
|
|
// only close if not already closed
|
|
|
|
select {
|
|
|
|
case <-w.closeNotifier:
|
|
|
|
default:
|
|
|
|
close(w.closeNotifier)
|
|
|
|
}
|
2015-07-03 13:50:06 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 22:26:52 +00:00
|
|
|
// WatchClose returns a channel receiver that receives notification
|
|
|
|
// when the watcher has been closed. This should only be called from
|
|
|
|
// one goroutine.
|
2015-07-03 13:50:06 +00:00
|
|
|
func (w *LogWatcher) WatchClose() <-chan struct{} {
|
|
|
|
return w.closeNotifier
|
2015-02-03 23:41:26 +00:00
|
|
|
}
|