This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Event log creation and presentation
#1
Hi,

Is it possible to have an event log presentation in the LM visualization?
For example, each time a door or window contact opens or a PIR is triggered a log line to be created with a time stamp like this:

- Mon 21/7/2016
  12:32:     Door opened
  10:32:     Movement detection in living room
.
.
- Tue  22/7/2016
  21:01:     Bedroom window opened
  22:12:     Door opened
  00:00:     AC turned off
.
.

.
.

This log needs to be stored in memory for a configurable max number of events (lets say up to 1000) and after that limit, any new events will delete the older one etc so anytime you can have the last 1000 events.
Is there a way to create and present in LM visualization such an event log?
Thank you in advance
Reply
#2
You can use storage engine to create a queue, here's a function that adds an item to a list named eventlog and trims the list to contain up to 1000 entries. Date/time is added automatically, so you only need to supply event text. Create event scripts to log whichever info you need.
Code:
function eventlog(text)
  local max = 1000 -- max number of entries

  -- add formatted date
  text = os.date('%a %d/%m/%Y %T ') .. text

  storage.exec('lpush', 'eventlog', text)
  storage.exec('ltrim', 'eventlog', 0, max - 1)
end

To output the list to client side you can create an .lp page and display it in the visualization via iframe. Create a file called eventlog.lp and upload it to user folder via FTP. Set iframe URL to /user/eventlog.lp
Code:
<?
require('apps')
items = storage.exec('lrange', 'eventlog', 0, 999)
?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Event log</title>
  <script src="/apps/js/jquery.js.gz"></script>
  <link rel="stylesheet" href="/apps/css/bootstrap.css">
</head>
<body>
<script>
$(function() {
  var items = <? json.write(items) ?>
    , container = $('.container')
    , header;

  $.each(items, function(index, item) {
    var chunks = item.split(' ')
      , newheader = chunks.shift() + ' ' + chunks.shift();

    // output data header each time it changes
    if (newheader != header) {
      header = newheader;
      $('<h3></h3>').text(header).appendTo(container);
    }

    // output log entry
    $('<p></p>').text(chunks.join(' ')).appendTo(container);
  });
});
</script>
<div class="container"></div>
</body>
</html>

Note that newest items come on top of the list.
FTP upload docs: http://forum.logicmachine.net/showthread.php?tid=85
Reply
#3
Wow, thank you very much for this fast and delicate solution...
I will try it this weekend... Smile
Reply
#4
Well it works like a charm, thank you once again.
Can I ask if I should be able to see this list in the storage tab?
Also, is it possible to access this log outside the LM or export it for backup or use the information with other programs like excel etc?
Thank you
Reply
#5
For now, item type "list" is not supported directly by storage.get, so you won't be able to see it in storage viewer. As for export you can adapt previously published scripts. This command returns all log entries as a Lua table (plain array) and converts them to plain text with each log item on a new line:
Code:
items = storage.exec('lrange', 'eventlog', 0, 999)
text = table.concat(items, '\r\n')
Reply
#6
Is it possible show the list with more color ?

For example:
- green for alert finished
- red for alert started
KNX Advanced Partner + Tutor
Reply
#7
(03.05.2019, 09:16)toujour Wrote: Is it possible show the list with more color ?

For example:
- green for alert finished
- red for alert started

Try Alert Manager app.
------------------------------
Ctrl+F5
Reply
#8
I prerfer eventlog (thanks).

How Can I delete old alerts in eventlog ?
KNX Advanced Partner + Tutor
Reply
#9
Hi,

You can only delete them all by the delete button or use the alert manager to delete them by line.

Another option is to reduce the alert log size (default 200) so FIFO will take care of it.

Last option is a SQL query to drop them from the DB, in this case you can make a query to drop entry’s older then x period.

Run this as scheduled script once a day / week
Code:
old = 30 -- days
timestamp = os.time() - (old * 86400)
db:query('delete from alerts where alerttime < ?', timestamp)
BR,

Erwin
Reply
#10
(21.07.2016, 10:15)admin Wrote: You can use storage engine to create a queue, here's a function that adds an item to a list named eventlog and trims the list to contain up to 1000 entries. Date/time is added automatically, so you only need to supply event text. Create event scripts to log whichever info you need.
Code:
function eventlog(text)
  local max = 1000 -- max number of entries

  -- add formatted date
  text = os.date('%a %d/%m/%Y %T ') .. text

  storage.exec('lpush', 'eventlog', text)
  storage.exec('ltrim', 'eventlog', 0, max - 1)
end

To output the list to client side you can create an .lp page and display it in the visualization via iframe. Create a file called eventlog.lp and upload it to user folder via FTP. Set iframe URL to /user/eventlog.lp
Code:
<?
require('apps')
items = storage.exec('lrange', 'eventlog', 0, 999)
?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Event log</title>
  <script src="/apps/js/jquery.js.gz"></script>
  <link rel="stylesheet" href="/apps/css/bootstrap.css">
