How to Use Logging in Python — pythonpip.com

Parvez Alam
2 min readOct 22, 2022

--

A built-in module called Python log defines the classes and functions that implement the adaptable event-logging system for libraries and applications.

Logging is the process of storing information in a log file or printing into stdout, which can then be utilized for debugging if a problem arises.

Why Do We Need Log?

It is crucial to log in order to obtain the root cause analysis (RCA) of a problem if your code occasionally breaks and you are unable to figure out what caused the problem.

Although you can send the messages to stderr, using print for logging is not recommended.

Python Logging Levels

There are the following types of logging available:

A simple example of logging

Let’s import the logging log module at the top of the file log.py file:

# log.py import logging logging.debug('This is a debug message') logging.info('This is an info message') logging.warning('This is a warning message') logging.error('This is an error message') logging.critical('This is a critical message')

Output:

WARNING:root:This is a warning message ERROR:root:This is an error message CRITICAL:root:This is a critical message

The debug() and info() messages didn't get logged. It is because, by default, the logging module logs the messages with the severity level of WARNING or above.

Logging Basic Configurations

You can use the basicConfig(args) method to configure the logging. There are following parameters are used for this configuration:

  • level: The root logger will be set to a specified severity level.
  • filename: This specifies the filename to log information.
  • filemode: The mode of file if the file name is set, The default is a, which means append.
  • form: This is the format of the log message.

The sample python log code

Let’s create a log.py file added a basic configuration for the python log.

import logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) logger.info('Hello! pythonpip.com') user = {'name': 'adam, 'age': 24} logger.debug('Records: %s', user) logger.info('Finished log')

You can also use debug(), info(), warning(), error(), and critical() method into the basicConfig(). By using a level parameter, you can set what level of log messages you want to record.

Output:

D:\workspace\python_workplace>py log.py INFO:main:Hello! pythonpip.com INFO:main:Finished log

How To Log message into the File in Python

Sometimes, We need to log messages into the file instead of stdout. We can achieve something using python log module:

import logging logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) # create a file handler handler = logging.FileHandler('info.log') handler.setLevel(logging.INFO) # create a logging format formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) # add the handlers to the logger logger.addHandler(handler) logger.info('See the info.log file')

Output:

//info.log 2022-10-15 22:50:30,916 - main - INFO - See the info.log file

Originally published at https://www.pythonpip.com on October 22, 2022.

--

--

Parvez Alam

Hey, I am Parvez Alam. A software developer since 2009. I love learning and sharing knowledge. https://www.phpflow.com/