Page 1 of 1

Python logging used by Indigo

PostPosted: Fri Apr 22, 2016 9:55 am
by jay (support)
[NOTE] This note is for plugin developers, not plugin users. The change should be made by the developer/maintainer of the plugin.

As of Indigo 6.1.8, we're now using the standard Python logging facility. We made this change in preparation for a complete overhaul of plugin logging in Indigo 7. This change has introduced some logging issues. Several plugins began logging odd things into the Indigo log. The reason is that libraries used by those plugins are set by default to log at the INFO level, and they contain logging at that level. We log everything at the INFO level to the Indigo log, so now there is more stuff showing up.

The primary library we're seeing is the requests library, and more specifically the urllib3 library that's installed with it. That library liberally uses INFO level logging throughout. We believe that pattern, for a library, is a poor design, but it is what it is. Fortunately, there's a simple way to fix it. Once you import the library, you can adjust the level thusly:

Code: Select all
logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)


You can use it for either - the current crop of unneeded logging is specifically in the urllib3 library, so you can just adjust that. You can use any of the standard logging levels of course. Anything below that level will not be logged. The default for both is logging.INFO, but setting it to a higher level will solve the problem.

See this stackoverflow post for a more complete discussion.

Re: Python logging used by Indigo

PostPosted: Sat Apr 23, 2016 9:31 am
by bkmar1192
It is not clear what am I suppose to do with this information. Do I need to change something - I tried running that code as trigger but it didn't seem to change anything. Is this a change that needs to happen in the plugin itself or can I do it globally? My log is filling up with messages.

Re: Python logging used by Indigo

PostPosted: Sat Apr 23, 2016 9:42 am
by autolog
bkmar1192 wrote:
It is not clear what am I suppose to do with this information. Do I need to change something - I tried running that code as trigger but it didn't seem to change anything. Is this a change that needs to happen in the plugin itself or can I do it globally? My log is filling up with messages.

The instructions above are for the plugin authors who need to implement the change as noted above.

Which plugin are you using that is filling the log?

Re: Python logging used by Indigo

PostPosted: Sat Apr 23, 2016 11:40 am
by fishmg01
I am getting what seems to be an identical problem with endless error messages in the log for the Hue Lights Plugin.

Hue Lights Starting new HTTP connection (1): 10.0.1.184
Hue Lights Starting new HTTP connection (1): 10.0.1.184
Hue Lights Starting new HTTP connection (1): 10.0.1.184
Hue Lights Starting new HTTP connection (1): 10.0.1.184
Etc Etc....

It sounds like the author of the plugin is going to have to implement the fix ? Is that correct?

Re: Python logging used by Indigo

PostPosted: Sat Apr 23, 2016 12:03 pm
by autolog
Yes - suggest you post regarding your issue in the Hue Plugin forum as the author monitors that forum. :)

There is already a thread here :)

Re: Python logging used by Indigo

PostPosted: Wed May 04, 2016 9:13 pm
by RogueProeliator
Code: Select all
logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)

Okay, does it matter where (what module) calls this code? It doesn't seem to work for me... but perhaps I am doing it somewhere/someway that is out of scope? Requests is included with the plugin itself and is called from a module in a subdirectory...

Server Plugin -> RPFramework -> requests

I've never used python logging and will definitely read up on it, but if anyone knows off-hand a gotcha in that scenario, please let me know...

Adam

Re: Python logging used by Indigo

PostPosted: Wed May 04, 2016 9:31 pm
by RogueProeliator
Okay, as often happens, figured it out after posting... if any others run across this, it appears the logger is named as it were loaded in the module that imported it - so in my case I had to do:
Code: Select all
logging.getLogger("RPFramework.requests").setLevel(logging.WARNING)
logging.getLogger("RPFramework.requests.packages.urllib3").setLevel(logging.WARNING)

with the RPFramework being the module/subdirectory of the path to the requests module.

Adam

Re: Python logging used by Indigo

PostPosted: Thu May 05, 2016 9:49 am
by jay (support)
Python logging is incredibly flexible/powerful. But, as is always the case, that flexibility comes at the price of complexity. I think the tradeoff is worth it though. We may not have everything about our approach completely nailed, but we're monitoring and will make changes as necessary.

Re: Python logging used by Indigo

PostPosted: Tue May 10, 2016 8:29 pm
by matt (support)
We've addressed this problem in v6.1.9 that was just posted. Plugins (or modules used by plugins) that request the default logger will no longer log to the Event Log window.

Re: Python logging used by Indigo

PostPosted: Wed May 11, 2016 5:05 am
by autolog
Just to confirm I have switched my plugins' debugging back to setlevel(logging.INFO) and all is working OK again in 6.1.9 :D

Re: Python logging used by Indigo

PostPosted: Sat Aug 03, 2019 9:06 am
by howartp
I haven't yet implemented any loggers() - i'm still using self.debugLog() and indigo.server.log()

Until I get myself sorted, is there any way in imported libraries such as Tesla to log things to either debugLog() or indigo.server.log()?

plugin.py
import indigo
import teslajson
self.debugLog("Started connecting")
connection = teslajson.Connection(self.pluginPrefs['username'],self.pluginPrefs['password'])
self.debugLog("Finished connecting")

teslajson.py
class Connection(object):
def __init__(self,email='',password=''):
HOWDOI.debugLog("opening baseurl")
tesla_client = self.__open2("/raw/pS7Z6yyP", baseurl="http://pastebin.com") #This is TimDorr's version, without id and api
HOWDOI.indigo.server.log("retrieved TeslaClient")
self.oauth = {"grant_type" : "password","client_id" : tesla_client[0],"client_secret" : tesla_client[1],"email" : email,"password" : password }
self.expiration = 0 # force refresh
HOWDOI.errorLog("Happy Birthday To %s".format(tesla_client[0])
self.vehicles = [Vehicle(v, self) for v in self.get('vehicles')['response']]


At the moment i'm havinng to throw(ValueException "HappyBirthdayToYou %s".format(tesla_client[0])) just to test what's happening ... which isn't right!

Peter

Re: Python logging used by Indigo

PostPosted: Sat Aug 03, 2019 9:24 am
by FlyingDiver
Put this at the top
Code: Select all
import logging
logger = logging.getLogger("Plugin.TeslaLibrary")

Put this wherever
Code: Select all
logger.debug(u"Got here with {}".format(something))       

I think that'll work. If the library is class based, you might need to use self.logger instead of a global variable.

Re: Python logging used by Indigo

PostPosted: Sun Nov 10, 2019 10:16 am
by howartp
FlyingDiver wrote:
Put this at the top
Code: Select all
import logging
logger = logging.getLogger("Plugin.TeslaLibrary")

Put this wherever
Code: Select all
logger.debug(u"Got here with {}".format(something))       

I think that'll work. If the library is class based, you might need to use self.logger instead of a global variable.

Necro reply, but thanks - that's working great!