AppDaemon Debug Mode


#1

I see where you can start AppDaemon with Debug mode. Can you start individual apps in Debug mode, or toggle debug mode on or off with a AppDaemon function call within the app in question. I don’t want to turn on debug for all apps at the same time, or I wouldn’t be able to find anything because of all the messages in the log.


#2

There is no explicit support for doing that within Apps, but it would be very easy to add a debug arg:

[App]
module = <some module>
class = <some class>
DEBUG = 1

Then in your app:

...
if self.args["DEBUG"] == 1:
  self.log("some kind of debug message")

Better yet, write a function:

def debug(self, text):
  if self.args["DEBUG"] == 1:
    self.log(text)

Then call it everywhere with:

self.debug("some debug text")

And it would opnly print if you set DEBUG = 1 in your config, which you can change as often as you like without restarting AppDaemon.


#3

Ok I did this

in init do the following
    self.LOGLEVEL="DEBUG"    

Then I ovrerode self.log so it uses the same parameters as the normal one, But if self.LOGLEVEL is set, then if the level you pass through to log is < LOGLEVEL, it will print out the message. Otherwise, it skips it like the python logger does.

  def log(self,message,level="INFO"):
    levels = {
              "CRITICAL": 50,
              "ERROR": 40,
              "WARNING": 30,
              "INFO": 20,
              "DEBUG": 10,
              "NOTSET": 0
            }
    if hasattr(self, "LOGLEVEL"):
      if levels[level]>=levels[self.LOGLEVEL]:
        super().log("{} - message={}".format(level,message))
    else:
      super().log("{}".format(message),level)

Because I’m using the python’s logger, it still gets their status at the beginning of the line, and then my status shows up further down the line, but it serves my purpose.


Announcement: AppDaemon beta 3
#4

i just use a lot of self.log(“something”) when i am writing and controlling an app.
as soon as i think that it functions like it should it take it out.


#5

Yea, I do the
self.log(“something”,level=“DEBUG”)

That way I can turn debug back on if I need to and see what’s going on if something goes screwy, rather than go back in and have to start putting log statements in the same places again. Especially since dealing with dictionaries, the statements can get rather long.


#6

if i want to leave them in for later use i just use # to edit them out.
but your way is more like it should be done.
i am to lazy to type ,level=“DEBUG” every time :wink:


#7

Yea, I usually cut and paste that in after I’m done developing. Probably when you go in and delete the line. :slight_smile: