Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logger: Add encoding #6910

Merged
merged 8 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions kivy/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class FileHandler(logging.Handler):
filename = 'log.txt'
fd = None
log_dir = ''
encoding = 'utf-8'

def purge_logs(self):
'''Purge log is called randomly to prevent the log directory from being
Expand Down Expand Up @@ -219,8 +220,7 @@ def _configure(self, *largs, **kwargs):
FileHandler.filename = filename
if FileHandler.fd is not None:
FileHandler.fd.close()
FileHandler.fd = open(filename, 'w')

FileHandler.fd = open(filename, 'w', encoding=FileHandler.encoding)
Logger.info('Logger: Record log in %s' % filename)

def _write_message(self, record):
Expand Down
28 changes: 28 additions & 0 deletions kivy/tests/test_issues/test_issue_6909.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import locale
import unittest
from unittest import mock
from kivy import Config
from kivy.logger import Logger, FileHandler
import pytest


class CodecLoggingTestCase(unittest.TestCase):
def test_log_handles_cp949(self):
with mock.patch("locale.getpreferredencoding", return_value="cp949"):
FileHandler.fd = None
FileHandler.encoding = "utf-8"
Config.set("kivy", "log_enable", 1)
Config.set("kivy", "log_level", "trace")
for string in ["한국어", "Niñas and niños"]:
Logger.trace("Lang: call_fn => value=%r" % (string,))

def test_non_utf8_encoding_raises_exception(
self,
): # the old error before utf-8 was standard
FileHandler.fd = None
FileHandler.encoding = "cp949"
Config.set("kivy", "log_enable", 1)
Config.set("kivy", "log_level", "trace")

with pytest.raises(UnicodeError):
Logger.trace("Lang: call_fn => value=%r" % ("Niñas and niños",))