UTF-8 In Python logging, how?

前端 未结 6 435
春和景丽
春和景丽 2020-12-02 15:45

I\'m trying to log a UTF-8 encoded string to a file using Python\'s logging package. As a toy example:

import logging

def logging_test():
    handler = log         


        
6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-02 16:17

    I had a similar problem running Django in Python3: My logger died upon encountering some Umlauts (äöüß) but was otherwise fine. I looked through a lot of results and found none working. I tried

    import locale; 
    if locale.getpreferredencoding().upper() != 'UTF-8': 
        locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') 
    

    which I got from the comment above. It did not work. Looking at the current locale gave me some crazy ANSI thing, which turned out to mean basically just "ASCII". That sent me into totally the wrong direction.

    Changing the logging format-strings to Unicode would not help. Setting a magic encoding comment at the beginning of the script would not help. Setting the charset on the sender's message (the text came from a HTTP-reqeust) did not help.

    What DID work was setting the encoding on the file-handler to UTF-8 in settings.py. Because I had nothing set, the default would become None. Which apparently ends up being ASCII (or as I'd like to think about: ASS-KEY)

        'handlers': {
            'file': {
                'level': 'DEBUG',
                'class': 'logging.handlers.TimedRotatingFileHandler',
                'encoding': 'UTF-8', # <-- That was missing.
                ....
            },
        },
    

提交回复
热议问题