Python Basic Logging Tutorial

Burak Bagatarhan
3 min readMay 23, 2022

--

Logging is a means of tracking events that happen when some software runs. The software’s developer adds logging calls to their code to indicate that certain events have occurred. An event is described by a descriptive message which can optionally contain variable data. Events also have an importance which the developer ascribes to the event; the importance can also be called the level or severity.

Python’s has a standard library for logging. In this article, I will explain to you how to add basic logging to your code.

For simple logging usage there are some logging functions. These are debug, info, warning, error and critical.

If you want display console output for ordinary usage you can use print() function. If you want reports the events when program executions you can use info and debug. You can use warnings for issue a warning in particular runtime. For report an error without raising an exception you can use error, exception or critical.

Logging functions are named after the level of the events they are use to track. For example ;

DEBUG : Use for detailed information, for diagnosing problems.

INFO : Use for everything working as expected.

WARNING : Use when something unexpected happens . Or indicate some problem in future.

ERROR : Use for more serious problems. For example when some functions can not run.

CRITICAL : Use when some serious errors occurs.

The default level is WARNING. This means only this level logs can be tracked. If you want you can change it.

Events can be tracked many ways. You can prints logs to console or you can write logs to a file.

As we said before, the logging module has functions for events. As you can see in the picture below, we can use the logging module. Nothing shows up when logging.debug and logging.info are executed. But when you execute logging.warning and logging.error some error logs appeared. This is because the default logging level is set to WARNING.

If we want to change log level. We can use the setLevel function. With setLevel function we can set the DEBUG level to our logger.

For example ;

Very common situation is to save log events to a file For this we can use basicConfig function and pass filename argument to this function and our logs will written to this file.

As you can see above, all log messages are written to a log file. If we remove logging.DEBUG, argument we can only see warnings logs.

For displaying date and time of an event we can use format argument. In format argument we need to use “%(asctime)s”

The default format for date is shown below picture. If you need more control over the formatting of date you can provide datefmt argument to basicConfig.

All of these examples are for basic logging. With this information, you can use “basic logging” in your codes. There are many more features that you can use in the logging package.

--

--

Burak Bagatarhan
Burak Bagatarhan

Written by Burak Bagatarhan

Senior Software Engineer @Trendyol

No responses yet