No, please don’t debug your program with print.

Python comes with a logging module in the standard library that provides a flexible framework for emitting log messages for Python programs.

The module provides some ways for applications to configure different log handlers and a way of routing log messages to these handlers. This allows for a highly flexible configuration that can deal with a lot of different use cases.

The logging functions are named after the level or severity of the events they are used to track. The standard levels and their applicability are described below:

LevelLevel CodeWhen used
DEBUG10Detailed information, typically of interest only when diagnosing problems.
INFO20Confirmation that things are working as expected.
WARNING30An indication that something unexpected happened. The software is still working as expected.
ERROR40Due to a more serious problem, the software has not been able to perform some function.
CRITICAL50A serious error, indicating that the program itself may be unable to continue running.

For more details : Python Foundation How To Logging

Basic Logging

A very simple example for logging is:

import logging"Hello, World!")
logging.warning('Watch out!')

If you run this script you will see:

WARNING:root:Watch out!

The INFO message doesn’t appear because the default level is WARNING.

Logging From Modules

However better approach will be using __name__ with DEBUG logging level. Since __name__ contains the full name of the current module, so this will simply work in any module.

import logging

logger = logging.getLogger(__name__)"Hello, World!")
logger.warning('Watch out!')

Output will be:

INFO:__main__:Hello, World!
WARNING:__main__:Watch out!

Logging to a File

A very common situation is that of recording logging events in a file.

import logging

logging.basicConfig(filename='example.log', level=logging.DEBUG)
logger = logging.getLogger(__name__)

logger.debug('This message should go to the log file')'So should this')
logger.warning('And this, too')

If we open the file— “example.log”, we should find the log messages:

DEBUG:__main__:This message should go to the log file
INFO:__main__:So should this
WARNING:__main__:And this, too

Configuration via an INI file

This is the best approach for the meajority of cases;

Crate a file name logging.iniin the project root directory as below:




format=%(asctime)s [%(levelname)s] %(name)s: %(message)s

format=[%(asctime)s] %(levelname)s [%(filename)s %(name)s %(funcName)s (%(lineno)d)]: %(message)s




Or curl it:

$ curl -o logging.ini

Then configure it as below;

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import logging

from logging.config import fileConfig

logger = logging.getLogger(__name__)

name = "YourName""Hello {name}")
logger.debug('This message should go to the log file')'So should this')
logger.warning('And this, too')

If you run the script, the sysout will be:

2019-07-14 18:15:13,161 [INFO] __main__: Hello YourName
2019-07-14 18:15:13,161 [DEBUG] __main__: This message should go to the log file
2019-07-14 18:15:13,161 [INFO] __main__: So should this
2019-07-14 18:15:13,161 [WARNING] __main__: And this, too

And debug.log file should contain:

[2019-07-14 18:12:58,910] INFO [ __main__ <module> (14)]: Hello YourName
[2019-07-14 18:12:58,910] DEBUG [ __main__ <module> (15)]: This message should go to the log file
[2019-07-14 18:12:58,910] INFO [ __main__ <module> (16)]: So should this
[2019-07-14 18:12:58,910] WARNING [ __main__ <module> (17)]: And this, too

All done.