</head>
<body>
<script>
$(function() {
  var items = <? json.write(items) ?>
    , container = $('.container')
    , header;

  $.each(items, function(index, item) {
    var chunks = item.split(' ')
      , newheader = chunks.shift() + ' ' + chunks.shift();

    // output data header each time it changes
    if (newheader != header) {
      header = newheader;
      $('<h3></h3>').text(header).appendTo(container);
    }

    // output log entry
    $('<p></p>').text(chunks.join(' ')).appendTo(container);
  });
});
</script>
<div class="container"></div>
</body>
</html>

Note that newest items come on top of the list.
FTP upload docs: http://forum.logicmachine.net/showthread.php?tid=85

Hi. I would like to use this code, but i don't know where i should put this code:
Code:
function eventlog(text)
  local max = 1000 -- max number of entries

  -- add formatted date
  text = os.date('%a %d/%m/%Y %T ') .. text

  storage.exec('lpush', 'eventlog', text)
  storage.exec('ltrim', 'eventlog', 0, max - 1)
end
event script? resident? sorry im not god coder, please help.
Reply
#11
Place the function definition into Common functions. Then you can call eventlog in any script.
Reply
#12
(30.12.2021, 15:01)admin Wrote: Place the function definition into Common functions. Then you can call eventlog in any script.

Thanks, working like a charm.
Reply
#13
(30.12.2021, 15:01)admin Wrote: Place the function definition into Common functions. Then you can call eventlog in any script.

One more questions. 

Is possible to clear some logs in eventlog? For example if error is not longer active? For now is working only by Restet/Clean Up (Utility's) and check Script storage. I've tried with Alert manager and is not working. Any ideas?


Thanks.
Reply
#14
If you want to clear the list completely you can simply delete the storage entry:
Code:
storage.delete('eventlog')

You can remove a certain event from the list like this:
Code:
text = 'Fri 07/01/2022 14:13:51 example event'
storage.exec('lrem', 'eventlog', 0, text)

If you want to do this from the UI then it's more complicated. You need an additional .lp to handle delete requests.
Reply
#15
(07.01.2022, 12:20)admin Wrote: If you want to clear the list completely you can simply delete the storage entry:
Code:
storage.delete('eventlog')

You can remove a certain event from the list like this:
Code:
text = 'Fri 07/01/2022 14:13:51 example event'
storage.exec('lrem', 'eventlog', 0, text)

If you want to do this from the UI then it's more complicated. You need an additional .lp to handle delete requests.
Thanks. This is what's a meant.
Reply
#16
(21.07.2016, 10:15)admin Wrote: You can use storage engine to create a queue, here's a function that adds an item to a list named eventlog and trims the list to contain up to 1000 entries. Date/time is added automatically, so you only need to supply event text. Create event scripts to log whichever info you need.
Code:
function eventlog(text)
  local max = 1000 -- max number of entries

  -- add formatted date
  text = os.date('%a %d/%m/%Y %T ') .. text

  storage.exec('lpush', 'eventlog', text)
  storage.exec('ltrim', 'eventlog', 0, max - 1)
end

To output the list to client side you can create an .lp page and display it in the visualization via iframe. Create a file called eventlog.lp and upload it to user folder via FTP. Set iframe URL to /user/eventlog.lp
Code:
<?
require('apps')
items = storage.exec('lrange', 'eventlog', 0, 999)
?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Event log</title>
  <script src="/apps/js/jquery.js.gz"></script>
  <link rel="stylesheet" href="/apps/css/bootstrap.css">
</head>
<body>
<script>
$(function() {
  var items = <? json.write(items) ?>
    , container = $('.container')
    , header;

  $.each(items, function(index, item) {
    var chunks = item.split(' ')
      , newheader = chunks.shift() + ' ' + chunks.shift();

    // output data header each time it changes
    if (newheader != header) {
      header = newheader;
      $('<h3></h3>').text(header).appendTo(container);
    }

    // output log entry
    $('<p></p>').text(chunks.join(' ')).appendTo(container);
  });
});
</script>
<div class="container"></div>
</body>
</html>

Note that newest items come on top of the list.
FTP upload docs: http://forum.logicmachine.net/showthread.php?tid=85

Hi,

I coudn't diplay eventlog.lp file as a frame, it will display "File not found: /www/user/eventlog.lp".
I uploaded the file via FileZilla as ou can see in the attachment

Attached Files Thumbnail(s)
   
Reply
#17
Path should be without www, just "/user/eventlog.lp"
Reply
#18
(14.09.2022, 11:43)admin Wrote: Path should be without www, just "/user/eventlog.lp"

(14.09.2022, 11:44)Fahd Wrote:
(14.09.2022, 11:43)admin Wrote: Path should be without www, just "/user/eventlog.lp"

It's already without www

Attached Files Thumbnail(s)
   
Reply
#19
Run this once and post what you get in Logs tab:
Code:
log(io.ls('/data/apps/store'))
log(io.ls('/data/apps/store/user'))
Reply
#20
(14.09.2022, 13:09)admin Wrote: Run this once and post what you get in Logs tab:
Code:
log(io.ls('/data/apps/store'))
log(io.ls('/data/apps/store/user'))
Code:
* table:
[1]
  * string: cron
[2]
  * string: public
[3]
  * string: user
[4]
  * string: libs
[5]
  * string: data
[6]
  * string: cloud
[7]
  * string: daemon

Code:
* table:
[1]
  * string: eventlog.Ip
Reply


Forum Jump: