How to get kubectl logs to output pod name alongside logs?

11/23/2018

I'm using the handy kubectl logs -l label=value command to get log from all my pods matching a label. I want to see which pod outputted what log, but only the log text is displayed. Is there a way to control the log format, or a command argument which will let me do this?

-- Dhiraj Gupta
kubectl
kubernetes

4 Answers

3/11/2020

kubectl now has a --prefix option that allows you to prefix the pod name before the log message.

-- Erik L
Source: StackOverflow

11/23/2018

Use the awesome kubetail script

-- Nima Hashemi
Source: StackOverflow

12/7/2018

I use stern to show logs from all pods https://github.com/wercker/stern.

-- Bal Chua
Source: StackOverflow

11/23/2018

As simple as this:

for pod in $(kubectl get po -l key=value -oname); do echo $pod; kubectl logs $pod; done;

this will fetch the names of the pods by their label, then will one by one print the logs after writing the name of the pod. So, it will look like something like this:

pod1
log
log
log
pod2
log
log
log
...
podn
log
log
log
-- suren
Source: StackOverflow