我正在尝试构建一个通过 http 发送日志消息的自定义日志处理程序。但是,我不想添加带有addHandler()方法的处理程序。我希望直接在日志根级别配置自定义处理程序,logging.basicConfig()特别是确保从具有不同记录器的不同模块触发的所有日志消息都通过 http 发送。我怎样才能做到这一点?
这是我当前的代码
"""Entrypoint to execute python scripts."""
import argparse
import logging
import sys
import utils
from batch import Batch
from data_source import DataSource
# Load default logging configuration
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
log = logging.getLogger(__name__)
# Define custom log handler
class CustomLogHandler(logging.Handler):
"""Custom logs handler to send log messages to GraphQL API."""
def __init__(self, authorization: str, batch_id: int):
logging.Handler.__init__(self)
self.authorization = authorization
self.batch_id = str(batch_id)
def emit(self, log_record):
file_name = log_record.name
log_level = log_record.levelname
log_message = self.format(log_record)
# Do stuff here...
utils.execute_graphql_request(self.authorization, mutation)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Entry point to execute data quality scripts.')
parser.add_argument('authorization', type=str, help='Authentication token of the user')
parser.add_argument('method', type=str, help='Method to be executed: execute_batch, test_data_source')
parser.add_argument('id', type=int, help='Id of the object on which to execute the method.')
arguments = parser.parse_args()
authorization = arguments.authorization
method = arguments.method
拉丁的传说
青春有我
随时随地看视频慕课网APP
相关分类