Python Logging Basics
The basics of using the logging module to record the events in a file are very simple. For that, simply import the module from the library.
- Create and configure the logger. It can have several parameters. But importantly, pass the name of the file in which you want to record the events.
- Here the format of the logger can also be set. By default, the file works in append mode but we can change that to write mode if required.
- Also, the level of the logger can be set which acts as the threshold for tracking based on the numeric values assigned to each level.
There are several attributes that can be passed as parameters. - The list of all those parameters is given in Python Library. The user can choose the required attribute according to the requirement.
After that, create an object and use the various methods as shown in the example.
Logging a Variable
This code demonstrates how to log an error message. The logging.error() function is used to log an error message with a placeholder %s for the variable name.
Python3
import logging name = 'GFG' logging.error( '%s raised an error' , name) |
Output :
ERROR:root:GFG raised an error
Logging of all the levels
This code demonstrates all the levels of logging.
Python
# importing module import logging # Create and configure logger logging.basicConfig(filename = "newfile.log" , format = '%(asctime)s %(message)s' , filemode = 'w' ) # Creating an object logger = logging.getLogger() # Setting the threshold of logger to DEBUG logger.setLevel(logging.DEBUG) # Test messages logger.debug( "Harmless debug Message" ) logger.info( "Just an information" ) logger.warning( "Its a Warning" ) logger.error( "Did you try to divide by zero" ) logger.critical( "Internet is down" ) |
The above code will generate a file with the provided name and if we open the file, the file contains the following data.
Configuring Logging
Logging to a File: temp.conf
[loggers] keys=root,simpleExample [handlers] keys=consoleHandler [formatters] keys=simpleFormatter [logger_root] level=DEBUG handlers=consoleHandler [logger_simpleExample] level=DEBUG handlers=consoleHandler qualname=simpleExample propagate=0 [handler_consoleHandler] class=StreamHandler level=DEBUG formatter=simpleFormatter args=(sys.stdout,) [formatter_simpleFormatter] format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
Example:
The code sets up a logging system using the configuration from the temp.conf file. It obtains a logger named simple example and logs messages with various log levels.
Python3
import logging import logging.config logging.config.fileConfig( 'temp.conf' ) # create logger logger = logging.getLogger( 'simpleExample' ) # 'application' code logger.debug( 'debug message' ) logger.info( 'info message' ) logger.warning( 'warn message' ) logger.error( 'error message' ) logger.critical( 'critical message' ) |
Output :
2023-06-15 18:16:21 - simpleExample - DEBUG - debug message 2023-06-15 18:16:21 - simpleExample - INFO - info message 2023-06-15 18:16:21 - simpleExample - WARNING - warn message 2023-06-15 18:16:21 - simpleExample - ERROR - error message 2023-06-15 18:16:21 - simpleExample - CRITICAL - critical message
Logging in Python
Logging is a means of tracking events that happen when some software runs. Logging is important for software developing, debugging, and running. If you don’t have any logging record and your program crashes, there are very few chances that you detect the cause of the problem. And if you detect the cause, it will consume a lot of time. With logging, you can leave a trail of breadcrumbs so that if something goes wrong, we can determine the cause of the problem.
There are a number of situations like if you are expecting an integer, you have been given a float and you can a cloud API, the service is down for maintenance, and much more. Such problems are out of control and are hard to determine.