diff --git a/CHANGELOG.rst b/CHANGELOG.rst index c3bbe956..d3aa7837 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -3,7 +3,8 @@ - Modify coroutine sink to make it discard log messages when ``loop=None`` and no event loop is running (due to internally using ``asyncio.get_running_loop()`` in place of ``asyncio.get_event_loop()``). - Remove the possibility to add a coroutine sink with ``enqueue=True`` if ``loop=None`` and no event loop is running. -- Prevent non-ascii characters to be escaped while logging JSON message with ``serialize=True`` (`#574 `_, thanks `@ponponon `_). +- Change default encoding of file sink to be ``utf8`` instead of ``locale.getpreferredencoding()`` (`#339 `_). +- Prevent non-ascii characters to be escaped while logging JSON message with ``serialize=True`` (`#575 `_, thanks `@ponponon `_). - Fix ``flake8`` errors and improve code readability (`#353 `_, thanks `@AndrewYakimets `_). diff --git a/loguru/_file_sink.py b/loguru/_file_sink.py index 691e3a74..5f48f512 100644 --- a/loguru/_file_sink.py +++ b/loguru/_file_sink.py @@ -1,7 +1,6 @@ import datetime as datetime_ import decimal import glob -import locale import numbers import os import shutil @@ -147,10 +146,10 @@ def __init__( delay=False, mode="a", buffering=1, - encoding=None, + encoding="utf8", **kwargs ): - self.encoding = locale.getpreferredencoding(False) if encoding is None else encoding + self.encoding = encoding self._kwargs = {**kwargs, "mode": mode, "buffering": buffering, "encoding": self.encoding} self._path = str(path) diff --git a/loguru/_logger.py b/loguru/_logger.py index 88474614..e2c66fdf 100644 --- a/loguru/_logger.py +++ b/loguru/_logger.py @@ -38,7 +38,6 @@ .. |contextvars| replace:: :mod:`contextvars` .. |Thread.run| replace:: :meth:`Thread.run()` .. |Exception| replace:: :class:`Exception` -.. |locale.getpreferredencoding| replace:: :func:`locale.getpreferredencoding()` .. |AbstractEventLoop| replace:: :class:`AbstractEventLoop` .. |asyncio.get_running_loop| replace:: :func:`asyncio.get_running_loop()` .. |asyncio.run| replace:: :func:`asyncio.run()` @@ -304,8 +303,7 @@ def add( The buffering policy as for built-in |open| function. It defaults to ``1`` (line buffered file). encoding : |str|, optional - The file encoding as for built-in |open| function. If ``None``, it defaults to - |locale.getpreferredencoding|. + The file encoding as for built-in |open| function. It defaults to ``"utf8"``. **kwargs Others parameters are passed to the built-in |open| function. diff --git a/tests/test_add_sinks.py b/tests/test_add_sinks.py index d1b4fab3..d53fd2cd 100644 --- a/tests/test_add_sinks.py +++ b/tests/test_add_sinks.py @@ -159,6 +159,14 @@ def test_file_sink_utf8_encoding(tmpdir): assert file.read_text("utf8") == "天\n" +def test_file_sink_default_encoding(tmpdir): + file = tmpdir.join("test.log") + logger.add(str(file), format="{message}", errors="strict", catch=False) + logger.info("天") + logger.remove() + assert file.read_text("utf8") == "天\n" + + def test_disabled_logger_in_sink(sink_with_logger): sink = sink_with_logger(logger) logger.disable("tests.conftest")