7

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.

1
  • 1
    Whats there in error.log file, You have written a script to write a logs in that file than why are you checking in docker logs locations. Commented Feb 7, 2018 at 6:32

1 Answer 1

10

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.

Sign up to request clarification or add additional context in comments.

1 Comment

On linux, docker exec -it ${container_name} bash followed by ls worked for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.