Add the names flag for pod logs

Fixes containers#13261

Signed-off-by: Xueyuan Chen <X.Chen-47@student.tudelft.nl>
This commit is contained in:
Xueyuan Chen
2022-02-26 23:07:25 +01:00
committed by Xueyuan Chen
parent c39dffe83d
commit 40c6192e9e
4 changed files with 42 additions and 3 deletions

View File

@ -87,6 +87,7 @@ func logsFlags(cmd *cobra.Command) {
flags.Int64Var(&logsPodOptions.Tail, tailFlagName, -1, "Output the specified number of LINES at the end of the logs.") flags.Int64Var(&logsPodOptions.Tail, tailFlagName, -1, "Output the specified number of LINES at the end of the logs.")
_ = cmd.RegisterFlagCompletionFunc(tailFlagName, completion.AutocompleteNone) _ = cmd.RegisterFlagCompletionFunc(tailFlagName, completion.AutocompleteNone)
flags.BoolVarP(&logsPodOptions.Names, "names", "n", false, "Output container names instead of container IDs in the log")
flags.BoolVarP(&logsPodOptions.Timestamps, "timestamps", "t", false, "Output the timestamps in the log") flags.BoolVarP(&logsPodOptions.Timestamps, "timestamps", "t", false, "Output the timestamps in the log")
flags.SetInterspersed(false) flags.SetInterspersed(false)
_ = flags.MarkHidden("details") _ = flags.MarkHidden("details")

View File

@ -28,6 +28,10 @@ chance that the log file will be removed before `podman pod logs` reads the fina
Instead of providing the pod name or id, get logs of the last created pod. (This option is not available with the remote Podman client, including Mac and Windows (excluding WSL2) machines) Instead of providing the pod name or id, get logs of the last created pod. (This option is not available with the remote Podman client, including Mac and Windows (excluding WSL2) machines)
#### **--names**, **-n**
Output the container names instead of the container IDs in the log.
#### **--since**=*TIMESTAMP* #### **--since**=*TIMESTAMP*
Show logs since TIMESTAMP. The --since option can be Unix timestamps, date formatted timestamps, or Go duration Show logs since TIMESTAMP. The --since option can be Unix timestamps, date formatted timestamps, or Go duration

View File

@ -1,5 +1,5 @@
//+build linux //go:build linux && systemd
//+build systemd // +build linux,systemd
package libpod package libpod
@ -235,6 +235,9 @@ func (c *Container) readFromJournal(ctx context.Context, options *logs.LogOption
logrus.Errorf("Failed parse log line: %v", err) logrus.Errorf("Failed parse log line: %v", err)
return return
} }
if options.UseName {
logLine.CName = c.Name()
}
if doTail { if doTail {
tailQueue = append(tailQueue, logLine) tailQueue = append(tailQueue, logLine)
continue continue

View File

@ -37,7 +37,9 @@ var _ = Describe("Podman logs", func() {
} }
podmanTest = PodmanTestCreate(tempdir) podmanTest = PodmanTestCreate(tempdir)
podmanTest.Setup() podmanTest.Setup()
podmanTest.SeedImages() if err := podmanTest.SeedImages(); err != nil {
os.Exit(1)
}
}) })
AfterEach(func() { AfterEach(func() {
@ -412,4 +414,33 @@ var _ = Describe("Podman logs", func() {
logs.WaitWithDefaultTimeout() logs.WaitWithDefaultTimeout()
Expect(logs).To(Not(Exit(0))) Expect(logs).To(Not(Exit(0)))
}) })
It("podman pod logs with container names", func() {
SkipIfRemote("Remote can only process one container at a time")
SkipIfInContainer("journalctl inside a container doesn't work correctly")
podName := "testPod"
containerName1 := "container1"
containerName2 := "container2"
testPod := podmanTest.Podman([]string{"pod", "create", fmt.Sprintf("--name=%s", podName)})
testPod.WaitWithDefaultTimeout()
Expect(testPod).To(Exit(0))
log1 := podmanTest.Podman([]string{"run", "--name", containerName1, "-d", "--pod", podName, BB, "/bin/sh", "-c", "echo log1"})
log1.WaitWithDefaultTimeout()
Expect(log1).To(Exit(0))
log2 := podmanTest.Podman([]string{"run", "--name", containerName2, "-d", "--pod", podName, BB, "/bin/sh", "-c", "echo log2"})
log2.WaitWithDefaultTimeout()
Expect(log2).To(Exit(0))
results := podmanTest.Podman([]string{"pod", "logs", "--names", podName})
results.WaitWithDefaultTimeout()
Expect(results).To(Exit(0))
output := results.OutputToStringArray()
Expect(output).To(HaveLen(2))
Expect(output).To(ContainElement(ContainSubstring(containerName1)))
Expect(output).To(ContainElement(ContainSubstring(containerName2)))
})
}) })