How To Use Logging Module In Python

Introduction

In this article, we are going to learn about how to use the logging module in Python? Following are the points we are going to cover in this article,

  • Why do we need logging?
  • How to use the logging module in Python?
  • Logging levels or severity
  • How to configure logging?
  • How do format the logs or display messages?
  • How to log errors or exceptions to file?
  • Examples

Why do we need logging?

It's very hard to find out the issue or breaking changes in the application without logging. The logging tracks the events and flows when the application runs, also helps the developers to easily find the breaking changes, easily debug and fix them.

How to use the logging module in Python?

The logging module is the part of Python standard library so you don't need to install it separately. This module supports logging with the help of logging, logging.config, and logging.handlers modules. It is a very powerful module, easy to configure and use. Following are the methods we can use to log:

  • print(): To display the output to the console.
  • info(msg*args**kwargs), debug(msg*args**kwargs): To log the events or the details output about the application.
  • warning(msg*args**kwargs): To log a warning to the user about the event or flow in the application.
  • error(msg*args**kwargs), exception(msg*args**kwargs): To log the bugs in code, syntax error, or the exceptions in the application.
  • critical(msg*args**kwargs): To log the error which stops the application to run.

Note
In the above logging methods, there are three parameters - The first parameter is the 'msg' which is nothing but the message we want to log or show. The second parameter '*args' is nothing but the run time we can pass to the message using the string formatting operator. The third parameter '**kwargs', there are four keyword arguments we can pass which are 'exc_infostack_infostacklevel, and extra'.

exc_info The default value is 'False', if it is set to 'True' then it adds the exception information into the log message.

stack_info The default value is 'False', if it is set to 'True' then it adds stack information into the log message.

stacklevel The default value is 1, if it is greater than 1 then the number of stack frames will be skipped when logging the event.

extra It is used to logging the user-defined attributes, incorporated with the messages.

Logging levels or severity

There are few predefined levels or severity, based on this the logging function names are defined. Following are the levels with their numeric value and description:

  • CRITICAL(50) - A major error because of that application stop working.
  • ERROR(40) - Due to some error or exception some functionality of an application won't work.
  • WARNING(30) - To indicate to the user that some code will get errors in the feature but the application doesn't stop.
  • INFO(20) - To indicate to the user that the application working as expected.
  • DEBUG(10) - Gives detailed information, when we debug the application.

Note
The default logging level is set to 'WARNING' which means the log will get this level and above. If you want to log all the events then in the configuration we need to set level 'DEBUG' then you will see all the levels log. Please see the below example, you will only get the log about the 'WARNING' level in the output.

Simple example

import logging
logging.info('Info message goes here')
logging.warning('Warning message goes here')

Output

How to configure logging?

In the previous section, we saw that the 'INFO' logging does not log the message. To overcome that, the logging module provided the method 'basicConfig(**kwargs)'. With the help of this method, we can do the basic configuration about the logging. Following are the commonly used list of arguments we can pass to it more info please check here,

  • filename - To specify the file name.
  • filemode - To specify the file open mode, defaults to 'a'(append).
  • format - To specify the format of logs, defaults to attributes levelnamename and message separated by colons.
  • datefmt - To specify the date/time format, as accepted by 'time.strftime()'.
  • style - To specify the styling of the format string, defaults to '%'.
  • level - To specify the logger level here, the default is 'WARNING'.

Simple example

The above example shows how to configure the logger configuration, we had used the same example just added the configuration line with logger level set to 'INFO' so in the output you will see both the logs.

import logging
logging.basicConfig(level=logging.INFO)
logging.info('Info message goes here')
logging.warning('Warning message goes here')

Output

How do format the logs or display the messages?

To format the logs or display messages, we need to set the 'format' parameter in 'basicConfig(**kwargs)' method as in below example. Following are the three supported style string formats:

printf - ‘%(name)s:%(message)s’

str.format() - ‘{name}:{message}’

str.template - ‘$name:$message’

Simple example

In the below example we have passed the parameter 'format' with values 'name, levelname and message'.

import logging
logging.basicConfig(level=logging.INFO, format='%(name)s - %(levelname)s - %(message)s')
logging.info('Info message goes here')
logging.warning('Warning message goes here')

Console Output

Once you execute the above code you will get the formatted console output as below,

How to log errors or exceptions to file?

In day-to-day development, developers use the console output to check the issues, bugs, or errors in code but in the case of deployed applications how we are going to check? In that situation, we have to write a log or record the errors in a file. Using the same 'logging' module we can achieve that, with the help of file parameters in the 'basicConfig(**kwargs)' method as below example.

Simple example

In the below example we have passed the two parameters first is 'filename=app.log' and the second is 'filemode='w''.

import logging
logging.basicConfig(level=logging.INFO, format='%(levelname)s - %(message)s', filename='app.log', filemode='w')
logging.info('Info message goes here')
logging.warning('Warning message goes here')

File Output

When you open the 'app.log' file as we mentioned the name in the example you will see an output as below,

Examples

Customize the output message by passing run time data.

import logging
logging.basicConfig(format='%(levelname)s - %(message)s')
logging.warning('%s it, %s it!', 'Learn', 'Share')

Output: WARNING - Learn it, Share it!

Show DateTime in the message by using the 'asctime' format value and configuration parameter 'datefmt'.

import logging
logging.basicConfig(format='%(levelname)s - %(asctime)s - %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
logging.warning('%s it, %s it!', 'Learn', 'Share')

Output

WARNING - 09/03/2021 08:52:34 AM - Learn it, Share it!

Show the exception or stack trace using the 'logging.error' log message. In the below example we have used the 'exc_info=True' in the 'error' function, 'exc_info' default value is false then will get only the message in the output, here we have set it to true so will get the stack trace in output.

import logging
logging.basicConfig(format='%(levelname)s - %(asctime)s - %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
arr = []
try:
 result = arr[0]
except Exception as e:
 logging.error("Exception occurred", exc_info=True)

Output

In the output, you can see the message with the stack trace information.

ERROR - 09/03/2021 09:03:07 AM - Exception occurred
Traceback (most recent call last):
File "D:\Demos\py\test.py", line 7, in <module>
result = arr[0]
IndexError: list index out of range

Show the exception or stack trace using the 'logging.exception' log message.

import logging
logging.basicConfig(format='%(levelname)s - %(asctime)s - %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
arr = []
try:
 result = arr[0]
except Exception as e:
 logging.exception("Exception occurred")

Output

In the output, you can see the message with the stack trace information.

ERROR - 09/03/2021 09:13:57 AM - Exception occurred
Traceback (most recent call last):
File "D:\Demos\py\test.py", line 7, in <module>
result = arr[0]
IndexError: list index out of range

Conclusion

In this article, we learned about how to use, configure, write the logs to the file using the logging module in Python and simple examples. If you have any suggestions or queries regarding this article, please leave a comment.

“Learn it. Share it.”


Similar Articles