| 1 |
.. _plugin-logging: |
| 2 |
|
| 3 |
Logging |
| 4 |
======= |
| 5 |
|
| 6 |
Each plugin object has a ``_log`` attribute, which behaves like a logger from |
| 7 |
the `standard Python logging module`_ but is provided by beets' ``BeetsLogger`` |
| 8 |
class. The logger is set up to `PEP 3101`_, str.format-style string formatting. |
| 9 |
So you can write logging calls like this: |
| 10 |
|
| 11 |
.. code-block:: python |
| 12 |
|
| 13 |
self._log.debug("Processing {0.title} by {0.artist}", item) |
| 14 |
|
| 15 |
For especially noisy diagnostics, plugins can use |
| 16 |
``self._log.extra_debug(...)``. These messages are logged at ``DEBUG`` level |
| 17 |
only when beets is run with verbosity level 3 or higher, such as ``-vvv``. |
| 18 |
|
| 19 |
.. _pep 3101: https://peps.python.org/pep-3101/ |
| 20 |
|
| 21 |
.. _standard python logging module: https://docs.python.org/3/library/logging.html |
| 22 |
|
| 23 |
When beets is in verbose mode, plugin messages are prefixed with the plugin name |
| 24 |
to make them easier to see. |
| 25 |
|
| 26 |
Which messages will be logged depends on the logging level and the action |
| 27 |
performed: |
| 28 |
|
| 29 |
- Inside import stages and event handlers, the default is ``WARNING`` messages |
| 30 |
and above. |
| 31 |
- Everywhere else, the default is ``INFO`` or above. |
| 32 |
|
| 33 |
The verbosity can be increased with ``--verbose`` (``-v``) flags: each flags |
| 34 |
lowers the level by a notch. That means that, with a single ``-v`` flag, event |
| 35 |
handlers won't have their ``DEBUG`` messages displayed, but command functions |
| 36 |
(for example) will. With ``-vv`` on the command line, ``DEBUG`` messages will be |
| 37 |
displayed everywhere. |
| 38 |
|
| 39 |
This addresses a common pattern where plugins need to use the same code for a |
| 40 |
command and an import stage, but the command needs to print more messages than |
| 41 |
the import stage. (For example, you'll want to log "found lyrics for this song" |
| 42 |
when you're run explicitly as a command, but you don't want to noisily interrupt |
| 43 |
the importer interface when running automatically.) |