-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlog.py
More file actions
26 lines (23 loc) · 1.06 KB
/
log.py
File metadata and controls
26 lines (23 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import logging
# Set up basic logging configuration
logging.basicConfig(
level=logging.INFO, # Minimum level to log
format='%(asctime)s - %(levelname)s - %(message)s', # Log format
filename='app.log', # Log output file
filemode='w' # Write mode (overwrites each run)
)
def divide_numbers(a, b):
try:
result = a / b
logging.info(f"Division successful: {a} / {b} = {result}") # Info log for successful division
return result
except ZeroDivisionError as e:
logging.error("Attempted to divide by zero.") # Error log for division by zero
except TypeError as e:
logging.error("Invalid types provided for division.") # Error log for type error
except Exception as e:
logging.exception("An unexpected error occurred.") # Logs exception with traceback
# Testing the logging function
divide_numbers(10, 2) # Successful division
divide_numbers(10, 0) # Division by zero
divide_numbers(10, 'a') # Type error