UTF-8 In Python logging, how?

前端 未结 6 439
春和景丽
春和景丽 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:03

    I'm a little late, but I just came across this post that enabled me to set up logging in utf-8 very easily

    Here the link to the post

    or here the code:

    root_logger= logging.getLogger()
    root_logger.setLevel(logging.DEBUG) # or whatever
    handler = logging.FileHandler('test.log', 'w', 'utf-8') # or whatever
    formatter = logging.Formatter('%(name)s %(message)s') # or whatever
    handler.setFormatter(formatter) # Pass handler as a parameter, not assign
    root_logger.addHandler(handler)
    

提交回复
热议问题