-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
36 lines (27 loc) · 979 Bytes
/
logger.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
36
import logging
import os
from get_time import get_time
# Create a custom logger
logger = logging.getLogger('custom_logger')
# Set the logging level
logger.setLevel(logging.DEBUG)
# Create handlers
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
log_path = '../logs/'
if not os.path.exists(log_path):
os.makedirs(log_path)
logger.info(f"Directory '{log_path}' created.")
else:
logger.info(f"Directory '{log_path}' already exists.")
now_time = get_time()
file_handler = logging.FileHandler(log_path +f'{now_time}.log','a')
file_handler.setLevel(logging.WARNING)
# Create formatters and add them to the handlers
console_formatter = logging.Formatter('%(asctime)s: %(message)s')
console_handler.setFormatter(console_formatter)
file_formatter = logging.Formatter('%(asctime)s: %(message)s')
file_handler.setFormatter(file_formatter)
# Add handlers to the logger
logger.addHandler(console_handler)
logger.addHandler(file_handler)