github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/daemon/logger/jsonfilelog/read.go (about) 1 package jsonfilelog // import "github.com/docker/docker/daemon/logger/jsonfilelog" 2 3 import ( 4 "context" 5 "encoding/json" 6 "io" 7 8 "github.com/docker/docker/api/types/backend" 9 "github.com/docker/docker/daemon/logger" 10 "github.com/docker/docker/daemon/logger/jsonfilelog/jsonlog" 11 "github.com/docker/docker/daemon/logger/loggerutils" 12 "github.com/docker/docker/pkg/tailfile" 13 ) 14 15 // ReadLogs implements the logger's LogReader interface for the logs 16 // created by this driver. 17 func (l *JSONFileLogger) ReadLogs(config logger.ReadConfig) *logger.LogWatcher { 18 return l.writer.ReadLogs(config) 19 } 20 21 func decodeLogLine(dec *json.Decoder, l *jsonlog.JSONLog) (*logger.Message, error) { 22 l.Reset() 23 if err := dec.Decode(l); err != nil { 24 return nil, err 25 } 26 27 var attrs []backend.LogAttr 28 if len(l.Attrs) != 0 { 29 attrs = make([]backend.LogAttr, 0, len(l.Attrs)) 30 for k, v := range l.Attrs { 31 attrs = append(attrs, backend.LogAttr{Key: k, Value: v}) 32 } 33 } 34 msg := &logger.Message{ 35 Source: l.Stream, 36 Timestamp: l.Created, 37 Line: []byte(l.Log), 38 Attrs: attrs, 39 } 40 return msg, nil 41 } 42 43 type decoder struct { 44 rdr io.Reader 45 dec *json.Decoder 46 jl *jsonlog.JSONLog 47 } 48 49 func (d *decoder) Reset(rdr io.Reader) { 50 d.rdr = rdr 51 d.dec = nil 52 if d.jl != nil { 53 d.jl.Reset() 54 } 55 } 56 57 func (d *decoder) Close() { 58 d.dec = nil 59 d.rdr = nil 60 d.jl = nil 61 } 62 63 func (d *decoder) Decode() (msg *logger.Message, err error) { 64 if d.dec == nil { 65 d.dec = json.NewDecoder(d.rdr) 66 } 67 if d.jl == nil { 68 d.jl = &jsonlog.JSONLog{} 69 } 70 return decodeLogLine(d.dec, d.jl) 71 } 72 73 // decodeFunc is used to create a decoder for the log file reader 74 func decodeFunc(rdr io.Reader) loggerutils.Decoder { 75 return &decoder{ 76 rdr: rdr, 77 dec: nil, 78 jl: nil, 79 } 80 } 81 82 func getTailReader(ctx context.Context, r loggerutils.SizeReaderAt, req int) (io.Reader, int, error) { 83 return tailfile.NewTailReader(ctx, r, req) 84 }