module-level logging - integrate with indigo main logger

Posted on
Sat Mar 25, 2017 10:28 am
rbdubz3 offline
User avatar
Posts: 224
Joined: Sep 18, 2016
Location: San Diego, CA

module-level logging - integrate with indigo main logger

I have written a couple of python modules to perform helper functions for my home automation. Additionally I have integrated an open source module into a plugin. I'd like to have each of these modules capable of writing to the main indigo log file so that I can easily see which module created the log entry.. This is similar to what has been done for Plugins in Indigo 7.0. However, some these python modules I am accessing directly from Trigger and Schedule actions and not necessarily from within a plugin.

thanks in advance!

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

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

Re: module-level logging - integrate with indigo main logger

The main Indigo log file isn't implemented in Python so other than using the IOM function (indigo.server.log) there's no other way to write to it.

Having said that, you could likely add a handler to your python logging that writes to the event log window which would in turn write to the main Indigo log file. Here's the class we use to do that in plugins:

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)


You can then add this handler to the python logger you're using and then any logging messages from the logger will also pass through this handler and depending on the level will get logged to the event log window (and therefore into the main Indigo log file).

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sun Mar 26, 2017 11:39 am
rbdubz3 offline
User avatar
Posts: 224
Joined: Sep 18, 2016
Location: San Diego, CA

Re: module-level logging - integrate with indigo main logger

Thanks for the tip Jay.. I added the IndigoLogHandler class into the Osram Lightify python code, which is currently a set of classes that are instantiated by my Indigo Plugin.. Seems to kinda work.. Only thing is that I am only able to see WARNING, ERROR, CRITICAL messages in the indigo log.

As a python newbie, I am sure there is some type of logger inheritance stuff that I am missing.. I did attempt to set the logging level when instantiating my new handler, but it still didn't work. See the class initialization below :

Code: Select all
class Lightify:
    def __init__(self, host):
        self.__logger = logging.getLogger(MODULE)
        #self.__logger.addHandler(logging.NullHandler())
        self.__logger.addHandler(IndigoLogHandler("Lightify Direct", level=logging.DEBUG))
        self.__logger.info("Logging %s", MODULE)


Any help appreciated.

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

Posted on
Sun Mar 26, 2017 6:50 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: module-level logging - integrate with indigo main logger

At what level is self.__logger logging? It has a separate log level.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Mar 27, 2017 8:17 am
rbdubz3 offline
User avatar
Posts: 224
Joined: Sep 18, 2016
Location: San Diego, CA

Re: module-level logging - integrate with indigo main logger

Thanks Jay.. that was the issue.. Apologies for the python rookie question..

I just added code to set the level of the module logger.. i.e. self.__logger.setLevel(logging.INFO) .. All good now..

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

Posted on
Mon Mar 27, 2017 11:05 am
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: module-level logging - integrate with indigo main logger

It's not a rookie issue, it's a rather complicated Python logging issue that took me many days to understand (and I suspect I'm still not really an expert). Glad it's working for you.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 17 guests