简体   繁体   English

没有为logger“xhtml2pdf”找到处理程序

[英]No handlers could be found for logger “xhtml2pdf”

I'm using xhtmltopdf.pisa for generating pdf from html template. 我正在使用xhtmltopdf.pisa从html模板生成pdf。 The code was working fine. 代码工作正常。

pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")), dest=result, link_callback=fetch_resources, default_css=open(pdf_css_path, 'r').read())

But some times I noticed that, the above code is not working. 但有时我注意到,上面的代码不起作用。 The error threw was No handlers could be found for logger "xhtml2pdf" . 抛出的错误是No handlers could be found for logger "xhtml2pdf" Only some times I can find this error, but other times just works fine. 只有一些时间我可以找到这个错误,但其他时候只是工作正常。

Any help is appreciated. 任何帮助表示赞赏。

The reason for the error is that, the pisa module some time uses the Python's logging module to log warnings or errors. 出错的原因是,比萨模块有时会使用Python的日志记录模块来记录警告或错误。 By default it is trying to access a logger named xhtml2pdf . 默认情况下,它尝试访问名为xhtml2pdf的记录器。 So you need to define a handler for xhtml2pdf on your settings file. 因此,您需要在设置文件中为xhtml2pdf定义处理程序。

Django's logging settings would look like this, Django的日志设置看起来像这样,

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
    'standard': {
        'format': "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
        'datefmt': "%d/%b/%Y %H:%M:%S"
        },
    },
    'handlers': {
        'logfile': {
            'level': 'DEBUG',
            'filename': BASE_DIR + "/Log/info.log"
        },
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
            'formatter': 'standard'
        },
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'propagate': True,
            'level': 'WARN',
        },
        'xhtml2pdf': {
            'handlers': ['logfile'],
            'level': 'DEBUG'
       },
   }
}

The reason for this error's inconsistacy is bacause, pisa maynot use logger all the time. 这个错误不一致的原因是因为比萨可能不会一直使用记录器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM