I am test running python Script in Docker Container on Ubuntu Web Server. I am trying to find the Log file generated by Python Logger Module. Below is my Python Script
import time
import logging
def main():
logging.basicConfig(filename="error.log", level=logging.DEBUG)
start_time = time.time()
logging.debug("Program starts running at %d", start_time)
i = 0
while i < 1000:
print(i)
i += 1
while_time = time.time() - start_time
logging.debug("Program ends running at %d", while_time)
start_time = time.time()
logging.debug("Program starts running at %d", start_time)
for x in range(0, 100):
print(x)
if_time = time.time() - start_time
print('While took - %s Seconds' % while_time )
print('If took - %s Seconds' % if_time )
logging.debug("Program ends running at %d", start_time)
main()
I have searched and found that Docker file produces Log file in json format in /var/lib/docker/container/{con_id}/{con_id}.log
This log file only includes the stdout and I cannot find the Log file generated by Python. Is there any way to retrieve the file.
Logging LevelsWhen you set a logging level in Python using the standard module, you're telling the library you want to handle all events from that level up. Setting the log level to INFO will include INFO, WARNING, ERROR, and CRITICAL messages. NOTSET and DEBUG messages will not be included here.
The docker logs command shows information logged by a running container. The docker service logs command shows information logged by all containers participating in a service. The information that is logged and the format of the log depends almost entirely on the container's endpoint command.
getLogger(name) is typically executed. The getLogger() function accepts a single argument - the logger's name. It returns a reference to a logger instance with the specified name if provided, or root if not. Multiple calls to getLogger() with the same name will return a reference to the same logger object.
You have specified file 'error.log'
in command logging.basicConfig(filename="error.log", level=logging.DEBUG)
for logger
to put your logs into. This file is located inside your container and since containers are stateless, you have to mount the log file somewhere in your local machine in order to have access after powering off your container. You can read this for more information.
BTW, if you want to access the log file while it's already up, you can use exec
option of the docker to make an interactive shell through the container and find the logs:
docker exec -it ${container_name}
This documentation will helpful for exec
commandline option.
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