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

7/1/2018

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!

-- perseverance
docker
kubernetes
python

1 Answer

7/2/2018

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.

-- Nick Rak
Source: StackOverflow