Skip to content

Replace print() logging in pylib with Python logging#4666

Open
davidsauerwein wants to merge 1 commit intoankitects:mainfrom
davidsauerwein:main
Open

Replace print() logging in pylib with Python logging#4666
davidsauerwein wants to merge 1 commit intoankitects:mainfrom
davidsauerwein:main

Conversation

@davidsauerwein
Copy link
Copy Markdown

@davidsauerwein davidsauerwein commented Apr 3, 2026

Use Python logging facilities instead of printing directly to stdout with print(). This prevents library consumers from being spammed with unwanted debug logs and stack traces.

Closes #4665


My approach to the log levels is warning for any deprecation warning and debug for the "blocked main thread" messages.

The pylib is used in the qt parts of the anki code. To the best of my understanding, logging is already correctly set up there and no adjustments are needed.

The issue I created for this has a reproducer. With this change applied, the messages are gone when configuring the anki logger accordingly.

from anki.collection import Collection
import logging

logging.basicConfig(level=logging.DEBUG)
logging.getLogger("anki").setLevel(logging.INFO)

col = Collection("collection.anki2")
auth = col.sync_login(
    username=r"<username>",
    password=r"<password>",
    endpoint="https://sync.ankiweb.net/",
)
col.close()

setting the anki logger back to DEBUG makes the message appear again. Note that the format is only slightly different then previously.

DEBUG:anki._backend:blocked main thread for 309ms
Stack (most recent call last):
  File "/home/david/coding_stuff/anki/repro.py", line 8, in <module>
    auth = col.sync_login(
  File "/home/david/coding_stuff/anki/.venv/lib/python3.14/site-packages/anki/collection.py", line 1141, in sync_login
    return self._backend.sync_login(
  File "/home/david/coding_stuff/anki/.venv/lib/python3.14/site-packages/anki/_backend_generated.py", line 83, in sync_login
    raw_bytes = self._run_command(1, 3, message.SerializeToString())
  File "/home/david/coding_stuff/anki/.venv/lib/python3.14/site-packages/anki/_backend.py", line 168, in _run_command
    logger.debug(

Use Python logging facilities instead of printing directly to stdout
with print(). This prevents library consumers from being spammed with
unwanted debug logs and stack traces.

Closes ankitects#4665
@read-the-docs-community
Copy link
Copy Markdown

Documentation build overview

📚 Anki | 🛠️ Build #32112915 | 📁 Comparing c802421 against latest (922953a)

  🔍 Preview build  

Show files changed (3 files in total): 📝 3 modified | ➕ 0 added | ➖ 0 deleted
File Status
genindex.html 📝 modified
autoapi/anki/collection/index.html 📝 modified
autoapi/anki/consts/index.html 📝 modified

@abdnh
Copy link
Copy Markdown
Collaborator

abdnh commented Apr 20, 2026

Can we keep print() for deprecation warnings? With the default INFO level set by setup_logging(), add-on authors/users won't see the messages unless the ANKIDEV environment variable is defined (I doubt a lot of add-on developers know about it - it's not documented in the add-ons guide).

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.

pylib spams stack traces on stdout

2 participants