Py学习  »  Python

如何避免Python中的默认事件日志记录

Deepak • 5 年前 • 1377 次点击  

我开始在python中使用日志,我想将日志写入文件。

下面是当前正在进行的示例日志记录:


将事件名称从before-call.api gateway更改为before-call.api-gateway
正在从request-created.machinelearning更改事件名称。 预测到请求创建。机器学习。 预测将区域的配置变量设置为“us-west-2”

我如何实例化记录器

logger = logging.getLogger("S3_transfer")

def set_log_output_file(logname):
    if not os.path.exists('logs'):
        os.makedirs('logs')
    logging.basicConfig(filename='logs/{}.log'.format(logname),
                        filemode='a',
                        format='%(asctime)s %(levelname)-8s %(message)s',
                        datefmt='%a, %d %b %Y %H:%M:%S',
                        level=logging.DEBUG)

def get_logger():
    """
    Retrieves the current logger
    :return: Logger
    """
    return logger

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/55641
 
1377 次点击  
文章 [ 1 ]  |  最新文章 5 年前
4xel
Reply   •   1 楼
4xel    5 年前

发生这种情况的原因是,具有相同名称的多个调用logging.getLogger()将始终返回相同的Logger实例。python包中有一个实践,通过 logging.getLogger(__name__) ,它允许我们在一个地方获取所有日志并配置一次日志记录器。但是如果不想看到外部包中的任何日志,只需替换 __name__ 在您的文件中使用您自己的记录器名称,并设置基本配置,将其写入文件。