Is it possible add emoji to log in python

Question:

can we add emoji in our log report?
i got the below error when i add an emoji

UnicodeEncodeError: 'charmap' codec can't encode character 'u2795' in position 75: character maps to <undefined>
Call stack:
  File "c:UsersLukmanaDesktopuser_countusercount.py", line 121, in <module>
    logger.info("-Total count initiated-➕")
Message: '-Total count initiated-➕'
Arguments: ()
Check logfile 'C:user_countusercount.20.103.log' for details
Asked By: Flint_Lockwood

||

Answers:

Of course, just use an appropriate encoding. The following example script

import logging

fh = logging.FileHandler('emojis.log', mode='w', encoding='utf-8')
root = logging.getLogger()
root.addHandler(fh)
root.setLevel(logging.DEBUG)
root.debug('N{smiling face with sunglasses}')
root.debug('N{rolling on the floor laughing}')

outputs emojis to emojis.log:

$ more emojis.log 
 
 

Note that some versions of terminals on Windows may not show emojis properly. And you can of course use U escape sequences as well as the N examples above.

Answered By: Vinay Sajip
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.