ail-framework/bin/lib/ail_logger.py

54 lines
1.6 KiB
Python
Raw Normal View History

2023-05-12 13:29:53 +00:00
#!/usr/bin/env python3
# -*-coding:UTF-8 -*
import os
import json
import sys
import logging
2024-08-19 13:45:20 +00:00
import logging.handlers
2023-05-12 13:29:53 +00: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 13:45:20 +00: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 13:29:53 +00:00
def get_config(name=None):
if not name:
name = 'ail.log'
2023-05-12 13:29:53 +00:00
else:
name = f'{name}.log'
2024-08-19 13:45:20 +00:00
with open(os.path.join(LOGGING_CONF_DIR, f'logging.json'), 'r') as f:
2023-05-12 13:29:53 +00:00
config = json.load(f)
2024-08-19 13:45:20 +00:00
config['handlers']['file']['filename'] = os.path.join(LOGS_DIR, name)
2023-05-12 13:29:53 +00:00
return config
2024-08-19 13:45:20 +00:00
def get_access_config(create=False):
logger = logging.getLogger('access.log')
if create:
formatter = logging.Formatter('%(asctime)s - %(ip_address)s - %(levelname)s - %(user_id)s - %(message)s')
# 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