How to change kubectl logs timestamp timezone

Viewed 356

The timezone did not meet my expectations When i use kubectl logs <mypod> --timestamps to get the pod log.

current output:

2022-06-15T07:31:41.826543867Z 2022/06/15 15:31:41 [info] Start grpc server listen 58212 port.
2022-06-15T07:31:41.826568525Z 2022/06/15 15:31:41 [info] Start http server listen 10000 port.

expected output:

2022-06-15T15:31:41+0800 2022/06/15 15:31:41 [info] Start grpc server listen 58212 port.
2022-06-15T15:31:41+0800 2022/06/15 15:31:41 [info] Start http server listen 10000 port.

What should I set up to achieve this output?

1 Answers

kubectl does not support this directly at the time of writing this and it tricky to do this in a portable way. On Linux something like this would work:

kubectl logs --timestamps mypod | while read timestamp line; do \
  echo "$(env TZ="EST" date -d "$timestamp" '+%Y-%m-%dT%H:%M:%S.%N%:z') $line"; done

You will get an output like this:

2022-06-08T14:13:41.847615539-05:00 INFO [06-08|19:13:41.847] Starting Geth on Ethereum mainnet...
Related