具有该项目配置:
- src/
|-- classes/
|-- Logger.py
|-- Browser.py
|-- ubigeo.py
- main.py
我正在尝试使用python-logstash-logger
类中的Logger.py
设置来打印Browser.py
类,ubigeo.py
和main.py
脚本中的日志:
#Logger.py
import logging, logstash
class LoggerSingleton:
_shared_state = {}
def __init__(self):
self.__dict__ = self._shared_state
class Logger(LoggerSingleton):
def __init__(self):
LoggerSingleton.__init__(self)
self.logger = self.init_console_logger()
def init_console_logger(self):
logger = logging.getLogger('python-logstash-logger')
logger.setLevel('DEBUG')
formatter = logging.Formatter('%(asctime)s : %(levelname)s : %(message)s')
sh = logging.StreamHandler()
sh.setLevel(logging.INFO)
sh.setFormatter(formatter)
logger.addHandler(sh)
return logger
def log(self, message):
self.logger.info(message)
主要问题是,当我有多个相同类的实例时,日志会多次打印。
#main.py
from src.classes import Logger
if __name__ == "__main__":
logger_handler = Logger()
logger_handler.log('Checking python version...')
ubigeo.main()
#Browser.py
from .Logger import Logger
class Browser(Config, Logger):
def __init__(self):
self.logger = Logger()
self.logger.log('Setting up & launching a browser')
#ubigeo.py
from .classes.Browser import Browser
def main():
browser_handler = Browser()
此刻的日志结果是:
2019-04-21 13:25:12,758 : INFO : Checking python version...
2019-04-21 13:25:12,915 : INFO : Setting up & launching a browser
2019-04-21 13:25:12,915 : INFO : Setting up & launching a browser
期望的结果是
2019-04-21 13:25:12,758 : INFO : Checking python version...
2019-04-21 13:25:12,915 : INFO : Setting up & launching a browser