简体   繁体   中英

No handlers could be found for logger “xhtml2pdf”

I'm using xhtmltopdf.pisa for generating pdf from html template. 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" . 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. By default it is trying to access a logger named xhtml2pdf . So you need to define a handler for xhtml2pdf on your settings file.

Django's logging settings would look like this,

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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