Loguru: UnicodeEncodeError: 'charmap' codec can't encode characters

Created on 7 Aug 2019  Â·  5Comments  Â·  Source: Delgan/loguru

When I Use loguru==0.3.2 Python v3.7.4 on Windows 10 it give me UnicodeEncodeError:
--- Logging error in Loguru Handler #1 ---
Record was: {'elapsed': datetime.timedelta(seconds=2, microseconds=990974), 'exception': None, 'extra': {}, 'file': 'oneforall.py', 'function': 'run', 'level': 'INFOR', 'line': 73, 'message': '\u5f00\u59cb\u8fd0\u884cOneForAll', 'module': 'oneforall', 'name': 'oneforall', 'process': '1408', 'thread': '4480', 'time': datetime(2019, 8, 7, 11, 10, 40, 960312, tzinfo=datetime.timezone(datetime.timedelta(0), 'Greenwich Standard Time'))}
Traceback (most recent call last):
File "c:\python37\lib\site-packages\loguru_handler.py", line 253, in _queued_writer
self._writer(message)
File "c:\python37\lib\site-packages\loguru_logger.py", line 696, in writer
write(m)
File "c:\python37\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 85-88: character maps to
--- End of logging error ---

More detail:https://travis-ci.org/shmilylty/OneForAll/jobs/568803134#L199

Enter file is example.py
loguru config file is config.py

question

Most helpful comment

@glmn In this case, this is not because of sys.stdout encoding but the file one.

As you can guess from the traceback, Python is trying to write to a file object using cp1251 encoding, which does not handle some of the characters in "VÈŃØM" (yet it is successfully written on stderr).

You can try to explicitly configure the encoding while adding your file sink:

logger.add("logs.log", encoding="utf8")

All 5 comments

Hey @shmilylty, thanks for the code sample and Travis report.

I'm not sure if this is a problem related to Loguru itself. It looks like sys.stdout is not configured to handle some of your messages due to unicode. Do print() and sys.stdout.write() work with such characters?

I think you can either:

Thx very much!

Same issue.
As i know in python 3 default charset already set to 'utf-8'. sys.stdout.reconfigure() doesn't help.

My case:
Win 10 | Python 3.7.4

import sys
from loguru import logger

sys.stdout.reconfigure(encoding='utf-8', errors='backslashreplace')

str = "VÈŃØM"
logger.add("logs.log")
logger.info(str)

Error:

2019-09-01 01:16:02.735 | INFO     | __main__:<module>:8 - VÈŃØM
--- Logging error in Loguru Handler #1 ---
Record was: {'elapsed': datetime.timedelta(microseconds=7978), 'exception': None, 'extra': {}, 'file': 'test.py', 'function': '<module>', 'level': 'INFO', 'line': 8, 'message': 'VÈŃØM', 'module': 'test', 'name': '__main__', 'process': '12800', 'thread': '13004', 'time': datetime(2019, 9, 1, 1, 16, 2, 735728, tzinfo=datetime.timezone(datetime.timedelta(seconds=10800), 'RTZ 2 (ceia)'))}
Traceback (most recent call last):
  File "C:\Python37\lib\site-packages\loguru\_handler.py", line 144, in emit
    self._writer(str_record)
  File "C:\Python37\lib\site-packages\loguru\_file_sink.py", line 71, in write
    self._file.write(message)
  File "C:\Python37\lib\encodings\cp1251.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 60-62: character maps to <undefined>
--- End of logging error ---

Am i missed something? Can't understand.

>>> sys.stdout.encoding
'utf-8'

Automatically created file logs.log also encoded with utf-8

@glmn In this case, this is not because of sys.stdout encoding but the file one.

As you can guess from the traceback, Python is trying to write to a file object using cp1251 encoding, which does not handle some of the characters in "VÈŃØM" (yet it is successfully written on stderr).

You can try to explicitly configure the encoding while adding your file sink:

logger.add("logs.log", encoding="utf8")

Thanks!
**kwargs – Others parameters are passed to the built-in open() function. ... :rofl:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lgvaz picture lgvaz  Â·  6Comments

AllanLRH picture AllanLRH  Â·  6Comments

BarryThrill picture BarryThrill  Â·  6Comments

HarveySummers picture HarveySummers  Â·  4Comments

af6140 picture af6140  Â·  5Comments