Logging from Submodules

Posted on
Mon May 11, 2020 6:52 am
hannt offline
User avatar
Posts: 54
Joined: Jul 08, 2011

Logging from Submodules

Working on a plugin that also defines a module that will be imported. The intent is to use this module in other plugins as well. The module defines a class with multiple functions. The functions will be called by Action callbacks in the plugin. The problem I'm having is that I can't seem to get any logging to work from within this class. I'm using the standard Python logging module (as defined in API 2.0) for the plugin and all works there. I even tried standard Indigo logging and that didn't work either.

I suspect the problem lies in the way I have defined and initialized the class in the module. This is the first time I've tried to create a custom module that includes a class. I guess I could implement the module without a class, but there are some properties I would like to set that apply to all functions. It seemed like the best way to do that was with a class.

There's a section in the Indigo Plugin Developer's Guide v2.0 titled "Logging from submodules", but it hasn't been written yet. I bet the information that will eventually make its way to that section is what I'm looking for. Is there any documentation somewhere else on this topic that would help?

Thanks!

Posted on
Mon May 11, 2020 9:30 am
jay (support) offline
Site Admin
User avatar
Posts: 16212
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Logging from Submodules

There are a variety of strategies for logging from independent modules - and lots of discussion out on the net as to the best approach (which is why I haven't really written that section yet, still trying to figure it out myself).

Plugins define a few things automatically: the logger for the plugin (self.logger), a log handler that will output log messages to the Indigo event log (self.indigo_log_handler), and a handler that will log into the plugin specific log file (self.plugin_file_handler).

There are a couple of patterns that we've used: one is to pass the plugin logger into the module and log using that. I'm not convinced this is the best way, but it will definitely work. Another is one I'm going to be experimenting with in something I'm working on now: create the logger in the module (logging.getLogger("module_name")), but make sure that it's easily exposed to callers of the module. You can then add either or both of the handlers that the plugin creates to the module's logger, which *should* output into those logs (event log and/or plugin specific log file). Remember also to set the log level on the handlers and the module's level to make sure that what you want logged is in fact getting through. Note that I haven't gotten to the point of working specifically on this logging scenario, so there may be unexpected results that I've yet to see.

There are likely a many logging scenarios using a variety of combinations of the above and probably some other things as well. Hopefully that will be enough to get you started.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon May 11, 2020 1:25 pm
hannt offline
User avatar
Posts: 54
Joined: Jul 08, 2011

Re: Logging from Submodules

Thanks for the response, Jay - very helpful. I appreciate how Indigo does a lot of work up front to setup the logger for the plugin. I've played a little with creating the logger in the module, but it seems to be a lot of work and feels somewhat disconnected from Indigo. I'm going to try to pass the plugin logger into the module and see how that works.

Posted on
Mon May 11, 2020 2:30 pm
jay (support) offline
Site Admin
User avatar
Posts: 16212
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Logging from Submodules

You wouldn't really do it from the module side other than create (get) the logger by name. Then in the scripts using the module you get the logger (by the same name) and add handlers, formatters, change levels, etc.

So, in the module, you do this:

Code: Select all
# Anywhere in the module
import logging
logger = logging.getLogger("my_module")


Then, in the code in your plugin:

Code: Select all
import logging
module_logger = logging.getLogger("my_module")
module_logger.addHandler(self.indigo_log_handler)

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon May 11, 2020 7:07 pm
hannt offline
User avatar
Posts: 54
Joined: Jul 08, 2011

Re: Logging from Submodules

Thanks again for the helpful information.

I played around with passing the plugin logger into the module and log using that... And it just works. Very easy. It even changed logging levels in the module when I changed it in the plugin.

I'm no expert with Python logging or with Python modules & classes, far from it, so I don't know why this wouldn't be a good strategy.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 0 guests