两个单独的 python 文件的两个记录器
Two loggers for two separate python files
我有两个文件 entrypoint.py
和 op_helper.py
,我试图将每个脚本日志发送到不同的日志文件(webhook.log
和 op.log
)。我用两个不同的日志 类.
设置了我的 logger.py
文件
import logging
from logging.handlers import TimedRotatingFileHandler
class Logger:
def create_timed_rotating_log(self, path):
logger = logging.getLogger("Rotating Log")
logger.setLevel(logging.INFO)
handler = TimedRotatingFileHandler(path,
when="d",
interval=1,
backupCount=7)
formatter = logging.Formatter(fmt='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger
class WebhookLogger:
def create_timed_rotating_log(self, path):
logger = logging.getLogger("Rotating Log")
logger.setLevel(logging.INFO)
handler = TimedRotatingFileHandler(path,
when="d",
interval=1,
backupCount=7)
formatter = logging.Formatter(fmt='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger
today = datetime.today()
month = today.strftime("%B")
logger = Logger().create_timed_rotating_log(f'./{month + str(today.year)}Logger.log')
webhook_logger = WebhookLogger().create_timed_rotating_log(f'./{month + str(today.year)}WebhookLogger.log')
在我的 entrypoint.py
脚本中:
from logger import webhook_logger
webhook_logger.info("Something to log")
在我的 op_helper.py
脚本中:
from logger import logger
logger.info("Something else to log")
但是当我 运行 脚本时,两个日志语句都会记录到两个日志文件中。
2021-10-15 14:17:51 INFO Something to log
2021-10-15 14:17:51 INFO Something else to log
任何人都可以向我解释这里发生了什么,以及我做错了什么吗?
提前致谢!
这里是 logging 文档的摘录(粗体是我的):
logging.getLogger(name=None)
Return a logger with the specified name or, if name is None, return a logger which is the root logger of the hierarchy. If specified, the name is typically a dot-separated hierarchical name like ‘a’, ‘a.b’ or ‘a.b.c.d’. Choice of these names is entirely up to the developer who is using logging.
All calls to this function with a given name return the same logger instance. This means that logger instances never need to be passed between different parts of an application.
...
因此,解决方案是为您的第二个记录器指定一个不同的名称。
编辑:
但是请记住,如您所见,调用 getLogger 要么创建一个新实例(如果给定名称下的实例不存在),要么 returns 一个已经存在的实例。因此,接下来的每条指令只会修改现有的记录器。如果您打算使用 类 创建一种记录器类型的多个实例,则该方法将行不通。现在,它们都做完全相同的事情,所以也不需要两个单独的 类 。如您所见,logging 不适合与面向对象的方法一起使用,因为对象已经在别处实例化并且可以作为“全局”对象访问。但这只是旁注。
我有两个文件 entrypoint.py
和 op_helper.py
,我试图将每个脚本日志发送到不同的日志文件(webhook.log
和 op.log
)。我用两个不同的日志 类.
logger.py
文件
import logging
from logging.handlers import TimedRotatingFileHandler
class Logger:
def create_timed_rotating_log(self, path):
logger = logging.getLogger("Rotating Log")
logger.setLevel(logging.INFO)
handler = TimedRotatingFileHandler(path,
when="d",
interval=1,
backupCount=7)
formatter = logging.Formatter(fmt='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger
class WebhookLogger:
def create_timed_rotating_log(self, path):
logger = logging.getLogger("Rotating Log")
logger.setLevel(logging.INFO)
handler = TimedRotatingFileHandler(path,
when="d",
interval=1,
backupCount=7)
formatter = logging.Formatter(fmt='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger
today = datetime.today()
month = today.strftime("%B")
logger = Logger().create_timed_rotating_log(f'./{month + str(today.year)}Logger.log')
webhook_logger = WebhookLogger().create_timed_rotating_log(f'./{month + str(today.year)}WebhookLogger.log')
在我的 entrypoint.py
脚本中:
from logger import webhook_logger
webhook_logger.info("Something to log")
在我的 op_helper.py
脚本中:
from logger import logger
logger.info("Something else to log")
但是当我 运行 脚本时,两个日志语句都会记录到两个日志文件中。
2021-10-15 14:17:51 INFO Something to log
2021-10-15 14:17:51 INFO Something else to log
任何人都可以向我解释这里发生了什么,以及我做错了什么吗?
提前致谢!
这里是 logging 文档的摘录(粗体是我的):
logging.getLogger(name=None)
Return a logger with the specified name or, if name is None, return a logger which is the root logger of the hierarchy. If specified, the name is typically a dot-separated hierarchical name like ‘a’, ‘a.b’ or ‘a.b.c.d’. Choice of these names is entirely up to the developer who is using logging.
All calls to this function with a given name return the same logger instance. This means that logger instances never need to be passed between different parts of an application.
... 因此,解决方案是为您的第二个记录器指定一个不同的名称。
编辑: 但是请记住,如您所见,调用 getLogger 要么创建一个新实例(如果给定名称下的实例不存在),要么 returns 一个已经存在的实例。因此,接下来的每条指令只会修改现有的记录器。如果您打算使用 类 创建一种记录器类型的多个实例,则该方法将行不通。现在,它们都做完全相同的事情,所以也不需要两个单独的 类 。如您所见,logging 不适合与面向对象的方法一起使用,因为对象已经在别处实例化并且可以作为“全局”对象访问。但这只是旁注。