-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathAdd login
More file actions
40 lines (34 loc) · 1.13 KB
/
Add login
File metadata and controls
40 lines (34 loc) · 1.13 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import logging
import configparser
from datetime import datetime
# Step 1: Read configuration
config = configparser.ConfigParser()
# Configuration file name
config.read('config.ini')
# Get values from config file
log_file = config['LOGGING']['log_file']
log_level = config['LOGGING']['log_level']
# Step 2: Configure logging
logging.basicConfig(
filename=log_file,
level=getattr(logging, log_level.upper(), logging.INFO),
format='%(asctime)s - %(levelname)s - %(message)s'
)
# Step 3: Example program logic
def main():
logging.info("Program started")
try:
# Example calculation
a = int(input("Enter a number: "))
b = int(input("Enter another number: "))
result = a / b
print(f"Result = {result}")
logging.info(f"Calculation successful: {a} / {b} = {result}")
except ZeroDivisionError:
print("Error: Cannot divide by zero.")
logging.error("Division by zero error occurred.")
except Exception as e:
print("An unexpected error occurred:", e)
logging.exception("Unexpected error")
finally:
logging.info("Program ended")