# Mopidy debug logging under systemd

**URL:** <https://discourse.mopidy.com/t/mopidy-debug-logging-under-systemd/4277>\
**Category:** Misc\
**Created:** [September 4, 2020, 12:24pm UTC](https://discourse.mopidy.com/t/mopidy-debug-logging-under-systemd/4277 "2020-09-04T12:24:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ron4mac](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.mopidy.com/ron4mac/32/1721_2.png) [@ron4mac](https://discourse.mopidy.com/u/ron4mac)\
**Post date:** [September 4, 2020, 12:24pm UTC](https://discourse.mopidy.com/t/mopidy-debug-logging-under-systemd/4277/1 "2020-09-04T12:24:38Z")

</div>

I’m having difficulty understanding logging in Mopidy. I’m working on a backend and would like some debug logging (while running via systemd).

logging.conf (pointed to by mopidy.conf)

> ```
> [loggers]
> keys = root
> 
> [handlers]
> keys = fileHandler
> 
> [formatters]
> keys = simpleFormatter
> 
> [logger_root]
> handlers = fileHandler
> 
> [handler_fileHandler]
> class = FileHandler
> formatter = simpleFormatter
> level = INFO
> args = ('/var/log/mopidy/mopidy.log',)
> 
> [formatter_simpleFormatter]
> format = %(asctime)s %(levelname)s [%(process)d:%(threadName)s] %(name)s: %(message)s
> datefmt =
> 
> ```

And in my playback.py

> ```
> import logging
> 
> from mopidy import backend
> 
> logger = logging.getLogger( __name__ )
> logger.setLevel(logging.DEBUG)
> 
> class CalmRadioPlayback(backend.PlaybackProvider):
> def translate_uri(self, uri):
> logger.info('Translate URI: %s' % uri)
> logger.debug('Translate URI (debug): %s' % uri)
> 
> ```

I would expect that, explicitly setting the logger level, I would get both info and debug logging … but I get only info. What am I missing? (new to mopidy and python … be kind 😕)

---

<div class="post-metadata">

**Author:** ![ron4mac](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.mopidy.com/ron4mac/32/1721_2.png) [@ron4mac](https://discourse.mopidy.com/u/ron4mac)\
**Post date:** [September 18, 2020, 8:31pm UTC](https://discourse.mopidy.com/t/mopidy-debug-logging-under-systemd/4277/2 "2020-09-18T20:31:17Z")

</div>

Bumping this … hoping someone can point me in the right direction.

---

<div class="post-metadata">

**Author:** ![kingosticks](https://avatars.discourse-cdn.com/v4/letter/k/858c86/32.png) [@kingosticks](https://discourse.mopidy.com/u/kingosticks)\
**Post date:** [September 18, 2020, 11:00pm UTC](https://discourse.mopidy.com/t/mopidy-debug-logging-under-systemd/4277/3 "2020-09-18T23:00:45Z")

</div>

Your code to set the level to DEBUG will execute when the module is imported. But then later, when Mopidy actually starts running, it’ll set the logging level to INFO as defined in your config.

I’d personally scrap all that logging.conf and use the `loglevels/*` section within your mopidy.conf. So if I wanted to have DEBUG logging for just Mopidy-Spotify’s playlists module and TRACE debugging for the web module: I’d do:

```auto
[loglevels]
mopidy_spotify.playlists = DEBUG
mopidy_spotify.web = TRACE

```

But when I am developing I’d always be using a [develop environment](https://docs.mopidy.com/en/latest/devenv/) and regular stdout logging, not systemd. Not that it makes much different here when it comes to logging, it’s just generally easier to work with.

---

<div class="post-metadata">

**Author:** ![ron4mac](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.mopidy.com/ron4mac/32/1721_2.png) [@ron4mac](https://discourse.mopidy.com/u/ron4mac)\
**Post date:** [September 19, 2020, 12:28am UTC](https://discourse.mopidy.com/t/mopidy-debug-logging-under-systemd/4277/4 "2020-09-19T00:28:26Z")

</div>

Thank you for your response and advice. I tried using [loglevels] with no real success … but I’ll revisit that. One of the backends I’m working on has an intermittent failure … could be only once or twice a day. Thought the mopidy log file would be the best way to go.

---

<div class="post-metadata">

**Author:** ![kingosticks](https://avatars.discourse-cdn.com/v4/letter/k/858c86/32.png) [@kingosticks](https://discourse.mopidy.com/u/kingosticks)\
**Post date:** [September 19, 2020, 6:27am UTC](https://discourse.mopidy.com/t/mopidy-debug-logging-under-systemd/4277/5 "2020-09-19T06:27:39Z")

</div>

Maybe try running under pdb with breakpoints to catch the failure mode?

---

<div class="post-metadata">

**Author:** ![ron4mac](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.mopidy.com/ron4mac/32/1721_2.png) [@ron4mac](https://discourse.mopidy.com/u/ron4mac)\
**Post date:** [September 19, 2020, 3:19pm UTC](https://discourse.mopidy.com/t/mopidy-debug-logging-under-systemd/4277/6 "2020-09-19T15:19:33Z")

</div>

Thanks for all your help. For whatever reason, [loglevels] does not work for me.  
I was able to achieve what I wanted in a particular file with:

```
import logging

from mopidy import backend

logger = logging.getLogger( __name__ )

handler = logging.FileHandler('var/log/mopidy/calmradio.log')
formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)

class CalmRadioPlayback(backend.PlaybackProvider):
        def translate_uri(self, uri):
               logger.debug('Translate URI: %s' % uri)
```

---

<div class="post-metadata">

**Author:** ![kingosticks](https://avatars.discourse-cdn.com/v4/letter/k/858c86/32.png) [@kingosticks](https://discourse.mopidy.com/u/kingosticks)\
**Post date:** [September 19, 2020, 5:19pm UTC](https://discourse.mopidy.com/t/mopidy-debug-logging-under-systemd/4277/7 "2020-09-19T17:19:32Z")

</div>

If you want to share the config you tried and the name of your module we can try and work out why that is. Otherwise, I wish you luck with your debugging.
