AIL-framework/bin/lib/ail_logger.py

54 lines
1.6 KiB
Python
Raw Normal View History

2023-05-12 15:29:53 +02:00
#!/usr/bin/env python3
# -*-coding:UTF-8 -*
import os
import json
import sys
import logging
2024-08-19 15:45:20 +02:00
import logging.handlers
2023-05-12 15:29:53 +02:00
sys.path.append(os.environ['AIL_BIN'])
##################################
# Import Project packages
##################################
from lib.ConfigLoader import ConfigLoader
config_loader = ConfigLoader()
r_db = config_loader.get_db_conn("Kvrocks_DB")
config_loader = None
2024-08-19 15:45:20 +02:00
LOGGING_CONF_DIR = os.path.join(os.environ['AIL_HOME'], 'configs')
LOGS_DIR = os.path.join(os.environ['AIL_HOME'], 'logs')
2023-05-12 15:29:53 +02:00
def get_config(name=None):
if not name:
name = 'ail.log'
2023-05-12 15:29:53 +02:00
else:
name = f'{name}.log'
2024-08-19 15:45:20 +02:00
with open(os.path.join(LOGGING_CONF_DIR, f'logging.json'), 'r') as f:
2023-05-12 15:29:53 +02:00
config = json.load(f)
2024-08-19 15:45:20 +02:00
config['handlers']['file']['filename'] = os.path.join(LOGS_DIR, name)
2023-05-12 15:29:53 +02:00
return config
2024-08-19 15:45:20 +02:00
def get_access_config(create=False):
logger = logging.getLogger('access.log')
if create:
2024-09-06 15:04:28 +02:00
formatter = logging.Formatter('%(asctime)s - %(ip_address)s - %(user_agent)s - %(levelname)s - %(user_id)s - %(message)s')
2024-08-19 15:45:20 +02:00
# STDOUT
handler = logging.StreamHandler()
handler.setLevel(logging.INFO)
handler.setFormatter(formatter)
logger.addHandler(handler)
# FILE
handler = logging.handlers.RotatingFileHandler(filename=os.path.join(LOGS_DIR, f'access.log'),
maxBytes=10*1024*1024, backupCount=5)
handler.setLevel(logging.INFO)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.propagate = False
return logger