Logging Module in Python

Logging Module in Python

In this article, I am going to discuss Logging Module in Python with examples. Please read our previous article where we discussed Regular Expression Methods in Python. As part of this article, we are going to discuss the following pointers in detail.

  1. What is logging?
  2. Why do we need logging?
  3. Advantages of logging
  4. Logging Module in Python
  5. Logging Module Implementation in Python
  6. How to configure a log file in overriding mode?
  7. Formatting Log Messages in Python
  8. Writing Exceptions to Log File in Python
What is logging?

The process of saving or storing the complete application flow and exception information to a file is called logging.

Why do we need logging?

In real time, application logging is very important and mandatory. When any action is being carried out using a program and if we save all the steps that are being executed for that particular action in a file. Then we will have the privilege to check what has happened for which action and how it happened.

For example, when you transfer money, it’s good to save the corresponding steps in a log file to monitor if something goes wrong while transferring. When a program crashes, if there is no logging record then it’s more pain to track or understand what actually has happened.

Advantages of logging

Log files help us while debugging code when an issue occurs.

  1. We can know which user has logged in and which operations are carried by him etc.
  2. We can provide statistics like number of requests per day etc.
Logging Module in Python:

In python, if we want to implement logging for our code, we can use an inbuilt module by name ‘logging’. Before getting into the logging module details, let’s understand some logging levels which are available.

Logging Module in Python

Logging Module Implementation in Python:

In order to implement logging in our code, we have to import the logging module.

                  import logging

After importing, we are required to create a file to store messages and we have to specify which level log messages should be stored. This can be done by using basicConfig() method available in the logging module

                  logging.basicConfig(filename=’log.txt’, level=logging.WARNING)

The above line, when executed, will create a file log.txt and will store log messages which are of either WARNING level or higher-level.

After creating a log file, we can write log messages to that file by using the following methods.

  1. logging.debug(message)
  2. logging.info(message)
  3. logging.warning(message)
  4. logging.error(message)
  5. logging.critical(message)
Program: logging Implementation in python (demo1.py)
import logging
logging.basicConfig(filename='sample_log.txt', level=logging.WARNING)
print('Logging started')
logging.debug('Debug Information')
logging.info('info Information')
logging.warning('warning Information')
logging.error('error Information')
logging.critical('critical Information')
print('Logging end')

Output:

logging Implementation in python

File sample_log.txt:

logging module Implementation in python

In the above program, we have set the logging level to ‘logging.WARNING’, hence in the log file only the logs of level warning and above will be stored. This is the default logging level, the python takes, if we don’t specify any level.

Debug level:

If we want level of logs to be saved, then we need to set the the level to ‘DEBUG’

Program: Debug level logging Implementation in python (demo2.py)

import logging
logging.basicConfig(filename='sample_log2.txt', level=logging.DEBUG)
print('Logging started')
logging.debug('Debug Information')
logging.info('info Information')
logging.warning('warning Information')
logging.error('error Information')
logging.critical('critical Information')
print('Logging end')

Output:

Debug level logging Implementation in python

File sample_log2.txt

Debug level logging module Implementation in python

In the above program, since we have used a different file name from the first example, a new file has been created. If we had used the same file name as in the first one i.e ‘sample_txt.log’ instead of ‘sample_txt2.log’, then the log messages could have been appended to the file.

How to configure a log file in overriding mode?

When we create a log file using the basicConfig() method, the file will be created, by default, in appending mode if we don’t mention any file mode. Instead of appending if we want to overwrite data then we have to mention it in the basicConfig() filemode property.

logging.basicConfig(filename=’log.txt’, level=logging.WARNING,filemode=’w’)

Note: logging.basicConfig(filename=’log.txt’, level=logging.WARNING,filemode=’w’)

  1. If we don’t mention the file name in the above statement, then by default, the log messages will be printed to the console rather than a file.
  2. If we don’t mention the logging level, then by default, the warning level is considered.
  3. If we don’t mention the file mode, then by default, append mode is considered.
Program: configure a log file in overriding mode (demo3.py)
import logging
logging.basicConfig()
print('Logging started')
logging.debug('Debug Information')
logging.info('info Information')
logging.warning('warning Information')
logging.error('error Information')
logging.critical('critical Information')
print('Logging end')

Output:

configure a log file in overriding mode

The log messages are printed to the console because we haven’t mentioned the file in the basicConfig() method. The messages printed to the console are of warning and above level, because the default level is taken as warning.

Formatting Log Messages in Python:

By using format keyword-argument in the basicConfig() method, we can format log messages as per our requirement. Let’s see some common formattings:

To display only level name:

logging.basicConfig(format=’%(levelname)s’)

Program: Display only level name (demo4.py)

import logging
logging.basicConfig(format='%(levelname)s')
print('Logging started')
logging.debug('Debug Information')
logging.info('info Information')
logging.warning('warning Information')
logging.error('error Information')
logging.critical('critical Information')
print('Logging end')

Output:

Display only level name

To display level name and message

logging.basicConfig(format=’%(levelname)s:%(message)s’)

Program: Display level name and message (demo5.py)

import logging
logging.basicConfig(format='%(levelname)s:%(message)s')
print('Logging started')
logging.debug('Debug Information')
logging.info('info Information')
logging.warning('warning Information')
logging.error('error Information')
logging.critical('critical Information')
print('Logging end')

Output:

Display level name and message

Add timestamp in the log messages

logging.basicConfig(format=’%(asctime)s:%(levelname)s:%(message)s’)

Program: Add timestamp in the log messages (demo6.py)

import logging
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s')
print('Logging started')
logging.debug('Debug Information')
logging.info('info Information')
logging.warning('warning Information')
logging.error('error Information')
logging.critical('critical Information')
print('Logging end')

Output:

Add timestamp in the log messages

Change date and time format

logging.basicConfig(format=’%(asctime)s:%(levelname)s:%(message)s’,
datefmt=’%d/%m/%Y %I:%M:%S %p’)
datefmt=’%d/%m/%Y %I:%M:%S %p’ → case is important
%I → 12 hours time scale
%H → 24 hours time scale

Program: Change date and time format in the log messages (demo7.py)

import logging
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s',datefmt='%d/%m/%Y %I:%M:%S %p')
print('Logging started')
logging.debug('Debug Information')
logging.info('info Information')
logging.warning('warning Information')
logging.error('error Information')
logging.critical('critical Information')
print('Logging end')

Output:

Change date and time format in the log messages in python

Writing Exceptions to Log File in Python:

By using the following function, we can write exception information to the log file.

logging.exception(msg)

Program: Writing Exceptions to Log File in Python (demo8.py)

import logging
logging.basicConfig(filename='demo.txt',level=logging.INFO,format='%(asctime)s:%(levelname)s:%(message)s', datefmt='%d/%m/%Y%I:%M:%S %p')
logging.info('A new Request Came')
try:
   x=int(input('Enter First Number:'))
   y=int(input('Enter Second Number:'))
   print('The Result:',x/y)
except ZeroDivisionError as msg:
   print('Cannot divide with zero')
   logging.exception(msg)
except ValueError as msg:
   print('Please provide int values only')
   logging.exception(msg)
logging.info('Request Processing Completed')

Output1:

Writing Exceptions to Log File in Python

Output2:

Logging Module in Python

File demo.txt:

Logging Module in Python with Examples

In the next article, I am going to discuss Customized Logging in Python. Here, in this article, I try to explain Logging Module in Python with Examples. I hope you enjoy this Logging Module in Python with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *