IndigoLogHandler - logging with python libraries

Posted on
Sat Jan 19, 2019 11:25 am
rbdubz3 offline
User avatar
Posts: 224
Joined: Sep 18, 2016
Location: San Diego, CA

IndigoLogHandler - logging with python libraries

This is likely more of python question than an Indigo one.. I have an set of functions in a library, and would like to properly setup the code so that the log messages output the specific library file.. So essentially log messages in the Indigo log window would be as follows:

- PlugIn Log Sample plugin message
- Plugin Log Another plugin message
- Plugin Library Log A message from the library
- Plugin Library Log Another message from the library

Additionally I was hoping to have a 'toggleDebugging' menu item, which is pretty standard for Indigo Plugins. The toggle would also toggle any library logging to the appropriate debug level

Any help on how to properly lay this out in Python/Indigo would be helpful

I automate because I am lazy :D - My Plugins: https://forums.indigodomo.com/viewforum.php?f=309

Posted on
Sat Jan 19, 2019 11:42 am
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: IndigoLogHandler - logging with python libraries

Read through the Python Specific Logging part of the Debugging and Logging section of the Indigo 7.0 announcement (and look through the linked Python logging tutorial). That should get you started, then if you have specific questions feel free to ask here.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sun Jan 20, 2019 10:02 am
rbdubz3 offline
User avatar
Posts: 224
Joined: Sep 18, 2016
Location: San Diego, CA

Re: IndigoLogHandler - logging with python libraries

Thanks Jay - must be something simple - not sure why my module doesn't spit out any log messages.

BTW - this module is used by the SylvaniaLightify plugin for direct tcp communications to the hub - the module has been pretty stagnant for a while, but looks like some developers are fixing it up for use by the HomeAssistant tools - basically they are adding support for temperature/motion sensors/etc. Anyhow, I wanted to be able to utilize their internal log messages which are sprinkled throughout the code. Ideally I can tap into using the module log messages with very little change to the code

plugin.py:
Code: Select all
    # Startup
    ########################################
    def startup(self):
        indigo.server.log(u"Startup - SylvaniaLightify Plugin, version=" + self.pluginVersion)
        indigo.server.log(u"Initializing Lightify Hub, IP Address=" + self.lightifyHubIpAddr)
        try:
            self.lightifyConn = lightifydirect.Lightify(self.lightifyHubIpAddr)
            self.lightifyConn.update_all_light_status()
            self.lightifyConn.update_group_list()
        except Exception as ex:
            self.errorLog("Error initializing Lightify Hub - " + str(ex))
            self.errorLog("Check IP address in Plugin Configuration.")
        .......


plugin_library.py:
Code: Select all
class IndigoLogHandler(logging.Handler, object):
    def __init__(self, displayName, level=logging.NOTSET):
        super(IndigoLogHandler, self).__init__(level)
        self.displayName = displayName

    def emit(self, record):
        # First, determine if it needs to be an Indigo error
        is_error = True if record.levelno in (logging.ERROR, logging.CRITICAL) else False
        type_string = self.displayName
        # For any level besides INFO and ERROR (which Indigo handles), we append
        # the debug level (i.e. Critical, Warning, Debug, etc) to the type string
        if record.levelno not in (logging.INFO, logging.ERROR):
            type_string += u" %s" % record.levelname.title()
        # Then we write the message
        indigo.server.log(message=self.format(record), type=type_string, isError=is_error)

class Lightify:
    def __init__(self, host):
        self.__logger = logging.getLogger(MODULE)
        self.__logger.setLevel(logging.NOTSET)
        self.__logger.addHandler(IndigoLogHandler(MODULE))
        self.__logger.info("Initializing python %s, version=%s", MODULE, __version__)
       .......

I automate because I am lazy :D - My Plugins: https://forums.indigodomo.com/viewforum.php?f=309

Posted on
Sun Jan 20, 2019 10:31 am
rbdubz3 offline
User avatar
Posts: 224
Joined: Sep 18, 2016
Location: San Diego, CA

Re: IndigoLogHandler - logging with python libraries

Hmm.. well I think I have it working. I basically had to expose a method in the library module to toggle the debugging programmatically. I guess I was under the impression that the level would get inherited from the plugin if left to logging.NOTSET

So I have a new method in the library:
Code: Select all
    def set_debug(self, debugOn):
        if debugOn:
            self.__logger.setLevel(logging.DEBUG)
        else:
            self.__logger.setLevel(logging.INFO)
        self.__logger.info("set_debug to '%s'", debugOn)


which is called from the plugin toggleDebugging method:
Code: Select all
    def toggleDebugging(self):
        if self.debug:
            indigo.server.log("Turning off debug logging")
            self.pluginPrefs["showDebugInfo"] = False
        else:
            indigo.server.log("Turning on debug logging")
            self.pluginPrefs["showDebugInfo"] = True
        self.debug = not self.debug
        self.lightifyConn.set_debug(self.debug)


If there are some tips on making this a bit more elegant using the built-in stuff from the python Logging module - please pass along. Thanks!

I automate because I am lazy :D - My Plugins: https://forums.indigodomo.com/viewforum.php?f=309

Posted on
Sun Jan 20, 2019 11:14 am
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: IndigoLogHandler - logging with python libraries

Log levels are rarely (I'm hesitant to say never) inherited through the various loggers and handlers. Each maintains it's own - this gives it ultimate flexibility but also tends to make the whole logging module more complex to understand.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 4 guests