Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Logging in Docker

Tags:

python

docker

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.

like image 554
Code4Bread Avatar asked Feb 07 '18 06:02

Code4Bread


People also ask

How does Python logging work?

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.

What is docker logging?

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.

What is logging getLogger (__ Name __)?

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.


1 Answers

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.

like image 152
Zeinab Abbasimazar Avatar answered Sep 22 '22 06:09

Zeinab Abbasimazar