using indigo logger in imported class

Posted on
Mon Dec 03, 2018 1:07 am
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

using indigo logger in imported class

trying to use indigo debug logger in an imported class.

this works:
Code: Select all
import logging

class Plugin(indigo.PluginBase):

   def __init__(self, pluginId, pluginDisplayName, pluginVersion, pluginPrefs):
....

   def startup(self):
      indigo.server.log("myLogSet doing logging stuff")
      logformatLOG = logging.Formatter('%(msg)s')
      self.indigo_log_handler.setFormatter(logformatLOG)
      self.indigo_log_handler.setLevel(logging.WARNING)

      logformatFile = logging.Formatter( '%(asctime)s.%(msecs)03d\t%(levelname)-12s\t%(name)s.%(funcName)-25s %(msg)s' , datefmt='%H:%M:%S')
      self.plugin_file_handler.setFormatter(logformatFile)      
      self.plugin_file_handler.setLevel(logging.THREADDEBUG)
   
      self.mylogger = logging.getLogger("minMax")
   
      self.mylogger.info("info")
      self.mylogger.warning("warning")
      self.mylogger.error("error")
      indigo.server.log("myLogSet end  logging stuff")


this does not work :
Code: Select all
import indigo
# import logging

class MLX():

   def __init__(self):
      return

   def myLogSet(self, **kwargs ):
      for key, value in kwargs.iteritems():

         elif key == "pluginSelf" :
            self.plugin         = value

         elif key == "logging":
            self.logging      = value


      indigo.server.log("myLogSet doing logging stuff")
      logformatLOG = logging.Formatter('%(msg)s')
      self.plugin.indigo_log_handler.setFormatter(logformatLOG)
      self.plugin.indigo_log_handler.setLevel(logging.WARNING)

      logformatFile = logging.Formatter( '%(asctime)s.%(msecs)03d\t%(levelname)-12s\t%(name)s.%(funcName)-25s %(msg)s' , datefmt='%H:%M:%S')
      self.plugin.plugin_file_handler.setFormatter(logformatFile)      # w date time ...
      self.plugin.plugin_file_handler.setLevel(logging.THREADDEBUG)
   
      self.mylogger = logging.getLogger("minMax")
   
      self.mylogger.info("info")
      self.mylogger.warning("warning")
      self.mylogger.error("error")
      indigo.server.log("myLogSet end  logging stuff")
      return


and in plugin.py

import myLogPgms.myLogPgms
import logging

class Plugin(indigo.PluginBase):

   def __init__(self, pluginId, pluginDisplayName, pluginVersion, pluginPrefs):
....

   def startup(self):
      self.ML = myLogPgms.myLogPgms.MLX()
      self.ML.myLogSet(pluginSelf=self, logging=logging)
      

the self.mylogger statements do nothing, no output, no error.

Question: how do I use the indigo debug logger in an imported class.

tried all kinds of variations w local logger import, handed down from indigo ...import indigo in imported file ...
have looked at other plugins ..
??
any pointer is welcome.

Karl

Posted on
Mon Dec 03, 2018 10:24 am
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: using indigo logger in imported class

In your startup call, you're passing the logging module. I think what you want to do it pass the logger (self.logger):

Code: Select all
   def startup(self):
      self.ML = myLogPgms.myLogPgms.MLX()
      self.ML.myLogSet(pluginSelf=self, logging=logging) # should be self.logger since that's the logger instance created for the plugin


But there are a other potential issues in your MLX class. I think before trying to identify everything that's wrong there, maybe we should get a description (not code) of exactly what you're trying to accomplish from your logging exactly. Can you just describe that and maybe we can help simplify this for you?

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Dec 03, 2018 2:25 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: using indigo logger in imported class

just found it after a long walk and a lunch w w:

correct:
Code: Select all
         self.mylogger = logging.getLogger("Plugin.minMax")

does not work
Code: Select all
         self.mylogger = logging.getLogger("minMax")
would be nice to have a simple code section , like in the SKD package how to get started with the indigo logger

Karl


