Skip to content

Commit 26e1784

Browse files
authored
Merge pull request #3641 from Sup3rGeo/bugfix/logfile-unicode
Fixes #3630
2 parents a48c47b + dad3e77 commit 26e1784

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

changelog/3630.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Log messages with unicode characters would not appear in the output log file.

src/_pytest/logging.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,9 @@ def __init__(self, config):
392392
config, "log_file_date_format", "log_date_format"
393393
)
394394
# Each pytest runtests session will write to a clean logfile
395-
self.log_file_handler = logging.FileHandler(log_file, mode="w")
395+
self.log_file_handler = logging.FileHandler(
396+
log_file, mode="w", encoding="UTF-8"
397+
)
396398
log_file_formatter = logging.Formatter(
397399
log_file_format, datefmt=log_file_date_format
398400
)

testing/logging/test_reporting.py

+38
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22
import re
33
import os
4+
from io import open
45

56
import six
67

@@ -827,6 +828,43 @@ def test_log_file(request):
827828
assert "This log message won't be shown" not in contents
828829

829830

831+
def test_log_file_unicode(testdir):
832+
log_file = testdir.tmpdir.join("pytest.log").strpath
833+
834+
testdir.makeini(
835+
"""
836+
[pytest]
837+
log_file={}
838+
log_file_level = INFO
839+
""".format(
840+
log_file
841+
)
842+
)
843+
testdir.makepyfile(
844+
"""
845+
# -*- coding: utf-8 -*-
846+
from __future__ import unicode_literals
847+
import logging
848+
849+
def test_log_file():
850+
logging.getLogger('catchlog').info("Normal message")
851+
logging.getLogger('catchlog').info("├")
852+
logging.getLogger('catchlog').info("Another normal message")
853+
"""
854+
)
855+
856+
result = testdir.runpytest()
857+
858+
# make sure that that we get a '0' exit code for the testsuite
859+
assert result.ret == 0
860+
assert os.path.isfile(log_file)
861+
with open(log_file, encoding="utf-8") as rfh:
862+
contents = rfh.read()
863+
assert "Normal message" in contents
864+
assert u"├" in contents
865+
assert "Another normal message" in contents
866+
867+
830868
@pytest.mark.parametrize("has_capture_manager", [True, False])
831869
def test_live_logging_suspends_capture(has_capture_manager, request):
832870
"""Test that capture manager is suspended when we emitting messages for live logging.

0 commit comments

Comments
 (0)