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
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.
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
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.