Tips and Tricks for Handling Logging Files in Python
By: Ng Wai Foong
Tags: Python, DevOps, Debugging, Flask, Programming

1. Basic Usage

Import

You need to import the module before you can use it. Add the following import statement:

import logging

Next, you can log the messages by utilizing the available functions based on the severity level.

Logging levels

There are altogether five levels provided:

DEBUG — Detailed information, typically of interest only when diagnosing problems.

— Detailed information, typically of interest only when diagnosing problems. INFO — Confirmation that things are working as expected.

— Confirmation that things are working as expected. WARNING — An indication that something unexpected happened, or indicative of some problem in the near future (e.g. “disk space low”). The software is still working as expected.

— An indication that something unexpected happened, or indicative of some problem in the near future (e.g. “disk space low”). The software is still working as expected. ERROR — Due to a more serious problem, the software has not been able to perform some function.

— Due to a more serious problem, the software has not been able to perform some function. CRITICAL — A serious error, indicating that the program itself may be unable to continue running.

You can call them as follows:

logging.debug('Debug')

logging.info('Info')

logging.warning('Warning')

logging.error('Error')

logging.critical('Critical')

Let’s test it out by setting up a simple Flask server and running the log messages.

Save the code above in a file called log_test.py . Open up a terminal and point it to the directory and run it via the following command:

python log_test.py

Open up a browser and enter localhost:5000 , you should be able to see the Hello world response and the following output in the console.

Image by Author

You will notice that the “Called hello_world” is not logged. This is mainly because the logging level has been defaulted to warning and above. Debug and info messages will not be logged.

We can easily change the logging level via the basicConfig function during the initialization. Let’s test it by adding the following code right above the app.route decorator and below the app = Flask(__name__) initialization .

logging.basicConfig(level=logging.INFO)

You should be able to see the following output.

Image by Author

Initialization via basicConfig

The basicConfig function also accepts a filename parameter which indicates that it should use a FileHandler to log it to a file instead of a console.

Create a new directory called log and modify the basicConfig code to the following:

logging.basicConfig(filename='log/flask_server.log' ,level=logging.INFO)

A file called flask_server.log will be generated once you access localhost:5000 in the web browser.

Image by Author

The logging system will append the log messages to the file for the subsequent call to the URL.

Variable data

You can use format string to log variable data. Example as follows:

data = 'hello_world'

logging.error('An exception occurred at %s', data)

Format

You can specify the format of the output based on your preferences. The following example will log the time, the severity level of the message, and lastly, the output message.

logging.basicConfig(filename='log/flask_server.log' ,level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")

You should get the following output:

Image by Author

In the next section, we will be exploring two other main components of the logging module.