Python | Add Logging to Python Libraries
Last Updated :
23 Feb, 2023
In this article, we will learn how to add a logging capability to a library, but don’t want it to interfere with programs that don’t use logging. For libraries that want to perform logging, create a dedicated logger object, and initially configure it as shown in the code below - Code #1 :
Python3
# abc.py
import logging
log = logging.getLogger(__name__)
log.addHandler(logging.NullHandler())
# Example function (for testing)
def func():
log.critical('A Critical Error !')
log.debug('A debug message')
With this configuration, no logging will occur by default.
Python3
import somelib
abc.func()
Output :
NO OUTPUT
If the logging system gets configured, log messages will start to appear. Code #2:
Python3
import logging
logging.basicConfig()
somelib.func()
Output :
CRITICAL:somelib:A Critical Error!
How does it work ?
- Libraries present a special problem for logging, since information about the environment in which they are used isn’t known.
- As a general rule, never write library code that tries to configure the logging system on its own or which makes assumptions about an already existing logging configuration.
- Thus, one needs to take great care to provide isolation.
- The call to getLogger(__name__) creates a logger module that has the same name as the calling module.
- Since all modules are unique, this creates a dedicated logger that is likely to be separate from other loggers.
- The log.addHandler(logging.NullHandler()) operation attaches a null handler to the just created logger object. A null handler ignores all logging messages by default.
- Thus, if the library is used and logging is never configured, no messages or warnings will appear.
Logging of individual libraries can be independently configured, regardless of other logging settings as shown in the code given below - Code #3:
Python3
import logging
logging.basicConfig(level = logging.ERROR)
import abc
print (abc.func())
Change the logging level for 'abc' only
logging.getLogger('abc').level = logging.DEBUG
print (abc.func())
Output :
CRITICAL:abc:A Critical Error!
DEBUG:abc:A debug message
The time complexity of the code is constant time O(1) as there are no loops or iterations.
The space complexity of the code is also constant space O(1) as the amount of memory used does not increase with the size of the input.
Similar Reads
Python | Add Logging to a Python Script In this article, we will learn how to have scripts and simple programs to write diagnostic information to log files. Code #1 : Using the logging module to add logging to a simple program Python3 1== import logging def main(): # Configure the logging system logging.basicConfig(filename ='app.log', le
2 min read
Python | Logging Test Output to a File Problem - Writing the results of running unit tests to a file instead of printed to standard output. A very common technique for running unit tests is to include a small code fragment (as shown in the code given below) at the bottom of your testing file. Code #1 : Python3 1== import unittest class M
2 min read
Creating Your Own Python IDE in Python In this article, we are able to embark on an adventure to create your personal Python Integrated Development Environment (IDE) the usage of Python itself, with the assistance of the PyQt library. What is Python IDE?Python IDEs provide a characteristic-rich environment for coding, debugging, and goin
3 min read
How to Install Python-logging module on Linux? A programmer's backpack should include a logging library. It can aid in the development of a better knowledge of a program's flow and the discovery of scenarios that you may not have considered when designing. Because most third-party Python libraries employ logging, you may combine your log message
2 min read
Libraries in Python Normally, a library is a collection of books or is a room or place where many books are stored to be used later. Similarly, in the programming world, a library is a collection of precompiled codes that can be used later on in a program for some specific well-defined operations. Other than pre-compil
8 min read