API to log information

Hi, I’m writing an addon (syntax-highlighting-ng) and I wonder if there’s an API to log messages to anki.log.
From looking at the code it seems there’s a logging system in rust (eg. Collection.initialize_backend_logging) but it is not clear to me how is linked to the python logging or how to use: any hint please?

I’d advice against writing to anki.log. Instead, I recommend simply logging to a file in your add-on’s folder (under the user_files subfolder).

Mmm, I was hoping there was a centralized logging mechanism/dispatch.

The logging framework allows that (and dispatching to an different streams, that can be module depend). Something like:

(somewhere in the main aqt._run)

if __name__ == "__main__":
   logging.basicConfig() # or replace with whatever anki config mechanism is in place

So each module can independently log info like
(in my addon modules)

log = logging.getLogger(f"addon.{__name__}")
log.info("a message from the addon")

I could write a PR for it, but I’m not familiar with the rust bridge part (I see it might be used for logging).
Does this make sense?

Anki’s backend logging mechanism is intended for Anki’s own use in the Rust side, but you have to ask @dae about exposing it or adding a centralized logging interface that add-ons can use.

I personally use a utility function to set up a logger using Python’s logging module and save logs to user_files.

1 Like

Hi, I’ve placed a PR (anki/pull/2969) to include python logging in the anki. It’s heavily inspired by ankiutils.log.

Ideally that code should be part of the “framework” not depending on (optional) external modules, eg.
something like this at the top of the addon:

logger = mw.addonManager.getLogger(__name__)

It doesn’t work for me.

Anki 23.12.1 (1a1d4d54)  (ao)
Python 3.9.15 Qt 6.5.3 PyQt 6.5.3
Platform: macOS-13.3.1-arm64-arm-64bit

Traceback (most recent call last):
  File "aqt.taskman", line 142, in _on_closures_pending
  File "aqt.taskman", line 86, in <lambda>
  File "/Users/jarrettye/Library/Application Support/Anki2/addons21/759844606/schedule/disperse_siblings.py", line 155, in on_done
    tooltip(f"{future.result()} in {time.time() - start_time:.2f} seconds")
  File "concurrent.futures._base", line 439, in result
  File "concurrent.futures._base", line 391, in __get_result
  File "concurrent.futures.thread", line 58, in run
  File "/Users/jarrettye/Library/Application Support/Anki2/addons21/759844606/schedule/disperse_siblings.py", line 160, in <lambda>
    lambda: disperse_siblings_backgroud(
  File "/Users/jarrettye/Library/Application Support/Anki2/addons21/759844606/schedule/disperse_siblings.py", line 188, in disperse_siblings_backgroud
    best_due_dates, _, _ = disperse(siblings)
  File "/Users/jarrettye/Library/Application Support/Anki2/addons21/759844606/schedule/disperse_siblings.py", line 137, in disperse
    with open("disperse_logs.txt", "a") as f:
OSError: [Errno 30] Read-only file system: 'disperse_logs.txt'

It works! Thank you!