0

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:\Users\Lukmana\Desktop\user_count\usercount.py", line 121, in <module>
    logger.info("-Total count initiated-➕")
Message: '-Total count initiated-➕'
Arguments: ()
Check logfile 'C:\user_count\usercount.20.103.log' for details
vvvvv
  • 25,404
  • 19
  • 49
  • 81
Flint_Lockwood
  • 164
  • 1
  • 15

1 Answers1

1

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.

Vinay Sajip
  • 95,872
  • 14
  • 179
  • 191