I'm getting logging error (UnicodeEncodeError
) while trying log and print url which contain not ASCII letter. I specified encoding while setting up logger in my_logging.py
my_logging.py
import logging
import sys
from pathlib import Path
def get_logger(filepath: Path) -> None:
logging.basicConfig(
level=logging.DEBUG,
encoding='utf-8',
format="[{asctime},{msecs:03.0f}]:[{levelname}]:{message}",
datefmt='%d.%m.%Y %H:%M:%S',
style='{',
handlers=[
logging.FileHandler(filepath, mode='a'),
logging.StreamHandler(sys.stdout),
]
)
logging.getLogger('asyncio').setLevel(logging.WARNING)
def log_and_print(msg: str) -> None:
logging.info(msg)
print(msg)
Error:
--- Logging error ---
Traceback (most recent call last):
File "C:\Users\vboxuser\AppData\Local\Programs\Python\Python311\Lib\logging\__init__.py", line 1113, in emit
stream.write(msg + self.terminator)
File "C:\Users\vboxuser\AppData\Local\Programs\Python\Python311\Lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'charmap' codec can't encode characters in position 156-157: character maps to <undefined>
Call stack:
File "C:\Users\vboxuser\30.rossiya-airlines\app.py", line 162, in run
asyncio.run(download_reports_for_month(login=self.login, password=self.password, month=self.month))
File "C:\Users\vboxuser\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 190, in run
return runner.run(main)
File "C:\Users\vboxuser\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 118, in run
return self._loop.run_until_complete(task)
File "C:\Users\vboxuser\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 640, in run_until_complete
self.run_forever()
File "C:\Users\vboxuser\AppData\Local\Programs\Python\Python311\Lib\asyncio\windows_events.py", line 321, in run_forever
super().run_forever()
File "C:\Users\vboxuser\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 607, in run_forever
self._run_once()
File "C:\Users\vboxuser\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 1922, in _run_once
handle._run()
File "C:\Users\vboxuser\AppData\Local\Programs\Python\Python311\Lib\asyncio\events.py", line 80, in _run
self._context.run(self._callback, *self._args)
File "C:\Users\vboxuser\30.rossiya-airlines\scrapper.py", line 138, in download_reports_for_month
await _download_reports_for_month(login=login, password=password, month=month)
File "C:\Users\vboxuser\30.rossiya-airlines\scrapper.py", line 169, in _download_reports_for_month
log_and_print(f'Start downloading {url=}')
File "C:\Users\vboxuser\30.rossiya-airlines\my_logging.py", line 27, in log_and_print
logging.info(msg)
Message: "Start downloading url='https://edu.rossiya-airlines.com/workplan/view_flight_report-1?flight_date=02-08-2023&flight_number=ФВ6321&legnum=1&departure_airport_id=705&arrival_airport_id=307&id_para=1214928'"
Arguments: ()
Line raising error:
log_and_print(f'Start downloading {url=}')
I can workaround exceptions by encoding every msg manually:
def log_and_print(msg: str) -> None:
logging.info(msg.encode('utf-8')
print(msg)
But then binary prefix appears. I don't like it so much and want to understand why passing encoding='utf-8'
in logging.BasicConfig
is not working as I expect. What am i missing?
Windows 10
, Python 3.11.4