Merge pull request #12362 from ahmetalpbalkan/logs/since
Add --since argument to docker logs cmd
This commit is contained in:
commit
e0cff4977e
3 changed files with 29 additions and 4 deletions
|
@ -32,16 +32,20 @@ func (jl *JSONLog) Reset() {
|
|||
jl.Created = time.Time{}
|
||||
}
|
||||
|
||||
func WriteLog(src io.Reader, dst io.Writer, format string) error {
|
||||
func WriteLog(src io.Reader, dst io.Writer, format string, since time.Time) error {
|
||||
dec := json.NewDecoder(src)
|
||||
l := &JSONLog{}
|
||||
for {
|
||||
l.Reset()
|
||||
if err := dec.Decode(l); err == io.EOF {
|
||||
return nil
|
||||
} else if err != nil {
|
||||
logrus.Printf("Error streaming logs: %s", err)
|
||||
return err
|
||||
}
|
||||
if !since.IsZero() && l.Created.Before(since) {
|
||||
continue
|
||||
}
|
||||
line, err := l.Format(format)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -49,6 +53,5 @@ func WriteLog(src io.Reader, dst io.Writer, format string) error {
|
|||
if _, err := io.WriteString(dst, line); err != nil {
|
||||
return err
|
||||
}
|
||||
l.Reset()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ func TestWriteLog(t *testing.T) {
|
|||
}
|
||||
w := bytes.NewBuffer(nil)
|
||||
format := timeutils.RFC3339NanoFixed
|
||||
if err := WriteLog(&buf, w, format); err != nil {
|
||||
if err := WriteLog(&buf, w, format, time.Time{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
res := w.String()
|
||||
|
@ -52,7 +52,7 @@ func BenchmarkWriteLog(b *testing.B) {
|
|||
b.SetBytes(int64(r.Len()))
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
if err := WriteLog(r, w, format); err != nil {
|
||||
if err := WriteLog(r, w, format, time.Time{}); err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
b.StopTimer()
|
||||
|
|
22
timeutils/utils.go
Normal file
22
timeutils/utils.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package timeutils
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// GetTimestamp tries to parse given string as RFC3339 time
|
||||
// or Unix timestamp, if successful returns a Unix timestamp
|
||||
// as string otherwise returns value back.
|
||||
func GetTimestamp(value string) string {
|
||||
format := RFC3339NanoFixed
|
||||
loc := time.FixedZone(time.Now().Zone())
|
||||
if len(value) < len(format) {
|
||||
format = format[:len(value)]
|
||||
}
|
||||
t, err := time.ParseInLocation(format, value, loc)
|
||||
if err != nil {
|
||||
return value
|
||||
}
|
||||
return strconv.FormatInt(t.Unix(), 10)
|
||||
}
|
Loading…
Reference in a new issue