Python 如何使用dictConfig语法指定具有必需参数的日志处理程序类?
我想在Django中使用日志处理程序。我使用dictConfig语法指定了我的记录器 以下是Notifer自己的文档中的一个示例:Python 如何使用dictConfig语法指定具有必需参数的日志处理程序类?,python,django,logging,Python,Django,Logging,我想在Django中使用日志处理程序。我使用dictConfig语法指定了我的记录器 以下是Notifer自己的文档中的一个示例: >>> import logging >>> from notifiers.logging import NotificationHandler >>> log = logging.getLogger(__name__) >>> defaults = { ... 'token': 'fo
>>> import logging
>>> from notifiers.logging import NotificationHandler
>>> log = logging.getLogger(__name__)
>>> defaults = {
... 'token': 'foo,
... 'user': 'bar
... }
>>> hdlr = NotificationHandler('pushover', defaults=defaults)
>>> hdlr.setLevel(logging.ERROR)
>>> log.addHandler(hdlr)
>>> log.error('And just like that, you get notified about all your errors!')
dictConfig语法如下所示:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'file': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': '/path/to/django/debug.log',
},
},
'loggers': {
'django': {
'handlers': ['file'],
'level': 'DEBUG',
'propagate': True,
},
},
}
def slack_handler_factory():
return NotificationHandler(
'slack',
defaults={
'webhook_url': SLACK_WEBHOOK_URL,
},
)
如何使用后一种语法添加通知程序处理程序?我找不到将第一个必需参数设置为NotificationHandler的方法。也许可以指定自己的工厂类来实例化处理程序。这可以通过下面描述的语法完成:
也许可以指定自己的工厂类来实例化处理程序。这可以通过下面描述的语法完成:
我认为应该通过添加新的: 使现代化 通过覆盖NotificationHandler,解决方案可以如下所示:
class CustomNotificationHandler(NotificationHandler):
def __init__(self, *args, **kwargs):
defaults = {
'token': 'foo,
'user': 'bar
}
super(CustomNotificationHandler, self).__init__('pushover', defaults)
并在django日志记录的处理程序中使用它
'notify': {
'level': 'DEBUG',
'class': 'CustomNotificationHandler',
},
我认为应该通过添加新的: 使现代化 通过覆盖NotificationHandler,解决方案可以如下所示:
class CustomNotificationHandler(NotificationHandler):
def __init__(self, *args, **kwargs):
defaults = {
'token': 'foo,
'user': 'bar
}
super(CustomNotificationHandler, self).__init__('pushover', defaults)
并在django日志记录的处理程序中使用它
'notify': {
'level': 'DEBUG',
'class': 'CustomNotificationHandler',
},
感谢@jspcal,我找到了答案。定义如下所示的处理程序工厂:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'file': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': '/path/to/django/debug.log',
},
},
'loggers': {
'django': {
'handlers': ['file'],
'level': 'DEBUG',
'propagate': True,
},
},
}
def slack_handler_factory():
return NotificationHandler(
'slack',
defaults={
'webhook_url': SLACK_WEBHOOK_URL,
},
)
…可以使用-语法将其添加到处理程序中:
感谢@jspcal,我找到了答案。定义如下所示的处理程序工厂:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'file': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': '/path/to/django/debug.log',
},
},
'loggers': {
'django': {
'handlers': ['file'],
'level': 'DEBUG',
'propagate': True,
},
},
}
def slack_handler_factory():
return NotificationHandler(
'slack',
defaults={
'webhook_url': SLACK_WEBHOOK_URL,
},
)
…可以使用-语法将其添加到处理程序中:
但是,通过这种方式,您不能为notifiers.logging.NotificationHandler.指定参数。。这首先是我的问题,是的。顺便说一句,你的答案看起来不错,但是你能看看我更新的答案吗。谢谢,那应该行,但工厂更优雅,不是吗☺️ ?是的,这更简洁:但是通过这种方式,您不能指定通知程序的参数。logging.NotificationHandler。。这首先是我的问题,是的。顺便说一句,你的答案看起来不错,但是你能看看我更新的答案吗。谢谢,那应该行,但工厂更优雅,不是吗☺️ ?是的,那要干净得多: