-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger_config.py
35 lines (33 loc) · 1.44 KB
/
logger_config.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
import shutil
import queue
from logging.handlers import RotatingFileHandler, QueueHandler, QueueListener
logger = logging.getLogger("pastel_mining_block_supernode_validator_api")
def setup_logger():
if logger.handlers:
return logger
old_logs_dir = 'old_logs'
if not os.path.exists(old_logs_dir):
os.makedirs(old_logs_dir)
logger.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
log_file_path = 'pastel_mining_block_supernode_validator_api.log'
log_queue = queue.Queue(-1) # Create a queue for the handlers
fh = RotatingFileHandler(log_file_path, maxBytes=10*1024*1024, backupCount=5)
fh.setFormatter(formatter)
def namer(default_log_name): # Function to move rotated logs to the old_logs directory
return os.path.join(old_logs_dir, os.path.basename(default_log_name))
def rotator(source, dest):
shutil.move(source, dest)
fh.namer = namer
fh.rotator = rotator
sh = logging.StreamHandler() # Stream handler
sh.setFormatter(formatter)
queue_handler = QueueHandler(log_queue) # Create QueueHandler
queue_handler.setFormatter(formatter)
logger.addHandler(queue_handler)
listener = QueueListener(log_queue, fh, sh) # Create QueueListener with real handlers
listener.start()
logging.getLogger('sqlalchemy.engine').setLevel(logging.WARNING) # Configure SQLalchemy logging
return logger