15. Logging - MantsSk/CA_PTUA14 GitHub Wiki
Logging is a very useful tool in a programmer’s toolbox. It can help you develop a better understanding of the flow of a program and discover scenarios that you might not even have thought of while developing.
By logging useful data from the right places, you can not only debug errors easily but also use the data to analyze the performance of the application to plan for scaling or look at usage patterns to plan for marketing.
The logging module in Python is a ready-to-use and powerful module that is designed to meet the needs of beginners as well as enterprise teams. It is used by most of the third-party Python libraries, so you can integrate your log messages with the ones from those libraries to produce a homogeneous log for your application.
import logging
Logging
and Print
both can be used to debug the code but still, there are reasons for you to choose log
over print()
.
A Log file
contains a log message with other information such as line number, module name, date, time, etc. On the other hand, a print
statement only has a log message.
A Log file
will save the records of the application even after it is closed but a print statement will lose out all the records and log message just after the execution stops.
The only time when you should consider using a print()
statement over a log
is when you need to display a help statement on the command line.
There are five ways we can display a log message in python with setting log levels: DEBUG, INFO, WARNING, ERROR, and CRITICAL.
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
In the code output above, there are two things to notice:
- The root is the default logger for all the logs.
- There are only three log messages printed. The reason behind this is the severity level of log messages and messages higher than or equal to the severity level of
WARNING
will only get printed.
- DEBUG: It is used for diagnosing the problem. It gives a piece of detailed information about the problem. The severity level is 10.
- INFO: It gives the confirmation message of the successful execution of the program. The severity level is 20.
- WARNING: The message is for when an unexpected situation occurs. The severity level is 30.
- ERROR: It is due to a more serious problem than a warning. It can be due to some inbuilt error Like syntax or logical error. The severity level is 40.
- CRITICAL: It occurs when the program execution stops and it can not run itself anymore. The severity level is 50.
We can print the DEBUG
and INFO
messages too by changing the basic configuration of the logger with the help of basicConfig(**kwargs)
.
There are some parameters that are commonly used in this:
- level: To change the root logger to a specified severity level.
- filename: Filename where the logs going to be stored.
- filemode: If a filename is given then this specifies the file mode in which the file will open. default is append (a )
- format: This is the format of the log message.
- datefmt : It specified the date and time format.
Simple example:
import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt='%d/%m/%Y %H:%M:%S')
logging.debug('This is Debug Message')
logging.info('This is an info message')
-------------------------CONSOLE OUTPUT------------------
12/05/2021 20:46:41 - root - DEBUG - This is Debug Message
12/05/2021 20:46:41 - root - INFO - This is an info message
Similarly, for logging the result** in a file** you can add filename and file mode to basicConfig
:
logging.basicConfig(level=logging.DEBUG,filename='data.log', filemode='w')
import logging
logging.basicConfig(level=logging.DEBUG,filename='data.log', filemode='a', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt='%d/%m/%Y %H:%M:%S')
name = input("Enter Your Name:\n")
logging.info(f"{name} has logged in successfully !!")
-----------------data.log file output-------------
12/05/2021 21:01:37 - root - INFO - Tom has logged in successfully !!
12/05/2021 21:02:47 - root - INFO - Karl has logged in successfully !!
12/05/2021 21:03:27 - root - INFO - Rahul has logged in successfully !!
The logging module
can also able to trace and showcase full exceptional errors that occur during the execution of the program. With the help of exc_info
with set argument as True
:
import logging
a = 10
b = 0
try:
c = a / b
except Exception as e:
logging.error("Exception Occurred", exc_info=True) ## At default it is True
----------------------------------CONSOLE OUTPUT------------------------
ERROR:root:Exception Occurred ## If exc_info=False then only this message will print
Traceback (most recent call last):
File "C:\Users\minde\Desktop\ds\python\advance_concepts\logging_code.py", line 5, in <module>
c = a / b
ZeroDivisionError: division by zero
-
Create a simple program that would log all inputs from the terminal. Configs must show all additional data (time, date, level etc.)
-
Create a program:
- Create and use a function that returns a sum of numbers
- Create and use a function that returns a square root of a number
- Create and use a function that returns a number of symbols of a string
- Create and use a function that returns a division of numbers
- Save all the results of these 4 functions to a log file
- All the results should be saved in this format:
date/time - Log level - message
- Log level of all logs should be
INFO
-
Change the 2nd exercise program:
- Make it so an exception would be saved to a log file if you pass a string value to the square root function
- Make it so an exception would be saved to a log file if you pass a 0 (division from zero) to the division of numbers function