Get and send previous logs

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
Foune
Experienced User
Posts: 94
Joined: Sun Jul 03, 2011 7:08 pm

Get and send previous logs

Post by Foune » Mon Sep 04, 2017 7:38 am

Hello,

I'm debugging "complex" scenarios based on presence detection in my home.

I need a script that collects the 50 previous log entries and send them to a specific email address.

Is it possible ?

User avatar
kgschlosser
Site Admin
Posts: 3928
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Get and send previous logs

Post by kgschlosser » Mon Sep 04, 2017 8:23 am

Oh come on now...

LOL

Of course it is..
and what log are you trying to send? an EG log?
If you like the work I have been doing then feel free to Image

User avatar
kgschlosser
Site Admin
Posts: 3928
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Get and send previous logs

Post by kgschlosser » Mon Sep 04, 2017 9:38 am

You will want to trigger this script. which will set the last 50 log entries into eg.result. which can then be used in conjunction with the E-Mail plugin by Pako to send the e-mail. you will need to put {eg.result} into the body section of the send e-mail action.

Code: Select all

from time import strftime, localtime

ICONS = {
    eg.Icons.EVENT_ICON: 'EVENT: ',
    eg.Icons.NOTICE_ICON: 'NOTICE: ',
    eg.Icons.INFO_ICON: 'INFO: ',
    eg.Icons.MACRO_ICON: 'MACRO: ',
    eg.Icons.ACTION_ICON: 'ACTION: ',
    eg.Icons.PLUGIN_ICON: 'PLUGIN: ',
    eg.Icons.AUTOSTART_ICON: 'AUTOSTART: '
}

PLUGIN_ICONS = list(plugin.info.icon for plugin in eg.pluginList)

capture = 50
if len(eg.log.data) < 50:
    capture  = len(eg.log.data)

results = []
for i in range(len(eg.log.data) - capture, len(eg.log.data)):
    results += [eg.log.data[i]]

for i, result in enumerate(results):
    line, icon, wRef, when, indent = result
    if eg.config.eg.Classes.MainFrame.logTime:
        when = strftime(" %H:%M:%S   ", localtime(when))
    else:
        when = ' '

    if icon in ICONS:
        entry_type = ICONS[icon]
    elif icon == eg.Icons.ERROR_ICON:
        entry_type = 'ERROR: '
        if len(line) > 7:
            if when !=  ' ' and line[:8] == when[1:9]:
                entry_type = 'DEBUG: '
            else:
                digits = line[:8].split(':')
                if len(digits) == 3:
                    entry_type = 'DEBUG: '
                    for digit in digits:
                        if len(digit) < 2 or not digit.isdigit():
                            entry_type = 'ERROR: '
                            break
    elif icon in PLUGIN_ICONS or line.startswith('Plugin:'):
        entry_type =  'PLUGIN: ' 
    else:
        entry_type =  'ACTION: '

    if eg.config.eg.Classes.MainFrame.indentLog:
        line =  entry_type + when + ' ' * indent + line
    else:
         line = entry_type + when + line
    results[i] = line

eg.result = '\n'.join(results)
I have tested this and it does work. now there are so many variables to detecting the type of log entry so I don't know if I have them all covered. but I can code in the ability to send mime attachments and convert the icon to be able to send it in the e-mail. I can also format the whole output to look like a screen shot of the log if you like. This would require you to have viewing of HTML turned on in your e-mail client/web provider. I think that would be the only way to have it properly format the data with the viewing of the icons. But this script will prepend ACTION: or DEBUG: MACRO: those kinds of things for the various types of log entries.
If you like the work I have been doing then feel free to Image

User avatar
kgschlosser
Site Admin
Posts: 3928
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Get and send previous logs

Post by kgschlosser » Mon Sep 04, 2017 9:44 am

It also takes into account of the log times are set and if you have indenting turned on as well as if you have debugging turned on.
If you like the work I have been doing then feel free to Image

User avatar
topix
Experienced User
Posts: 413
Joined: Sat May 05, 2007 3:43 pm
Location: Germany
Contact:

Re: Get and send previous logs

Post by topix » Mon Sep 04, 2017 6:09 pm

There's also a log redirect plugin: viewtopic.php?f=9&t=2852&hilit=log+sender#p16393

User avatar
kgschlosser
Site Admin
Posts: 3928
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Get and send previous logs

Post by kgschlosser » Mon Sep 04, 2017 7:09 pm

I have never used the log redirect plugin how does it handle dealing with the icons and log times?. also does it convert the log into pure text with markers for what the actual log entries are?
If you like the work I have been doing then feel free to Image

User avatar
kgschlosser
Site Admin
Posts: 3928
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Get and send previous logs

Post by kgschlosser » Mon Sep 04, 2017 7:17 pm

ok i took a look at it. it does handle converting the log to text but it needs to be updated to handle the debugging message. because the debugging messages use the error icon it will think they are errors. and it has no action to return a reformatted version of the log. it will only write it to a file. either way it is a really neat plugin that could be expanded upon greatly and updated to work with the debugging message and also an action to return the currently stored log entries.
If you like the work I have been doing then feel free to Image

Foune
Experienced User
Posts: 94
Joined: Sun Jul 03, 2011 7:08 pm

Re: Get and send previous logs

Post by Foune » Wed Sep 20, 2017 8:59 am

Thank you for your help, kgschlosser . i'll try your script and give a feedback.
Eventghost is becoming critical in my setup nowadays :

- Light
- Heating

These features are (or will soon be) dependant of my eventghost setup. In some rooms, no eventghost, no light. I have virtualized my home automation machine in order to make periodic snapshots and facilitate the recovery in case of crash.
Your script will be precious to improve my scripts behaviour.

Additionnally, I'll try to find how to generate/send this log when an exception/error is triggered.

Post Reply