Add --since argument to docker logs cmd · SourceCode/docker@cb9a6b9 · GitHub
Skip to content

Commit cb9a6b9

Browse files
committed
Add --since argument to docker logs cmd
Added --since argument to `docker logs` command. Accept unix timestamps and shows logs only created after the specified date. Default value is 0 and passing default value or not specifying the value in the request causes parameter to be ignored (behavior prior to this change). Signed-off-by: Ahmet Alp Balkan <ahmetalpbalkan@gmail.com>
1 parent 340fd14 commit cb9a6b9

15 files changed

Lines changed: 154 additions & 25 deletions

File tree

api/client/events.go

Lines changed: 2 additions & 16 deletions

api/client/logs.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/docker/docker/api/types"
99
flag "github.com/docker/docker/pkg/mflag"
10+
"github.com/docker/docker/pkg/timeutils"
1011
)
1112

1213
// CmdLogs fetches the logs of a given container.
@@ -16,6 +17,7 @@ func (cli *DockerCli) CmdLogs(args ...string) error {
1617
var (
1718
cmd = cli.Subcmd("logs", "CONTAINER", "Fetch the logs of a container", true)
1819
follow = cmd.Bool([]string{"f", "-follow"}, false, "Follow log output")
20+
since = cmd.String([]string{"-since"}, "", "Show logs since timestamp")
1921
times = cmd.Bool([]string{"t", "-timestamps"}, false, "Show timestamps")
2022
tail = cmd.String([]string{"-tail"}, "all", "Number of lines to show from the end of the logs")
2123
)
@@ -43,6 +45,10 @@ func (cli *DockerCli) CmdLogs(args ...string) error {
4345
v.Set("stdout", "1")
4446
v.Set("stderr", "1")
4547

48+
if *since != "" {
49+
v.Set("since", timeutils.GetTimestamp(*since))
50+
}
51+
4652
if *times {
4753
v.Set("timestamps", "1")
4854
}

api/server/server.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,9 +594,19 @@ func (s *Server) getContainersLogs(version version.Version, w http.ResponseWrite
594594
return fmt.Errorf("Bad parameters: you must choose at least one stream")
595595
}
596596

597+
var since time.Time
598+
if r.Form.Get("since") != "" {
599+
s, err := strconv.ParseInt(r.Form.Get("since"), 10, 64)
600+
if err != nil {
601+
return err
602+
}
603+
since = time.Unix(s, 0)
604+
}
605+
597606
logsConfig := &daemon.ContainerLogsConfig{
598607
Follow: boolValue(r, "follow"),
599608
Timestamps: boolValue(r, "timestamps"),
609+
Since: since,
600610
Tail: r.Form.Get("tail"),
601611
UseStdout: stdout,
602612
UseStderr: stderr,

contrib/completion/bash/docker

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ _docker_logs() {
593593

594594
case "$cur" in
595595
-*)
596-
COMPREPLY=( $( compgen -W "--follow -f --help --tail --timestamps -t" -- "$cur" ) )
596+
COMPREPLY=( $( compgen -W "--follow -f --help --since --tail --timestamps -t" -- "$cur" ) )
597597
;;
598598
*)
599599
local counter=$(__docker_pos_first_nonflag '--tail')

contrib/completion/fish/docker.fish

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ complete -c docker -f -n '__fish_docker_no_subcommand' -a logs -d 'Fetch the log
233233
complete -c docker -A -f -n '__fish_seen_subcommand_from logs' -s f -l follow -d 'Follow log output'
234234
complete -c docker -A -f -n '__fish_seen_subcommand_from logs' -l help -d 'Print usage'
235235
complete -c docker -A -f -n '__fish_seen_subcommand_from logs' -s t -l timestamps -d 'Show timestamps'
236+
complete -c docker -A -f -n '__fish_seen_subcommand_from logs' -l since -d 'Show logs since timestamp'
236237
complete -c docker -A -f -n '__fish_seen_subcommand_from logs' -l tail -d 'Output the specified number of lines at the end of logs (defaults to all logs)'
237238
complete -c docker -A -f -n '__fish_seen_subcommand_from logs' -a '(__fish_print_docker_containers running)' -d "Container"
238239

contrib/completion/zsh/_docker

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ __docker_subcommand () {
305305
(logs)
306306
_arguments \
307307
{-f,--follow}'[Follow log output]' \
308+
'-s,--since[Show logs since timestamp]' \
308309
{-t,--timestamps}'[Show timestamps]' \
309310
'--tail=-[Output the last K lines]:lines:(1 10 20 50 all)' \
310311
'*:containers:__docker_containers'

daemon/logs.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"strconv"
1010
"sync"
11+
"time"
1112

1213
"github.com/Sirupsen/logrus"
1314
"github.com/docker/docker/pkg/jsonlog"
@@ -19,6 +20,7 @@ import (
1920
type ContainerLogsConfig struct {
2021
Follow, Timestamps bool
2122
Tail string
23+
Since time.Time
2224
UseStdout, UseStderr bool
2325
OutStream io.Writer
2426
}
@@ -88,6 +90,7 @@ func (daemon *Daemon) ContainerLogs(name string, config *ContainerLogsConfig) er
8890
lines = -1
8991
}
9092
}
93+
9194
if lines != 0 {
9295
if lines > 0 {
9396
f := cLog.(*os.File)
@@ -101,16 +104,21 @@ func (daemon *Daemon) ContainerLogs(name string, config *ContainerLogsConfig) er
101104
}
102105
cLog = tmp
103106
}
107+
104108
dec := json.NewDecoder(cLog)
105109
l := &jsonlog.JSONLog{}
106110
for {
111+
l.Reset()
107112
if err := dec.Decode(l); err == io.EOF {
108113
break
109114
} else if err != nil {
110115
logrus.Errorf("Error streaming logs: %s", err)
111116
break
112117
}
113118
logLine := l.Log
119+
if !config.Since.IsZero() && l.Created.Before(config.Since) {
120+
continue
121+
}
114122
if config.Timestamps {
115123
// format can be "" or time format, so here can't be error
116124
logLine, _ = l.Format(format)
@@ -121,7 +129,6 @@ func (daemon *Daemon) ContainerLogs(name string, config *ContainerLogsConfig) er
121129
if l.Stream == "stderr" && config.UseStderr {
122130
io.WriteString(errStream, logLine)
123131
}
124-
l.Reset()
125132
}
126133
}
127134
}
@@ -139,7 +146,7 @@ func (daemon *Daemon) ContainerLogs(name string, config *ContainerLogsConfig) er
139146
stdoutPipe := container.StdoutLogPipe()
140147
defer stdoutPipe.Close()
141148
go func() {
142-
errors <- jsonlog.WriteLog(stdoutPipe, outStream, format)
149+
errors <- jsonlog.WriteLog(stdoutPipe, outStream, format, config.Since)
143150
wg.Done()
144151
}()
145152
}
@@ -148,7 +155,7 @@ func (daemon *Daemon) ContainerLogs(name string, config *ContainerLogsConfig) er
148155
stderrPipe := container.StderrLogPipe()
149156
defer stderrPipe.Close()
150157
go func() {
151-
errors <- jsonlog.WriteLog(stderrPipe, errStream, format)
158+
errors <- jsonlog.WriteLog(stderrPipe, errStream, format, config.Since)
152159
wg.Done()
153160
}()
154161
}

