Python 无法全局访问记录器
在我的项目中,我有两个文件:Python 无法全局访问记录器,python,python-3.6,Python,Python 3.6,在我的项目中,我有两个文件: main.py和 方法.py 在作为入口点的main.py中,我对记录器进行了如下初始化: logging.basicConfig(filename='log.txt', filemode='w', format='%(asctime)s - %(message)s', level=logging.INFO) #Creating an object logger=logging.getLogger() #Setting the threshold of logge
logging.basicConfig(filename='log.txt', filemode='w', format='%(asctime)s - %(message)s', level=logging.INFO)
#Creating an object
logger=logging.getLogger()
#Setting the threshold of logger to DEBUG
logger.setLevel(logging.DEBUG)
但是我无法在methods.py中访问它,它有一个包含两个方法的类。为了使它现在能够工作,我正在用这两种方法重新初始化记录器
我真正想要的是在main.py中初始化记录器,在其他文件方法中,我应该能够直接记录:
logger.info('Triggered')
为记录器命名时,您可以在多个文件中访问它:
#main.py
logging.basicConfig(filename='log.txt', filemode='w', format='%(asctime)s - %(message)s', level=logging.INFO)
#Creating an object
logger=logging.getLogger("my_logger")
#Setting the threshold of logger to DEBUG
logger.setLevel(logging.DEBUG)
#methods.py
logger=logging.getLogger("my_logger")
logger.debug("Some log")