Skip to content

Commit

Permalink
Change default file sink encoding to be "utf8" (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
Delgan committed Jan 24, 2022
1 parent b02ef7a commit 1eeea19
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Expand Up @@ -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 <https://github.com/Delgan/loguru/pull/575>`_, thanks `@ponponon <https://github.com/ponponon>`_).
- Change default encoding of file sink to be ``utf8`` instead of ``locale.getpreferredencoding()`` (`#339 <https://github.com/Delgan/loguru/issues/339>`_).
- Prevent non-ascii characters to be escaped while logging JSON message with ``serialize=True`` (`#575 <https://github.com/Delgan/loguru/pull/575>`_, thanks `@ponponon <https://github.com/ponponon>`_).
- Fix ``flake8`` errors and improve code readability (`#353 <https://github.com/Delgan/loguru/issues/353>`_, thanks `@AndrewYakimets <https://github.com/AndrewYakimets>`_).


Expand Down
5 changes: 2 additions & 3 deletions loguru/_file_sink.py
@@ -1,7 +1,6 @@
import datetime as datetime_
import decimal
import glob
import locale
import numbers
import os
import shutil
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions loguru/_logger.py
Expand Up @@ -38,7 +38,6 @@
.. |contextvars| replace:: :mod:`contextvars`
.. |Thread.run| replace:: :meth:`Thread.run()<threading.Thread.run()>`
.. |Exception| replace:: :class:`Exception`
.. |locale.getpreferredencoding| replace:: :func:`locale.getpreferredencoding()`
.. |AbstractEventLoop| replace:: :class:`AbstractEventLoop<asyncio.AbstractEventLoop>`
.. |asyncio.get_running_loop| replace:: :func:`asyncio.get_running_loop()`
.. |asyncio.run| replace:: :func:`asyncio.run()`
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions tests/test_add_sinks.py
Expand Up @@ -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")
Expand Down

0 comments on commit 1eeea19

Please sign in to comment.