github.com/michael-k/docker@v1.7.0-rc2/pkg/jsonlog/jsonlog.go (about) 1 package jsonlog 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io" 7 "time" 8 ) 9 10 type JSONLog struct { 11 Log string `json:"log,omitempty"` 12 Stream string `json:"stream,omitempty"` 13 Created time.Time `json:"time"` 14 } 15 16 func (jl *JSONLog) Format(format string) (string, error) { 17 if format == "" { 18 return jl.Log, nil 19 } 20 if format == "json" { 21 m, err := json.Marshal(jl) 22 return string(m), err 23 } 24 return fmt.Sprintf("%s %s", jl.Created.Format(format), jl.Log), nil 25 } 26 27 func (jl *JSONLog) Reset() { 28 jl.Log = "" 29 jl.Stream = "" 30 jl.Created = time.Time{} 31 } 32 33 func WriteLog(src io.Reader, dst io.Writer, format string, since time.Time) error { 34 dec := json.NewDecoder(src) 35 l := &JSONLog{} 36 for { 37 l.Reset() 38 if err := dec.Decode(l); err != nil { 39 if err == io.EOF { 40 return nil 41 } 42 return err 43 } 44 if !since.IsZero() && l.Created.Before(since) { 45 continue 46 } 47 48 line, err := l.Format(format) 49 if err != nil { 50 return err 51 } 52 if _, err := io.WriteString(dst, line); err != nil { 53 return err 54 } 55 } 56 }