Debug logging for libraries within custom components

Quick question about debug logging for a custom integration. I am creating a new light integration, with a light.py, and I have written a library that light.py uses (lampster.py), which light.py draws upon to make an object and then makes calls into the object. Nothing exciting there…thats how integrations work.

But I can only get debug logging to actually write to the HA logs when I do logger calls from light.py.

import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level="DEBUG")

<within a def function:>
logger.debug("my log message")

…and in System/Logs I see the log my log message

When I do the same logger calls in my lampster.py library in the same way absolutely nothing seems to happen. It runs and the lines seem to execute, but no logs appear in the HA logs (System/Logs)

Any ideas? It makes it really hard to work out what’s going on when the library is failing silently (incidentally if I raise an Exception in the library that doesn’t seem to feature in the logs either)

–Chris

You shouldnt set the log level in your integration. If you want to turn on debug logging for a custom component you should do this in configuration.yaml.

Ie.

logger:
  default: warning
  logs:
    custom_components.[your_component_domain]: debug

EDIT: Oh and if lamster.py is part of a library loaded via pip (or in your manifest.json) you would need to add a line for that library in the above. If just part of your custom component files, above will work (providing you have the same definition for logger in that file too.

Ok … the library is just a file in the same folder I import that I wrote (it’s a library because I use it outside HA too so it saves me writing the complicated BLE command set out twice) and not installed with pip.

My configuration.yaml names the component already in debug logging. (I copied another integration when I started and the logging code earlier was already there so I assumed i needed it!)

As the component is set to debug logging should my library I import be captured by the same config? Otherwise do I put the name of the python library in configuration.yaml?

Yes it should. Do you want to post a link to your whole code and i’ll have a look. Also provide the logging section of your config.yaml

The code is long and messy. It doesn’t need to be seen in public. I’ve got fragments of working code and lots of mess as I try to work out why I am having so much of a problem in HA with BLE*

It’s just a typical light.py in structure and the lampster library looks something like the one in my GitHub repo

lampsterble.py

The config yaml for the logging is


logger:
  default: info
  logs:
    hacs: debug
    custom_components.hacs: debug
    # custom_components.panasonic_ac: debug
    queueman: debug
    aiogithubapi: debug 
    httpx: debug
    # homeassistant.components.rest: debug
    custom_components.lampster: debug

I could just rewrite the whole thing in light.py so logging works but I would rather keep my library separate so I can keep it in sync

So it’s an annoyance really but it must be fixable

Chris

*(I have been talking to bdraco as the HA wrapper for Bleak seems to cause me issues when Bleak works fine outside HA. One issues has been fixed but the wrapper combined with a really badly engjneered light I’m controlling is making this hard)