docs/man/docker-logs.1.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ docker-logs - Fetch the logs of a container
88
**docker logs**
99
[**-f**|**--follow**[=*false*]]
1010
[**--help**]
11+
[**--since**[=*SINCE*]]
1112
[**-t**|**--timestamps**[=*false*]]
1213
[**--tail**[=*"all"*]]
1314
CONTAINER
@@ -31,6 +32,9 @@ then continue streaming new output from the container’s stdout and stderr.
3132
**-f**, **--follow**=*true*|*false*
3233
Follow log output. The default is *false*.
3334

35+
**--since**=""
36+
Show logs since timestamp
37+
3438
**-t**, **--timestamps**=*true*|*false*
3539
Show timestamps. The default is *false*.
3640

@@ -42,3 +46,4 @@ April 2014, Originally compiled by William Henry (whenry at redhat dot com)
4246
based on docker.com source material and internal work.
4347
June 2014, updated by Sven Dowideit <SvenDowideit@home.org.au>
4448
July 2014, updated by Sven Dowideit <SvenDowideit@home.org.au>
49+
April 2015, updated by Ahmet Alp Balkan <ahmetalpbalkan@gmail.com>

docs/sources/reference/api/docker_remote_api.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ You can still call an old version of the API using
5252
You can now supply a `stream` bool to get only one set of stats and
5353
disconnect
5454

55+
`GET /containers(id)/logs`
56+
57+
**New!**
58+
59+
This endpoint now accepts a `since` timestamp parameter.
60+
5561
## v1.18
5662

5763
### Full documentation

docs/sources/reference/api/docker_remote_api_v1.19.md

Lines changed: 3 additions & 1 deletion

0 commit comments

Comments
 (0)