Skip to content

Commit 4497522

Browse files
authored
Merge pull request #556 from clarklee92/print-the-previous-log
Print the previous log of the container(just like kubectl)
2 parents 1da46ca + 0e13d35 commit 4497522

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

cmd/crictl/logs.go

+14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"fmt"
2222
"os"
23+
"strings"
2324
"time"
2425

2526
timetypes "github.com/docker/docker/api/types/time"
@@ -40,6 +41,10 @@ var logsCommand = cli.Command{
4041
Name: "follow, f",
4142
Usage: "Follow log output",
4243
},
44+
cli.BoolFlag{
45+
Name: "previous, p",
46+
Usage: "Print the logs for the previous instance of the container in a pod if it exists",
47+
},
4348
cli.Int64Flag{
4449
Name: "tail",
4550
Value: -1,
@@ -77,6 +82,7 @@ var logsCommand = cli.Command{
7782
return err
7883
}
7984
timestamp := ctx.Bool("timestamps")
85+
previous := ctx.Bool("previous")
8086
logOptions := logs.NewLogOptions(&v1.PodLogOptions{
8187
Follow: ctx.Bool("follow"),
8288
TailLines: &tailLines,
@@ -92,6 +98,14 @@ var logsCommand = cli.Command{
9298
if logPath == "" {
9399
return fmt.Errorf("The container has not set log path")
94100
}
101+
if previous {
102+
containerAttempt := status.GetMetadata().Attempt
103+
if containerAttempt == uint32(0) {
104+
return fmt.Errorf("Previous terminated container %s not found", status.GetMetadata().Name)
105+
}
106+
logPath = fmt.Sprintf("%s%s%s", logPath[:strings.LastIndex(logPath, "/")+1], fmt.Sprint(containerAttempt-1),
107+
logPath[strings.LastIndex(logPath, "."):])
108+
}
95109
return logs.ReadLogs(context.Background(), logPath, status.GetId(), logOptions, runtimeService, os.Stdout, os.Stderr)
96110
},
97111
}

0 commit comments

Comments
 (0)