Py #3 Print vs Logging

Göker Güner
Python Blog
Published in
5 min readMar 20, 2022

--

Hello, In the third article of the series where we examine the useful features of the Python language, we will examine the logging library, which is used in almost all product-level software projects, its uses and the differences / advantages from the print function.

If you are not interested in the Turkish version of this article, you can skip this paragraph.

Merhabalar, Python dilinin kullanışlı özelliklerini incelediğimiz serinin üçüncü yazısında, ürün seviyesindeki yazılım projelerinin neredeyse tamamında kullanılan logging kütüphanesini, kullanımlarını ve print fonksiyonundan farklarını/avantajlarını inceleyeceğiz.

Bu yazının Türkçe versiyonu için link: Link

No matter what programming language we are learning, the first thing almost all of us learn and write is the same:

Why Logging?

However, when developing a product in our professional life, the print function is not very useful.

  • You may want to periodically see that your code is working correctly.
  • You may want to know which line of the file, in which function and at what time your error messages occur.
  • You can distinguish the records you want to keep according to their importance.

These are the conveniences that the logging library gives you. However, apart from these, there are 2 more critical points:

  • If your code is being imported by other python tools or scripts, it’s not practical to print something to stdout using print because the user won’t know where these messages are coming from.
  • Also, you will most likely be running your code by Dockerizing it (i.e. converting it to a Docker image and in a Docker container). This means that you won’t be able to see any of the lines you print using print in the container logs, because you don’t have access to the console at this time. The print function only prints to the console.

Creating a Log File with Logging

An example usage of the logging library is as follows:

When we created this file and ran it with the “python logging_file_example.py” command, we saw only one line of output. This is the screen output that the print function prints to the console. The output of the logging library is in the directory where you run the file. The file named logfile.txt that you specified in the basicConfig parameters was created and this output was saved there. Let’s examine this output:

The sentence and record type that we want to be written on the screen, in which directory/file exactly the error is, in which line, the part of the code that caused the error, the type of the error and the message are written exactly.

In order to print the same details with the print function, you must make these definitions for each point you use yourself. However, we only need to do it once in basicConfig for the logging library.

Let’s take a closer look at the code we wrote. For example, the debug record on line 14 is not in our txt file. So why? The answer is the level parameter in basicConfig. Since we have specified this parameter as an error, it will not write any logs lower than the severity level to the file.

What are the log levels?

In order of increasing importance, the log levels are:

Debug: We use it to examine/correct an unexpected output that occurs while the program is running.

Info: We use it to observe the healthy flow of our software.

Warning: Unexpected error etc. We use it when we want to observe a situation.

Error: We use it when there is a problem in the program, usually when the program cannot execute some commands.

Critical: The highest level of error. We usually use it when the program can no longer run.

Other basicConfig Parameters

To briefly mention the other parameters we wrote in basicConfig:

Filename: The name of the file where our logs will be kept. If a directory is not specified, it keeps it in its own directory.

Format: Determines the format in which the record will be kept. (In our example, 3 parameters are given.)

Filemode: Determines the mode in which the log will be kept. You can choose “w” or “a”. “w” starts from the beginning of writing the contents of the previously written file, while “a” continues from where the file last left off.

Now let’s see the difficulty of trying to get a similar output using the print function.

Note that we have to re-write each of the variables we write inside the format function, wherever we use the print function. In the scenario where we use the logging library, it is sufficient to define them once at the beginning of our code. It should be noted that the output we created using the print function does not include details such as which line of the code in which directory gives an error.

Generating Console Output with Logging

As another usage example, let’s see the output directly on the screen this time instead of printing it to a file.

As expected, our code first printed the output of the logger and then the output of the print function.

First, we created our logger variable from the logging library with the getLogger method. Then we created a handler variable named console to print our output to the console. The StreamHandler method sends records to the console via stdout, similar to what print does.

We determine the format of the error message we want to create with the format_str variable. Unlike our previous example, this time we increased the number of parameters and stated that we wanted to see 6 parameters. Then, with the help of the Formatter method, we specify that we want to format our record according to this format and print it to the console with the help of the setFormatter method. Then we add our console variable to the logger with the help of a handler.

We want to see the record level with the help of the setLevel method.
we are setting. In the previous example, we gave it directly inside the basicConfig. Alternatively, we can set it this way.

When we run our code, we see the details we want to see as complete console output.

Finally, I would like to share the documentation link of the logging library, as I often do, especially in the Machine Learning series. For Python developers, it’s worth noting that the documentation is pretty well done, and reads and improves the implementation a lot: https://docs.python.org/3/howto/logging-cookbook.html#

See you in our next articles.

--

--

Göker Güner
Python Blog

YTU Alumni. ML Ops & Engineer at AlternaCX. Data&AI Enthusiast. MS Student at Bahcesehir University AI Program with Thesis.