https://www.youtube.com/watch?v=VVT67z28qBs
If you aren’t using logs right now, you should start thinking about adding them to your code whenever useful. Python comes with a logging module built in so there’s no need to install anything extra.
Logging in Python is the process of tracking and recording important events that occur while the software is running. It is usually used to monitor errors and the performance of the software. Logging in Python can be done using the built-in logging module or third-party libraries such as Logbook. The logging module provides a range of different logging levels that allow developers to choose the level of detail they want to record. By using the logging module, developers can easily store error messages, debug output, and other important data.
import logging
# DEBUG: Detailed information, typically of interest only when diagnosing problems.
# INFO: Confirmation that things are working as expected.
# WARNING: An indication that something unexpected happened, or indicative of some problem in the near future
# (e.g. ‘disk space low’). The software is still working as expected.
# ERROR: Due to a more serious problem, the software has not been able to perform some function.
# CRITICAL: A serious error, indicating that the program itself may be unable to continue running.
logging.basicConfig(filename='test.log', level=logging.DEBUG,
format='%(asctime)s:%(levelname)s:%(message)s')
def add(x, y):
"""Add Function"""
return x + y
def subtract(x, y):
"""Subtract Function"""
return x - y
def multiply(x, y):
"""Multiply Function"""
return x * y
def divide(x, y):
"""Divide Function"""
return x / y
num_1 = 20
num_2 = 10
add_result = add(num_1, num_2)
logging.debug('Add: {} + {} = {}'.format(num_1, num_2, add_result))
sub_result = subtract(num_1, num_2)
logging.debug('Sub: {} - {} = {}'.format(num_1, num_2, sub_result))
mul_result = multiply(num_1, num_2)
logging.debug('Mul: {} * {} = {}'.format(num_1, num_2, mul_result))
div_result = divide(num_1, num_2)
logging.debug('Div: {} / {} = {}'.format(num_1, num_2, div_result))
The code contains four functions - add, subtract, multiply and divide - that take two numbers as inputs and return the respective result of the operation. Finally, two numbers are assigned and the four functions are called with the two numbers as arguments. The result of each operation is logged using the logging module.
Playing this script results in a file being created, ‘test.log’ with the following:
2023-01-14 02:39:34,078:DEBUG:Add: 20 + 10 = 30
2023-01-14 02:39:34,078:DEBUG:Sub: 20 - 10 = 10
2023-01-14 02:39:34,078:DEBUG:Mul: 20 * 10 = 200
2023-01-14 02:39:34,078:DEBUG:Div: 20 / 10 = 2.0
2023-01-14 02:39:38,039:DEBUG:Add: 20 + 10 = 30
2023-01-14 02:39:38,040:DEBUG:Sub: 20 - 10 = 10
2023-01-14 02:39:38,040:DEBUG:Mul: 20 * 10 = 200
2023-01-14 02:39:38,040:DEBUG:Div: 20 / 10 = 2.0
You must import logging.
There are five standard logging levels:
The default level for logging is set to Warning, which means it will capture anything that is warning or above, ignoring debug and info.
In order to change the level of logging, set the level of basicConfig to logging.DEBUG with this code:
logging.basicConfig(filename='test.log', level=logging.DEBUG,
format='%(asctime)s:%(levelname)s:%(message)s')
import logging
logging.basicConfig(filename='employee.log', level=logging.INFO,
format='%(levelname)s:%(message)s')
class Employee:
"""A sample Employee class"""
def __init__(self, first, last):
self.first = first
self.last = last
logging.info('Created Employee: {} - {}'.format(self.fullname, self.email))
@property
def email(self):
return '{}.{}@email.com'.format(self.first, self.last)
@property
def fullname(self):
return '{} {}'.format(self.first, self.last)
emp_1 = Employee('John', 'Smith')
emp_2 = Employee('Corey', 'Schafer')
emp_3 = Employee('Jane', 'Doe')