Skip to content

Latest commit

 

History

History

004. generate app logs outside of the container

⬅️ Back to repo home ▪️ ↗️ Back to search

004. Generate a application log file outside of the container

Capturing the logs generated by an application in a file and binding it to a volume outside the container for data persistence.

What You Will Do?

  • Run a logging app which creates random logs inside a text file.
  • Bind the log file with a directory in the host machine.
  • Retrieve real-time logs.

How To Do It?

You have two files in your aplication folder:

  • generate_logs.py which acts as the main application.
  • app_logger.py which acts a repository for helper functions.

You can configure a logger function which will write application logs to a text file by appending lines of text.

The logger function is defined inside app_logger.py which can act as a repository for helper functions inside your application. The app_logger.py uses two environment variables to work (as in any other proper application):

  • DEBUG allows the logger function to generate log data.
  • LOG_FILE_PATH tells the logger the location of the log file to write to.

To run this setup you can either use a single docker command:

cd 004.\ generate\ a\ log\ file\ outside\ of\ the\ container/
# build your image with name "log_generator"
docker build -t log_generator .
# start your image
docker run --name log_gen -v .:/app -w /app -e DEBUG=1 -e LOG_FILE_PATH="logs.txt" -d log_generator 

or alternatively, you can simply run the docker-compose.yml file which is the equivalent to the docker commands shown above:

cd 004.\ generate\ a\ log\ file\ outside\ of\ the\ container/
docker compose up

Log preview

You will see your logs being generated like this:

Resources