Python shell log command not showing up in docker/kubernetes pod

Multi tool use
Python shell log command not showing up in docker/kubernetes pod
I ran the following code in the python shell but it is not showing up the docker/kubernetes pod logs:
import logging
logging.basicConfig(level=logging.INFO)
logging.info("Test")
This is a simplified version of what I am trying to do which is essentially run one-off scripts/commands in the python shell and have logs to show up. How do I go about getting this to work? Is it because the shell I opened up is not attached to the process that creates the logs?
I'm currently using Docker and Kubernetes.
Thanks!
What @TarunLalwani said is 100% true, but you can also cheat and
tee
the output into /proc/1/fd/1
to have stdout of your process go to the stdout of the container's process (you may have to substitute /proc/1
for the PID of your container's main process, but it's usually 1); it may be possible to do some dup2
trickery to get both stdout and stderr to go to /proc/1
's fds, but that would require more than 161 more characters :-)– Matthew L Daniel
Jul 1 at 22:30
tee
/proc/1/fd/1
/proc/1
dup2
/proc/1
1 Answer
1
As Matthew L Daniel wrote, you can cheat by using additional tools as a workaround to send your logs to stdout.
Also, you can import library sys and stream logs to stdout by this lib. There is an example of code:
import logging
import sys
root = logging.getLogger()
root.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
root.addHandler(ch)
I found it on Stack, so I’ve attached link as a source.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You ran this in a shell? If so then it wont be in the logs. The PID 1 stdout only is sent to logs
– Tarun Lalwani
Jul 1 at 9:29