Outputting information from the plugin log

Posted on
Wed May 16, 2018 7:03 am
vtmikel offline
Posts: 628
Joined: Aug 31, 2012
Location: Boston, MA

Outputting information from the plugin log

Is it dangerous to offer a menu item that reads the plugin log and outputs certain lines to the Event Log? I was just thinking that this could result in an infinite loop depending on how Indigo handled the logging.

It would be a very limited set of lines, I wanted to offer an easy way for the user to see a list of results that the plugin takes over a period of time, and would be hard to see just traversing their event log.

Code:
Code: Select all
      try:
         indigo.server.log("Listing filter block log records:")
         for filename in os.listdir(self.LogFileLoc):
            log_file = open(self.LogFileLoc + filename, "r")

            for line in log_file.readlines():
               if "Filter block:" in line or "will not" in line:  # "will not is legacy, will be depreciated"
                  indigo.server.log(line)

         indigo.server.log("completed log output")

      except Exception as e:
         self.logger.error("An error occured while reading the filter log: " + str(e))

Posted on
Wed May 16, 2018 7:10 am
vtmikel offline
Posts: 628
Joined: Aug 31, 2012
Location: Boston, MA

Re: Outputting information from the plugin log

I've answered my own question, this was a bad idea. The logs will perpetuate itself. I'll pursue other ways of accomplishing this.

Posted on
Wed May 16, 2018 10:27 am
kw123 offline
User avatar
Posts: 8333
Joined: May 12, 2013
Location: Dallas, TX

Re: Outputting information from the plugin log

you can use a regular indigo trigger on errors in the logfile .. but only errors not regular log entries

Karl

Posted on
Wed May 16, 2018 10:34 am
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Outputting information from the plugin log

vtmikel wrote:
I've answered my own question, this was a bad idea. The logs will perpetuate itself. I'll pursue other ways of accomplishing this.


Have you read the post about the use of Python logger in Indigo 7? It may lead you to a solution. Python logging is almost infinitely flexible.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed May 16, 2018 11:02 am
vtmikel offline
Posts: 628
Joined: Aug 31, 2012
Location: Boston, MA

Re: Outputting information from the plugin log

I just re-read it to be sure. Though I'm not sure it helps. Here's what I'm trying to do:

1. In my Grafana plugin, the user has the ability to set up filtering rules that execute when updates to their indigo devices occur (deviceUpdated, variableUpdated)
2. Each rule as it is processed, the results can be optionally outputted to the Indigo Event Log
3. Regardless of how the user configures the event log option, the logs are put into my plugin log for debugging..
4. However, since the rules are processed as device updates come in, you could easily miss a log entry. I wanted to make it easy to show these log entries by traversing the plugin log, filtering for certain messages, and outputing it to the Indigo Log. This is for the USER, not for debugging my plugin.

I run into a parsing problem while doing this. Every time I ran using the plugin log as the source, it duplicates the log entries in the plugin.log since I'm taking data from the log and placing it back into the Indigo log. So, I've moved away from this strategy.

I've now created a new log file of my own inside the plugin. It contains an entry for the filters that I can easily output to the Indigo Log via the Menu Item I've added. Plugin updates will clear the log, and I've created a way to prune it, and tail any outputs to limit to the the last 50 messages.

Good approach?

Posted on
Wed May 16, 2018 1:18 pm
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Outputting information from the plugin log

Yeah, that's OK. You can write it to the Preferences/Plugins folder so that it persists between upgrades (and even Indigo upgrades since we move that entire folder over):

Code: Select all
cacheFilePath = "{}/Preferences/Plugins/{}.cache".format(indigo.server.getInstallFolderPath(), self.pluginId)


That will always give you a good path to a cache file named using your plugin's ID (so it'll likely be unique).

You could also store the cache data in your plugin's prefs file, though that's probably less optimal if it's a large amount of data (since it has to round-trip through the server to be read/written).

So, just to summarize, it's really less about doing the actual write to the log vs storing what you've previously logged in a way that can be logged later, right?

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed May 16, 2018 1:23 pm
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Outputting information from the plugin log

It just occurred to me that you can probably use Python's logger with the filename as specified above. Then you'd use a file handler to do the writing, which also has options for automatically trimming data, etc. Then you'd just need the ability to read from that file and re-log it to the standard logger that the plugin gets when it starts up.

Just thinking a bit...

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 0 guests

cron