python 3.6 *logging modul error* UnicodeEncodeError: 'charmap' codec can't encode characters

前端 未结 1 1629
隐瞒了意图╮
隐瞒了意图╮ 2021-01-17 11:45

On Windows 10 the logging module send this error (using scrapy)

# --- Logging error ---
...
# UnicodeEncodeError: \'charmap\' codec can\'t encode characters          


        
相关标签:
1条回答
  • 2021-01-17 12:27

    I'm not using Scrapy but I ran into the same issue with vanilla Python 3.6 logging of not being able to write UTF-8 characters via logging to a file (console worked just fine).

    Based on this comment I added 'utf-8' to my FileHandler initialization. I'm configuring logging via an INI file so it looks like this:

    [handler_file]
    class = FileHandler
    args = (r'log.txt', 'a', 'utf-8')
    level = NOTSET
    formatter = generic
    

    To use logging.basicConfig() I think that you should be able to do the following:

    configure_logging(install_root_handler=False) #override default log settings
    logging.basicConfig(
        handlers=[logging.FileHandler('logfile.log', 'w', 'utf-8')],
        format='%(levelname)s: %(message)s',
        datefmt='%m-%d %H:%M',
        level=logging.INFO #CRITICAL ERROR WARNING  INFO    DEBUG    NOTSET 
    )
    
    0 讨论(0)
提交回复
热议问题