-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_logging.py
35 lines (27 loc) · 1.04 KB
/
custom_logging.py
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
import logging
import os
from datetime import datetime
from logging.handlers import RotatingFileHandler
# Directory for log files
log_dir = os.path.join(os.getcwd(), 'logs')
os.makedirs(log_dir, exist_ok=True)
# Log file name with timestamp
log_file = f"{datetime.now().strftime('%d_%m_%Y_%H_%M_%S')}.log"
log_file_path = os.path.join(log_dir, log_file)
# Create logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)
# Create handlers
file_handler = RotatingFileHandler(log_file_path, maxBytes=10*1024*1024, backupCount=5) # 10 MB per file
file_handler.setLevel(logging.INFO)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
# Create formatter and add it to the handlers
formatter = logging.Formatter("[ %(asctime)s ] %(lineno)d %(name)s - %(levelname)s - %(message)s")
file_handler.setFormatter(formatter)
console_handler.setFormatter(formatter)
# Add handlers to the logger
logger.addHandler(file_handler)
logger.addHandler(console_handler)
if __name__ == "__main__":
logger.info("Logging has started")