Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用logging.basicConfig()将自定义处理程序添加到日志根级别_Python_Logging_Handler - Fatal编程技术网

Python 使用logging.basicConfig()将自定义处理程序添加到日志根级别

Python 使用logging.basicConfig()将自定义处理程序添加到日志根级别,python,logging,handler,Python,Logging,Handler,我正在尝试构建一个自定义日志处理程序,它通过http发送日志消息。但是,我不想使用addHandler()方法添加处理程序。我希望使用logging.basicConfig()直接在日志根级别配置自定义处理程序,特别是确保通过http发送从不同模块触发的所有日志消息,这些模块使用不同的记录器。我怎样才能做到这一点 这是我目前的代码 “执行python脚本的入口点。”“” 导入argparse 导入日志记录 导入系统 导入UTIL 从批导入批 从数据源导入数据源 #加载默认日志记录配置 loggi

我正在尝试构建一个自定义日志处理程序,它通过http发送日志消息。但是,我不想使用
addHandler()
方法添加处理程序。我希望使用
logging.basicConfig()
直接在日志根级别配置自定义处理程序,特别是确保通过http发送从不同模块触发的所有日志消息,这些模块使用不同的记录器。我怎样才能做到这一点

这是我目前的代码

“执行python脚本的入口点。”“”
导入argparse
导入日志记录
导入系统
导入UTIL
从批导入批
从数据源导入数据源
#加载默认日志记录配置
logging.basicConfig(level=logging.INFO,格式='%(asctime)s-%(name)s-%(levelname)s-%(message)s')
log=logging.getLogger(_名称__)
#定义自定义日志处理程序
类CustomLogHandler(logging.Handler):
“”“将日志消息发送到GraphQL API的自定义日志处理程序。”“”
def_uuuinit_uuuu(自我,授权:str,批处理id:int):
logging.Handler.\uuuuu init\uuuuu(self)
自我授权=授权
self.batch\u id=str(batch\u id)
def排放(自身、日志记录):
file\u name=log\u record.name
log\u level=log\u record.levelname
日志消息=self.format(日志记录)
#在这里做事。。。
utils.execute_graphql_请求(自我授权、变异)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
parser=argparse.ArgumentParser(description='Entry point to execute data quality scripts')
add_参数('authorization',type=str,help='user的身份验证令牌')
add_参数('method',type=str,help='method to execute:execute_batch,test_data_source')
add_参数('id',type=int,help='要在其上执行方法的对象的id'。)
arguments=parser.parse_args()
authorization=arguments.authorization
方法=参数。方法
如果方法==“执行批处理”:
batch_id=arguments.id
#覆盖日志根基本配置,以便在执行批处理时将日志发送到GraphQL API
logging.basicConfig(
级别=logging.INFO,
格式='(asctime)s-%(名称)s-%(级别名称)s-%(消息)s',
handlers=[logging.StreamHandler(),CustomLogHandler(授权,批处理id)])
批次=批次()
批处理执行(授权、批处理id)
埃利夫[…]

向根记录器添加处理程序有两种方法, 将其添加到
基本配置中

    logging.basicConfig(
        level=logging.INFO,
        format="%(asctime)s [%(levelname)s] %(message)s",
        handlers=[
            logging.StreamHandler(),
            CustomLogHandler()
        ]
    )
或者在完成基本配置后,可以将其添加到根记录器(以确保传播),如下所示:

root_log = logging.getLogger()
root_log.addHandler(CustomLogHandler())
那么每当你做一个

import logging

log = logging.getLogger(__name__)
log.info("message")
“消息”日志应通过根记录器发送