Skip to content

Commit

Permalink
fix(logging): Make sure our file handler uses unicode and continues t…
Browse files Browse the repository at this point in the history
…hrough errors
  • Loading branch information
ianohara committed Feb 25, 2025
1 parent a45ec55 commit e33aae7
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions software/squid/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import platformdirs

_squid_root_logger_name = "squid"
_baseline_log_format = "%(asctime)s.%(msecs)03d - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)"
_baseline_log_dateformat = "%Y-%m-%d %H:%M:%S"
_baseline_log_format = u"%(asctime)s.%(msecs)03d - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)"
_baseline_log_dateformat = u"%Y-%m-%d %H:%M:%S"


# The idea for this CustomFormatter is cribbed from https://stackoverflow.com/a/56944256
Expand Down Expand Up @@ -177,7 +177,9 @@ def add_file_logging(log_filename, replace_existing=False):
os.makedirs(os.path.dirname(abs_path), exist_ok=True)

# For now, don't worry about rollover after a certain size or time. Just get a new file per call.
new_handler = logging.handlers.RotatingFileHandler(abs_path, maxBytes=0, backupCount=25)
# NOTE(imo): We had issues with windows not defaulting to utf-8, so force that here. But also let the handler
# know that if it sees encoding errors, it should replace the error bytes with ? and continue.
new_handler = logging.handlers.RotatingFileHandler(abs_path, maxBytes=0, backupCount=25, encoding="utf-8", errors="replace")
new_handler.setLevel(py_logging.DEBUG)

formatter = py_logging.Formatter(fmt=_baseline_log_format, datefmt=_baseline_log_dateformat)
Expand Down

0 comments on commit e33aae7

Please sign in to comment.