here my working code:
Code: Select all
import logger
         logformatLOG = logging.Formatter('%(msg)s')
         self.indigo_log_handler.setFormatter(logformatLOG)
         self.indigo_log_handler.setLevel(logging.WARNING)

         logformatFile = logging.Formatter( '%(asctime)s.%(msecs)03d\t%(levelname)-12s\t%(name)s.%(funcName)-25s %(msg)s' , datefmt='%H:%M:%S')
         self.plugin_file_handler.setFormatter(logformatFile)      # w date time ...
         self.plugin_file_handler.setLevel(logging.THREADDEBUG)
      
         self.mylogger = logging.getLogger("Plugin.minMax")
      
         self.mylogger.info("info")
         self.mylogger.warning("warning")
         self.mylogger.error("error")


prints: to stand indigo log:
Code: Select all
 minMax Warning                  warning
   minMax Error                    error
 
and to .../Logs/plugin id ../plugin.log
Code: Select all
14:20:05.780   INFO           Plugin.minMax.myLogSet                  info
14:20:05.780   WARNING        Plugin.minMax.myLogSet                  warning
14:20:05.781   ERROR          Plugin.minMax.myLogSet                  error

Posted on
Mon Dec 03, 2018 4:28 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: using indigo logger in imported class

I had my own debugging schema, with some logic with ifs/ error printouts, formatting / file management ...

Realizing that that does not work well with the indigo logger as it prints always the calling method / function. ... and that would always be the same :my logging method, not the method that calls the logging method..

Is all done in a one liner:
if self.ML.decideMyLog(u"Sql"): self.ML.myLog( errorType = u"bigErr", text =" msg here ", mType=" abc", showDate=True, destination="standard")
if SQL logging on, then print text to different log files depending on options, with prefix and w/wo date-stamp...

anyway will need to consider if switching adds anything, besides some learning

Is there a way to not print the current function but the preceding in indigo logger?

Karl

Posted on
Mon Dec 03, 2018 5:18 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: using indigo logger in imported class

kw123 wrote:
does not work
Code: Select all
         self.mylogger = logging.getLogger("minMax")


Well, it did in fact work, it just created a new logger named "minMax" that wasn't in the plugin's logger chain so nothing showed up in the places you wanted.

We do need to add logging docs (other than what's in the Indigo 7.0 announcement thread).

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Dec 03, 2018 5:45 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: using indigo logger in imported class

kw123 wrote:
Is there a way to not print the current function but the preceding in indigo logger?


Not sure I'm following - can you show me an example?

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Dec 03, 2018 6:28 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: using indigo logger in imported class

current setup:
in
Code: Select all
   def pickDeviceCALLBACK(self,valuesDict="",typeId=""):
I call:
Code: Select all
      if self.ML.decideMyLog(u"Setup"): self.ML.myLog( text= unicode(valuesDict), mType="pickDeviceCALLBACK" )

if Setup --> ON:
Code: Select all
pickDeviceCALLBACK   unicode(valuesDict)
will be printed to a logfile.


with
logformatFile = logging.Formatter( '%(asctime)s\t%s%(funcName)-25s %(msg)s' , datefmt='%H:%M:%S')

When doing
def self.ML.myLog(text=""):
self.mylogger.info(text)

it will print
"time myLog text"
should do
"time pickDeviceCALLBACK text"

instead of myLog print the calling function pickDeviceCALLBACK.

I guess the logger has to be IN the function.. ... that prevents logic I would like to add

looking at the doc for logger thats likely not in the cards at least not that easily.

Anyway it works for me right now. will tackle it again when i have time.

thanks

Karl

Posted on
Tue Dec 04, 2018 11:32 am
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: using indigo logger in imported class

Right - wrapper methods around logging will most likely result in function names not working right (at least using the built-in log formatter). I'd recommending simplifying - lots of complexity in what you're doing and you'd likely do better just rethinking it in light of how the logger works.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Dec 04, 2018 12:19 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: using indigo logger in imported class

These thing are historically grown

Problem is that I have > 2000 log statements with many little variations. So a simple change string will not work.

Was looking for a simple way to move a conversion to a method all my log actions go through currently.


Sent from my iPhone using Tapatalk

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 10 guests