0

I would like to know if is possible to filter logs, only showing ERROR Level

kubectl logs -f -n the-namespace the-pod [LOG-LEVEL?] in example ERROR only

I was trying with:

kubectl logs -f -n bci-api the-pod | awk '{ if ($3 == "ERROR") { print } }' 

The problem, are there some lines that are continuation of ERROR line and will be hidden!

Is it possible?

Thanks in advance!

joseluisbz
  • 1,491
  • 1
  • 36
  • 58
  • I expect it'd be trivial with awk but you haven't shown any [mcve] with concise, testabe sample input/expected output for us to test with, nor have you tagged the question with awk. If you do both things then I expect you'd get answers. – Ed Morton Jul 15 '22 at 20:11

2 Answers2

1

According to this documentation there doesn't seem to be a way you can do this. There is a verbosity flag -v but that is for the level of verbosity in kubectl itself, not in the logs it is getting for the pods/containers.

Then you also have the --stderrthreshold <SEVERITY> option to kubectl as provided in this doc, but I suspect that is the same thing.

Perhaps a better way of handling this would be to set what is called a logging architecture for your cluster where you can control which logs the pod actually generates. (read more here) In that way, fetching them from kubectl will be probably more desirable.

We almost always use a 3rd party centralized logging solution (like fleuntd), outside of k8s for this specific reason. And we simply inject a logging sidecar container to your pods which continuously dumps the logs to this central component. This is a very common design pattern.

zer0
  • 2,153
  • 10
  • 12
0

can use grep functionality to filter errors from the entire logs

kubectl logs -f -n bci-api the-pod | grep -i 'Error'
dejanualex
  • 3,872
  • 6
  • 22
  • 37