如何在python中专业地导入软件包和一些自定义设置?

问题描述

我正在使用具有某些自定义设置的日志记录包,并且我有多个目录,子目录和文件,因此我正在应对和粘贴每个文件中的日志记录设置,这似乎并不专业。喜欢

我正在处理每个文件

import logging
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',level=logging.INFO,datefmt='%Y-%m-%d %H:%M:%s')

我想把它放在utils.py这样的文件中,所以我只从utils导入函数并开始工作。

我尝试做的,文件名:utils.py

def logger():
    import logging
    logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',datefmt='%Y-%m-%d %H:%M:%s')

正在导入其他文件,似乎不起作用,文件名:database_service.py

from src.utils import logger as logging
def connection_creator():
    try:
        client = MongoClient(DB_MACHINE,DB_PORT,serverSelectionTimeoutMS=2000)
        status = client.server_info()['ok']
        logging.info(f'Connection created Successfully! ["Status":{status}] `localhost` Port: `27017`')
        db_connection = client['techexpert']
        return db_connection
    except Exception as error:
        logging.error(f'in Creating connection `localhost` Port: `27017`! {error}')

db_connection = connection_creator()

在未解决的参考中记录logging.info()那么,导入这些类型的设置的最佳专业方法是什么。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)