Skip to content

Commit 1b1f783

Browse files
authored
Merge pull request docker#10076 from ndeloof/timestamp
introduce --timestamp option on compose up
2 parents 1cb5536 + 84ea395 commit 1b1f783

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed

cmd/compose/logs.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ import (
2020
"context"
2121
"os"
2222

23-
"github.com/docker/compose/v2/cmd/formatter"
24-
2523
"github.com/spf13/cobra"
2624

25+
"github.com/docker/compose/v2/cmd/formatter"
2726
"github.com/docker/compose/v2/pkg/api"
2827
)
2928

@@ -67,7 +66,7 @@ func runLogs(ctx context.Context, backend api.Service, opts logsOptions, service
6766
if err != nil {
6867
return err
6968
}
70-
consumer := formatter.NewLogConsumer(ctx, os.Stdout, os.Stderr, !opts.noColor, !opts.noPrefix)
69+
consumer := formatter.NewLogConsumer(ctx, os.Stdout, os.Stderr, !opts.noColor, !opts.noPrefix, false)
7170
return backend.Logs(ctx, name, consumer, api.LogOptions{
7271
Project: project,
7372
Services: services,

cmd/compose/up.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type upOptions struct {
4949
noPrefix bool
5050
attachDependencies bool
5151
attach []string
52+
timestamp bool
5253
wait bool
5354
}
5455

@@ -126,6 +127,7 @@ func upCommand(p *projectOptions, backend api.Service) *cobra.Command {
126127
flags.BoolVar(&up.cascadeStop, "abort-on-container-exit", false, "Stops all containers if any container was stopped. Incompatible with -d")
127128
flags.StringVar(&up.exitCodeFrom, "exit-code-from", "", "Return the exit code of the selected service container. Implies --abort-on-container-exit")
128129
flags.IntVarP(&create.timeout, "timeout", "t", 10, "Use this timeout in seconds for container shutdown when attached or when containers are already running.")
130+
flags.BoolVar(&up.timestamp, "timestamps", false, "Show timestamps.")
129131
flags.BoolVar(&up.noDeps, "no-deps", false, "Don't start linked services.")
130132
flags.BoolVar(&create.recreateDeps, "always-recreate-deps", false, "Recreate dependent containers. Incompatible with --no-recreate.")
131133
flags.BoolVarP(&create.noInherit, "renew-anon-volumes", "V", false, "Recreate anonymous volumes instead of retrieving data from the previous containers.")
@@ -176,7 +178,7 @@ func runUp(ctx context.Context, backend api.Service, createOptions createOptions
176178

177179
var consumer api.LogConsumer
178180
if !upOptions.Detach {
179-
consumer = formatter.NewLogConsumer(ctx, os.Stdout, os.Stderr, !upOptions.noColor, !upOptions.noPrefix)
181+
consumer = formatter.NewLogConsumer(ctx, os.Stdout, os.Stderr, !upOptions.noColor, !upOptions.noPrefix, upOptions.timestamp)
180182
}
181183

182184
attachTo := services

cmd/formatter/logs.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ import (
2323
"strconv"
2424
"strings"
2525
"sync"
26+
"time"
2627

2728
"github.com/docker/compose/v2/pkg/api"
29+
"github.com/docker/docker/pkg/jsonmessage"
2830
)
2931

3032
// LogConsumer consume logs from services and format them
@@ -36,10 +38,11 @@ type logConsumer struct {
3638
stderr io.Writer
3739
color bool
3840
prefix bool
41+
timestamp bool
3942
}
4043

4144
// NewLogConsumer creates a new LogConsumer
42-
func NewLogConsumer(ctx context.Context, stdout, stderr io.Writer, color bool, prefix bool) api.LogConsumer {
45+
func NewLogConsumer(ctx context.Context, stdout, stderr io.Writer, color, prefix, timestamp bool) api.LogConsumer {
4346
return &logConsumer{
4447
ctx: ctx,
4548
presenters: sync.Map{},
@@ -48,6 +51,7 @@ func NewLogConsumer(ctx context.Context, stdout, stderr io.Writer, color bool, p
4851
stderr: stderr,
4952
color: color,
5053
prefix: prefix,
54+
timestamp: timestamp,
5155
}
5256
}
5357

@@ -99,8 +103,13 @@ func (l *logConsumer) write(w io.Writer, container, message string) {
99103
return
100104
}
101105
p := l.getPresenter(container)
106+
timestamp := time.Now().Format(jsonmessage.RFC3339NanoFixed)
102107
for _, line := range strings.Split(message, "\n") {
103-
fmt.Fprintf(w, "%s%s\n", p.prefix, line)
108+
if l.timestamp {
109+
fmt.Fprintf(w, "%s%s%s\n", p.prefix, timestamp, line)
110+
} else {
111+
fmt.Fprintf(w, "%s%s\n", p.prefix, line)
112+
}
104113
}
105114
}
106115

docs/reference/compose_up.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Create and start containers
2727
| `-V`, `--renew-anon-volumes` | | | Recreate anonymous volumes instead of retrieving data from the previous containers. |
2828
| `--scale` | `stringArray` | | Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present. |
2929
| `-t`, `--timeout` | `int` | `10` | Use this timeout in seconds for container shutdown when attached or when containers are already running. |
30+
| `--timestamps` | | | Show timestamps. |
3031
| `--wait` | | | Wait for services to be running\|healthy. Implies detached mode. |
3132

3233

docs/reference/docker_compose_up.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,16 @@ options:
230230
experimentalcli: false
231231
kubernetes: false
232232
swarm: false
233+
- option: timestamps
234+
value_type: bool
235+
default_value: "false"
236+
description: Show timestamps.
237+
deprecated: false
238+
hidden: false
239+
experimental: false
240+
experimentalcli: false
241+
kubernetes: false
242+
swarm: false
233243
- option: wait
234244
value_type: bool
235245
default_value: "false"

0 commit comments

Comments
 (0)