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

Ensure all file handlers use utf-8 encoding. #9401

Merged
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
3 changes: 1 addition & 2 deletions kolibri/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
# TODO: Move version tools to build tools, so we don't have to do this
from colorlog import ColoredFormatter
from colorlog import getLogger
from colorlog import StreamHandler
except ImportError:
StreamHandler = None
getLogger = None
ColoredFormatter = None

from .logger import LOG_COLORS
from .logger import EncodingStreamHandler as StreamHandler
from kolibri.utils.compat import monkey_patch_collections


Expand Down
39 changes: 37 additions & 2 deletions kolibri/utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,39 @@
}


class EncodingStreamHandler(logging.StreamHandler):
"""
A custom stream handler that encodes the log message to the specified encoding.
"""

terminator = "\n"

def __init__(self, stream=None, encoding="utf-8"):
super(EncodingStreamHandler, self).__init__(stream)
self.encoding = encoding

def emit(self, record):
"""
Vendored and modified from:
https://github.com/python/cpython/blob/main/Lib/logging/__init__.py#L1098
"""
try:
msg = self.format(record)
stream = self.stream
# issue 35046: merged two stream.writes into one.
text = msg + self.terminator
if self.encoding and hasattr(stream, "buffer"):
bytes_to_write = text.encode(self.encoding)
stream.buffer.write(bytes_to_write)
else:
stream.write(text)
self.flush()
except RuntimeError: # See issue 36272
raise
except Exception:
self.handleError(record)


class KolibriTimedRotatingFileHandler(TimedRotatingFileHandler):
"""
A custom TimedRotatingFileHandler that overrides two methods, getFilesToDelete
Expand Down Expand Up @@ -172,14 +205,14 @@ def get_default_logging_config(LOG_ROOT, debug=False, debug_database=False):
"handlers": {
"console-error": {
"level": "ERROR",
"class": "logging.StreamHandler",
"class": "kolibri.utils.logger.EncodingStreamHandler",
"formatter": "color",
"stream": "ext://sys.stderr",
},
"console": {
"level": DEFAULT_LEVEL,
"filters": ["no_exceptions"],
"class": "logging.StreamHandler",
"class": "kolibri.utils.logger.EncodingStreamHandler",
"formatter": "color",
"stream": "ext://sys.stdout",
},
Expand All @@ -191,13 +224,15 @@ def get_default_logging_config(LOG_ROOT, debug=False, debug_database=False):
"formatter": "simple_date",
"when": "midnight",
"backupCount": 30,
"encoding": "utf-8",
},
"file_debug": {
"level": "DEBUG",
"filters": ["require_debug_true"],
"class": "logging.FileHandler",
"filename": os.path.join(LOG_ROOT, "debug.txt"),
"formatter": "simple_date",
"encoding": "utf-8",
},
},
"loggers": {
Expand Down