How do you set up basic logging for an addon?

The code below does not create a file anywhere on the system called ankilog.log

import logging
logging.basicConfig(filename='ankilog.log', level=logging.DEBUG)
logging.debug('This is a debug message')
logging.info('This is an info message')
logging.error('This is an error message')

I got it working with this

log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
file_handler = logging.FileHandler(log_path, mode='w')
file_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
log.addHandler(file_handler)
log.debug("Logging initialized")
1 Like

Recent Anki versions have some helper methods for logging: anki/qt/aqt/addons.py at 44e01ea063e6d1b812ace9c001f7ba4a8ccf4479 · ankitects/anki · GitHub

1 Like

Thanks. I’ll check that out.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.