Mayil-AI-Sandbox / loguru-Jan2023

MIT License
0 stars 0 forks source link

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 355: ordinal not in range(128) (hashtag581) #125

Closed vikramsubramanian closed 2 months ago

vikramsubramanian commented 2 months ago

We have a python project with loguru as dependency (version is not pinned) and one of our CI job running on ubuntu bionic (python 3.6) is failing. It seem that loguru was updated to 0.6.0 and raised the following error:

______________________________ test_setup_logger _______________________________
[gw1] linux -- Python 3.6.9 /__w/libretime/libretime/shared/.venv/bin/python3

tmp_path = PosixPath('/tmp/pytest-of-runner/pytest-0/popen-gw1/test_setup_logger0')

    def test_setup_logger(tmp_path: Path):
        log_filepath = tmp_path / "test.log"
        extra_log_filepath = tmp_path / "extra.log"

        setup_logger(INFO, log_filepath)

        extra_logger = create_task_logger(DEBUG, extra_log_filepath, True)

        logger.info("test info")
        extra_logger.info("extra info")
        logger.debug("test debug")

        extra_logger.complete()
        logger.complete()

        assert len(log_filepath.read_text().splitlines()) == 1
>       assert len(extra_log_filepath.read_text().splitlines()) == 1

tests/logging_test.py:52: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/usr/lib/python3.6/pathlib.py:1197: in read_text
    return f.read()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <encodings.ascii.IncrementalDecoder object at 0x7f723b689400>
input = b'{"text": "2022-01-31 07:35:54.009 | INFO     | logging_test:test_setup_logger:45 - extra info\\n", "record": {"elaps...19328, "name": "MainThread"}, "time": {"repr": "2022-01-31 07:35:54.009253+00:00", "timestamp": 1643614554.009253}}}\n'
final = True

    def decode(self, input, final=False):
>       return codecs.ascii_decode(input, self.errors)[0]
E       UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 355: ordinal not in range(128)

/usr/lib/python3.6/encodings/ascii.py:26: UnicodeDecodeError
----------------------------- Captured stderr call -----------------------------
2022-01-31 07:35:54.008 | INFO     | logging_test:test_setup_logger:44 - test info
vikramsubramanian commented 2 months ago

Hi.

Loguru uses "utf8" file encoding by default since 0.6.0 (it used to be locale.getpreferredencoding()). In your case, I guess you can get back the old behavior by specifying "encoding": "ascii" while adding the handler here:

vikramsubramanian commented 2 months ago

Thanks for your answer,

I thought that the encoding always was utf-8 starting with python3, it seems not.

So setting loguru encoding to ascii makes recent version of python fail, and setting encoding to utf-8 makes reading from a file without encoding="utf-8" fail on python36.

I feel this change will force people to set the proper encoding when reading/writing files.