Skip to content

Write the log file as UTF-8 - #2448

Merged
rolandwalker merged 1 commit into
dbcli:mainfrom
pcbeingused333:fix/log-file-utf8
Nov 19, 2026
Merged

rolandwalker merged 1 commit into
dbcli:mainfrom
pcbeingused333:fix/log-file-utf8

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

Description

With the default log_file = ~/.mycli.log and log_level = INFO, the REPL logs failed queries as sql: %r, error: %r. initialize_logging() builds that handler as logging.FileHandler(log_file) without an encoding, so the file is opened with the locale encoding. On Python 3.11–3.14 with a non-UTF-8 locale (for example Windows cp1252), a failed query containing characters outside that codepage makes the handler raise UnicodeEncodeError: Python prints a --- Logging error --- traceback to the terminal and the log entry is lost. The audit log, the -l logfile option and the config files already open their files as UTF-8.

Fix: pass encoding='utf-8' to the FileHandler, plus a changelog.md entry.

Test: test_initialize_logging_file_handler_uses_utf8 asserts the handler's encoding is UTF-8. It fails on main under Python 3.12 (the encoding is None) and passes with the fix. On 3.15 the default is already UTF-8, so the test is only red on earlier versions.

This PR was prepared with the assistance of an AI coding tool (Claude Code).

Checklist

  • I added this contribution to the changelog.md file.
  • I added my name to the mycli/AUTHORS.txt file (or it's already there).
  • To lint and format the code, I ran
    uv run ruff check && uv run ruff format && uv run mypy --install-types .

logging.FileHandler(log_file) used the locale encoding, so
non-ASCII queries or data raised UnicodeEncodeError in the log
handler on non-UTF-8 locales (for example Windows cp1252).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@rolandwalker

Copy link
Copy Markdown
Contributor

Thank you!

@rolandwalker
rolandwalker merged commit 357fed4 into dbcli:main Nov 19, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants