For several years now I've used Python's logging class in the same way:
def get_module_logger(mod_name):
"""
To use this, do logger = get_module_logger(__name__)
"""
logger = logging.getLogger(mod_name)
handler = logging.StreamHandler()
formatter = logging.Formatter(
'%(asctime)s [%(name)-12s] %(levelname)-8s %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
return logger
Then in some module,
logger = get_module_logger(__name__)
Now, I am running a Python app that uses this inside of a Docker container. I am running the container with -d -i -t
. When I am inside the container after a docker exec -it containername /bin/bash
, I can see the logs if I execute a command in my python script that produces logs. However, from outside, docker logs containername
never shows anything. I have tried running my container with PYTHONUNBUFFERED=0
per a few web posts and that did not help either. Tailing with docker logs -f containername
never shows anything either. So all my logs, both stderr and stdout are empty. I have also tried logging.StreamHandler(sys.stdout)
but to no avail.
What is wrong? Do I need to change something in the handler?
EDIT: my Dockerfile is very simple:
FROM python:3.5.1
MAINTAINER tommy@...
ADD . /tmp
#need pip > 8 to have internal pypi repo in requirements.txt
RUN pip install --upgrade pip
#do the install
RUN pip install /tmp/py/
CMD myservice
EDIT2:
~ docker --version
Docker version 1.11.0, build 4dc5990
PYTHONUNBUFFERED
If this is set to a non-empty string it is equivalent to specifying the -u option.
-u
Force the stdout and stderr streams to be unbuffered. This option has no effect on the stdin stream.
See also PYTHONUNBUFFERED.
Changed in version 3.7: The text layer of the stdout and stderr streams now is unbuffered.
I was able to see the log outputs and was not able to reproduce your issue with your code.
I created a file called tommy.py
:
import logging
def get_module_logger(mod_name):
"""
To use this, do logger = get_module_logger(__name__)
"""
logger = logging.getLogger(mod_name)
handler = logging.StreamHandler()
formatter = logging.Formatter(
'%(asctime)s [%(name)-12s] %(levelname)-8s %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
return logger
if __name__ == "__main__":
get_module_logger(__name__).info("HELLO WORLD!")
Ran the following:
docker run -d -v /tmp/tommy.py:/opt/tommy.py python:3.5 python /opt/tommy.py
And saw this:
$ docker logs -f sleepy_poincare
2016-08-30 17:01:36,026 [__main__ ] INFO HELLO WORLD!
Edit:
Here's my Docker version:
$ docker --version
Docker version 1.12.0, build 8eab29e
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With