FabianBartl / HARIBOT

A discord bot specialized for the "MINT TANK Community" discord server. Bot invite below:
https://discord.com/oauth2/authorize?client_id=1024235031037759500&permissions=8&scope=bot
MIT License
2 stars 3 forks source link

FileHandler of logger cannot handle emojis (unicode characters) and raises an error #16

Open FabianBartl opened 2 years ago

FabianBartl commented 2 years ago

Logfile extract:

2022-09-29 20:36:23,005 |    DEBUG | Dispatching event socket_event_type
2022-09-29 20:36:23,006 |    DEBUG | Dispatching event message
--- Logging error ---
Traceback (most recent call last):
  File "C:\Program Files\Python39\lib\logging\__init__.py", line 1086, in emit
    stream.write(msg + self.terminator)
  File "C:\Program Files\Python39\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\U0001fa99' in position 74: character maps to <undefined>
Call stack:
  File "C:\Users\fabia\OneDrive\Dokumente\GitHub\HARIBOT\src\main.py", line 331, in <module>
    if __name__ == "__main__": bot.run(TOKEN.DISCORD)
  File "C:\Users\fabia\AppData\Roaming\Python\Python39\site-packages\nextcord\client.py", line 853, in run
    loop.run_forever()
  File "C:\Program Files\Python39\lib\asyncio\windows_events.py", line 321, in run_forever
    super().run_forever()
  File "C:\Program Files\Python39\lib\asyncio\base_events.py", line 596, in run_forever
    self._run_once()
  File "C:\Program Files\Python39\lib\asyncio\base_events.py", line 1890, in _run_once
    handle._run()
  File "C:\Program Files\Python39\lib\asyncio\events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "C:\Users\fabia\AppData\Roaming\Python\Python39\site-packages\nextcord\client.py", line 502, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\fabia\OneDrive\Dokumente\GitHub\HARIBOT\src\main.py", line 93, in on_message
    LOG.LOGGER.debug(f"(msg sent) {channel.name} - {author.display_name}: '{content}'")
Message: "(msg sent) bot-test - Fabian Bartl: '🪙'"
Arguments: ()
2022-09-29 20:36:23,006 |    DEBUG | (msg sent) bot-test - Fabian Bartl: '🪙'
2022-09-29 20:36:23,007 |    DEBUG | guilds/1023614599502774362 data updated: {'messages_count': 522, 'users_count': 147, 'bots_count': 2, 'words_count': 8926, 'letters_count': 119582}
2022-09-29 20:36:23,008 |    DEBUG | users/386249332711620608 data updated: {'messages_count': 101, 'words_count': 334, 'letters_count': 2274}

-> UnicodeEncodeError: 'charmap' codec can't encode character '\U0001fa99' in position 74: character maps to <undefined>

FabianBartl commented 2 years ago

logging.StreamHandler() works:

2022-09-29 20:36:23,006 |    DEBUG | (msg sent) bot-test - Fabian Bartl: '🪙'


logging.FileHandler() raises the error:

UnicodeEncodeError: 'charmap' codec can't encode character '\U0001fa99' in position 74: character maps to <undefined>